kirki icon indicating copy to clipboard operation
kirki copied to clipboard

Allowing same 'settings' value multiple times

Open phlangf opened this issue 3 years ago • 2 comments

Issue description:

I would like to save certain fields into my database with a different option_name, but the same option_value keys. But only the last Field is shown in the Customizer. I suppose it is because the 'settings' are the same.

new \Kirki\Field\Checkbox_Toggle(
    [
      'settings'    => 'aktiv',
    
      'option_name' => 'A',
      'option_type' => 'option',
    ]
  );

  new \Kirki\Field\Checkbox_Toggle(
    [
      'settings'    => 'aktiv',
    
      'option_name' => 'B',
      'option_type' => 'option',
    ]
  );

Furthermore I would like to display different select fields depending on the main select, and again save their values with the same key. But again only the last dependent Select is shown. I could have subvalue1 and subvalue2 as settings, but this would bloat the database and calling the values would require unnecessary conditionals using the choices of Main Select.

  // Main Select
  new \Kirki\Field\Select(
    [
      'settings' => 'item',

      'option_name' => 'select',
      'option_type' => 'option',

      'choices'     => [
        'A' => esc_html__( 'A', 'kirki' ),
        'B' => esc_html__( 'B', 'kirki' ),
      ],
    ]
  );

  // Dependent Select 1
  new \Kirki\Field\Select(
    [
      'settings' => 'subvalue',

      'option_name' => 'select',
      'option_type' => 'option',

      'choices'     => [
              ... ABC
      ],

      'active_callback'  => [
        [
          'setting'  =>  'item',
          'operator' => '===',
          'value'    =>  'A',
        ],
      ]
    ]
  );

  // Dependent Select 2
  new \Kirki\Field\Select(
    [
      'settings' => 'subvalue',

      'option_name' => 'select',
      'option_type' => 'option',

      'choices'     => [
              ... DEF
      ],

      'active_callback'  => [
        [
          'setting'  =>  'item',
          'operator' => '===',
          'value'    =>  'B',
        ],
      ]
    ]
  );

And another use case where this happens to me is the following.

foreach ([A,B,C,D] as $i) {

    new \Kirki\Field\Checkbox_Toggle(
	    [
		    'settings'    => 'aktiv',
	    
		    'option_name' => 'item_'.$i,
		    'option_type' => 'option',
	    ]
    );
};

// Only shows the last and I need to make 'settings'  => $i.'-aktiv' to make it work

Summary:

I would love to have the possibility to have 'settings' the same when a different option_name or the active_callback is used. Then I could keep my database concise and my code clean.

Best regards. Love your plugin.

Version used:

4.0.24

Using theme_mods or options?

options

phlangf avatar Jun 14 '22 20:06 phlangf

I'm fairly new to Kirki myself, so take what I have to say with a grain of salt, but I'm not sure there's anything that can be within Kirki to accomplish what you want.

You're dead right that the reason only the last saved value is being returned is because you're using the same "settings" name, and there's no way around that. There has to be a unique identifier somewhere, someway.

what you're essentially doing in code is:

// set a unique variable with unique array values
$array = array( [0] => 'some value', [1] => 'some other value', );

// overwrite that unique variable with new unique values
$array = array( [0] => 'some new value', [1] => 'some other new value', );

// will ALWAYS only give you the last update
echo $array[0]; // shows 'some new value'
echo $array[1]; // shows 'some other new value',

There's no way around the requirement of having a unique identifier, and unique subfields for each unique identifier. If any are the same, they will overwrite eachother.

The solution is exactly what you don't want to do - use a unique identifier, and the additional code/calls necessary to work with that unique identifier and its' unique subfields. e.g.:

$i.'-aktiv'

codejp3 avatar Oct 03 '22 04:10 codejp3