Support

Account

Home Forums Feature Requests Rearrangeable fields

Helping

Rearrangeable fields

  • Imagine a scenario where you have 5 fields, each is a URL field relating to a specific social network.

    They are set in a specific order because that’s how they’ve been set up. The output is also hardcoded and therefore fixed.

    What happens if the user wants to rearrange the order from within WP… they can’t.

    I’d like to see a new field type that is similar to the repeater field, but doesn’t have the repeat functionality. In other words, you create a single field within which there are defined sub-fields. These sub-fields can be rearranged by the user as required and the output is a sub_field relationship just like the repeater field, making it totally flexible.

    Now you may say why not just use a repeater field – well you can, but it’s not perfect. You could have a select menu and URL that allows you to choose the pre-defined social netowrk from the menu and enter a corresponding URL. The downside here is that the user has the ability to select any single social network more than once, which may not be desirable.

    If there are any extensions that do this it would be great to know where, otherwise I believe this could be a very useful new field.

  • Using the acf validation filters it is possible to prevent the user from selecting the same choice more than once.

    Let’s say that you have a repeater field with a key of ‘field_0123456789abc’ and it has a sub field with a key of ‘field_cba9876543210’ and you want to make sure that the sub field in each rows has a unique value.

    
    <?php 
    // validate one row of a repeater against other rows of same repeater
    add_filter('acf/validate_value/key=field_cba9876543210', 'sub_field_unique', 10, 3);
    function sub_field_unique($valid, $value, $field, $input) {
      if (!$valid) {
        return $valid;
      }
      $repeater = 'field_0123456789abc';
      $subfield = 'field_cba9876543210';
      $values = $_POST['acf'][$repeater];
      if (!count($values)) {
        return $valid;
      }
      // extract the current row index from $input
      $current_row = preg_replace('/^\s*acf\[[^\]]+\]\[([^\]]+)\].*$/', '\1', $input);
      $count = count($values);
      foreach ($values as $index => $row) {
        if ($index != $current_row && $value == $row[$subfield]) {
          $valid = 'value must be unique';
        }
      }
      return $valid;
    }
    ?>
    

    The exact logic may be different depending on the field type.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Rearrangeable fields’ is closed to new replies.