• Resolved Javier

    (@xarkitu)


    Hi miunosoft,

    I have the need and the doubt as dynamically add fields.

    If I want to dynamically create fields, for example:

    $ArrayFields = array(
                       'label'             => 'Ananas',
                       'default'           => 'a'
                    );
    
    $this->addSettingFields(
            array(
                'field_id'          => 'sortable_labels',
                'type'              => 'hidden',
                'title'             => __( 'Sortale Labels', 'admin-page-framework-example' ),
                'sortable'          => true,
    
                &ArrayFields,
    
            )
        );

    The intention is to add as many fields as needed from data created by my application.

    Unable to find a way to do this, not if someone had a need and I could help.

    Greetings and thanks in advance for any help.

    https://ww.wp.xz.cn/plugins/admin-page-framework/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author miunosoft

    (@miunosoft)

    Hi,

    I guess you want to dynamically create sortable fields. You need to define numerically indexed array elements for the field definition array for sortable items.

    In your case, maybe,

    $ArrayFields = array(
        'label'             => 'Apple',
        'default'           => 'a',
        array(
               'label'             => 'Bananas',
               'default'           => 'b',
        ),
        array(
               'label'             => 'Cherry',
               'default'           => 'c',
        ),
    );
    
    $this->addSettingFields(
        array(
            'field_id'          => 'sortable_labels',
            'type'              => 'hidden',
            'title'             => __( 'Sortale Labels', 'admin-page-framework-example' ),
            'sortable'          => true,
        ) + $ArrayFields
    );

    Notice that the first sortable item (Apple) defined in the $ArrayFields array does not have a numeric index.

    So just create your custom function to format the $ArrayFields array from your generated data.

    Hope it helps.

    Thread Starter Javier

    (@xarkitu)

    Hey, this is great 🙂

    This opens many doors for my coding.

    //My array
    $ArrayList = array(
       'a'	=> 'Apple',
       'b'  	=> 'Bananas',
       'c' 	=> 'Cherry,
    );
    
    //My default sorteable list
    foreach($ArrayList as $key => $lb){
       $def[] = $key;
    }
    
    //Get the fields...
    $first = true;
    foreach($this->getOption('sortable_labels', $def ) as $option) {
       if($first) {
          $MyFields = array(
             'label'         => $ArrayList[$option],
             'default'       => $option,
          );
          $first = false;
       } else {
          $MyFields[] =  array(
             'label'     => $ArrayList[$option],
             'default'   => $option,
          );
       }
    }
    
    //Publishing the field...
    $this->addSettingFields(
        array(
          'field_id'          => 'sortable_labels',
          'type'              => 'hidden',
          'title'             => __( 'Sortale Labels', 'admin-page-framework-example' ),
         'sortable'          => true,
        ) + $MyFields
    );

    Miunosoft thank you very much for your support, I hope this helps anyone seeking to codify something.

    best regards

    Plugin Author miunosoft

    (@miunosoft)

    Glad to hear you found a way! Good luck!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Dynamically create fields’ is closed to new replies.