Support

Account

Home Forums Add-ons Repeater Field Repeater text field default values

Solved

Repeater text field default values

  • I have a repeater field with 2 sub-fields (Text and Checkbox).

    The field is outputted on the theme options page.

    What I would like to do is set the text/selected rows as default when a user installs the theme fresh. I have attempted to use the following code:

    field_59977c2759a56 – The text subfield
    field_599841d665210 – The checkbox subfield

    add_filter('acf/load_value/name=ek-search-date', 'acf_load_ek_search_date_value', 10, 3);
    
      function acf_load_ek_search_date_value($value, $post_id, $field) {
        if ($value !== NULL) {
          return $value;
        }
        $subfield_values = array(
          // field_key => array of values
          'field_59977c2759a56' => array('24 hours ago', '3 days ago', '7 days ago','14 days ago','30 days ago','Anytime'),
          'field_599841d665210' => array('','','','','1',''),
        );
        $values = array();
        foreach ($subfield_values as $field_key => $field_values) {
          for ($i=0; $i<count($field_values); $i++) {
            $values[$i][$field_key] = $field_values[$i];
          }
        }
        $value = $values;
        return $value;
      }

    Is there a way that if all rows are empty, then populate with the default rows? (i.e – 24 hours ago, 3 days ago etc.)

    Thanks!

  • The load field filter your using is correct except for the format of the subfield rows

    
    
        $subfield_values = array(
          // nested array for each row
          array(
            // field key value pairs for each sub field
            'field_59977c2759a56' => '24 hours ago',
            'field_599841d665210' => 0
          ),
          array(
            // field key value pairs for each sub field
            'field_59977c2759a56' => '3 days ago',
            'field_599841d665210' => 3 days ago
          ),
          // etc
          array(
            // field key value pairs for each sub field
            'field_59977c2759a56' => '30 days ago',
            'field_599841d665210' => 1
          ),
          // etc
        );
    
  • @hube2 Ah I see! Thanks. I’ve switched, but fields are still not appearing once the ek-search-date repeater is empty. Also tried on a new install and set to key instead of name. Is there another set I’m missing?

    Thanks!

    UPDATE:
    2 Fields will output as blank if I change to:

         if ($value === NULL) {
           return $value;
         }
  • Got it working by removing the foreach and just spitting out the return value:

    add_filter('acf/load_value/key=field_599774ca9d074', 'ek_option_defaults', 10, 3);
      function ek_option_defaults($value, $post_id, $field) {
          if ($value === false) {
              $value = array(
                  array(
                      'field_59977c2759a56' => '24 hours ago',
                      'field_599841d665210' => '',
                  ),
                  array(
                      'field_59977c2759a56' => '3 days ago',
                      'field_599841d665210' => '',
                  ),
                  array(
                      'field_59977c2759a56' => '7 days ago',
                      'field_599841d665210' => '',
                  ),
                //etc
    
              );
          }
          return $value;
      }
  • Glad you got it worked out. I missed that part

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

You must be logged in to reply to this topic.