Support

Account

Home Forums Add-ons Repeater Field Setting Repeater Defaults Per Page Template

Solving

Setting Repeater Defaults Per Page Template

  • I have a sidebar that the client can edit by adding modules via a repeater. The repeater has a select list that allows the client to add/remove/move a blocks at will.

    To avoid having to add the same blocks on multiple pages, they would like defaults set when creating a new page, while retaining the custom ability of the repeater.

    The default blocks are different depending on the template.

    Is this possible?

  • Hi there,

    Here’s an example that allows you to set defaults in a repeater dynamically, for when there is no current value:
    .. change the ‘key’ value in the filter call… you can expose the ‘key’ when editing your fields by checking the box in Screen Options (top right)

    
    <?php
    function my_default_value_subs($value, $post_id, $field) {
      if ($value === false) {
        $value = array(
          array(
            'field_58984ddb5b8cc' => 'My title',
            'field_58985045abac0' => 'My content',
          ),
          array(
            'field_58984ddb5b8cc' => 'Another title for a content-less row',
          ),
        );
      }
      return $value;
    }
    add_filter('acf/load_value/key=key_58c7ee3a37aee', 'my_default_value_subs', 10, 3);
    ?>
    

    Now… to make it conditional based on a page template.. you could use the function:

    get_page_template_slug( get_the_ID() );

    This returns the ‘slug’ of the page template…

    So… they would choose the template first… save the post (as Draft or whatever), and then the defaults would be set if you modified the above code to be like:

    
    <?php
    function my_default_value_subs($value, $post_id, $field) {
      if ($value === false) {
        switch (get_page_template_slug( get_the_ID() )) {
          case 'about.php':
            $value = array(
              array(
                'field_58984ddb5b8cc' => 'My title',
                'field_58985045abac0' => 'My content',
              ),
              array(
                'field_58984ddb5b8cc' => 'Another title for a content-less row',
              ),
            );
            break;
          case 'other-template.php':
            $value = array(
              array(
                'field_58984ddb5b8cc' => 'My title',
                'field_58985045abac0' => 'My content',
              ),
              array(
                'field_58984ddb5b8cc' => 'Another title for a content-less row',
              ),
            );
            break;
          case 'other-template-slug.php':
            $value = array(
              array(
                'field_58984ddb5b8cc' => 'My title',
                'field_58985045abac0' => 'My content',
              ),
              array(
                'field_58984ddb5b8cc' => 'Another title for a content-less row',
              ),
            );
            break;
        }
      }
      return $value;
    }
    add_filter('acf/load_value/key=key_58c7ee3a37aee', 'my_default_value_subs', 10, 3);
    ?>
    

    Your $value would be different depending on the ‘type’ of subfields, but this should get you pointed in the right direction I hope!

    Let me know,
    Keith

  • Hi Keith,

    Thank you so much for this, the function works.

    How would this be used for CPT? I tried is_singular() and the template name.

    There is a minor issue with the conditional statements, in that working off page ID to grab the template, the page needs to be saved first. Ideally I need these fields to be set before content is added if possible.

    Any ideas how would this be adapted to run the function on template change?

    Rob.

  • Hi Rob,

    To run the above code for only specific post types, you can add this code to the top:

    
    // store the current post type in a variable
    $current_post_type = get_post_type( $post_id );
    
    // you can use an array to add 1 or more post types that you
    // want this function to apply to, like so:
    $allowed_post_types = array( 'post-type-1', 'post-type-2', 'post-type-3' );
    
    // then, say... if 'not' in array, return $value untouched
    if ( !in_array( $current_post_type, $allowed_post_types ) ) {
      return $value;
    }
    

    For the last part of your question…
    … hmmm ….

    You could “disable” certain fields based on certain criteria (like post status, or if a custom field is empty)… then… re-enable them once the criteria is met.

    It’s simple enough to disable regular ACF fields (text, number, etc.) and in fact… sometimes you can use the “rules” for field groups to hide them based on post status.

    Here’s a resource about the acf/prepare_field filter that explains how you might disable ACF fields.

    To disable multiple fields, you can call the filter function multiple times:

    
    add_filter('acf/prepare_field/key=field_1', 'my_acf_prepare_field');
    add_filter('acf/prepare_field/key=field_2', 'my_acf_prepare_field');
    

    Be sure that you run your conditions within the function. Check the value of a different field to determine whether to disable the field or not. Use something like:

    
    if ( get_field( 'mandatory-field-key', get_the_id() ) ) {
      // field has a value, probably don't do anything
    } else {
      // field doesn't have a value, disable the other fields
    }
    

    To disable WordPress standard fields like the Title, you can try something like the code below.

    Note: this uses the CSS ID of the HTML element to “select” it, then “disable” it… you can do the same to target other non-text ACF fields. You can control the ID of all ACF fields (look at the Wrapper Attributes).

    
    /* START: Disables some fields until certain criteria is met */
    function my_set_fields_to_read_only() {
    
      $allowed_post_types = array('projects');
      if ( !in_array( get_post_type(), $post_types ) ) { return; }
      
      // if the important field has a value,
      // we don't need to do anything else
      if ( get_field( 'mandatory-field-key' ) ) { return; }
      
      // JavaScript to disable fields
      echo "<script type='text/javascript'>";
      echo "  ( function ( $ ) {";
      echo "    $( document ).ready( function () {";
      echo "      $('#title').attr('disabled','disabled');";
      echo "      $('#title').css({'background': '#f8f8f8'});";
      echo "    });";
      echo "  }( jQuery ) );";
      echo "</script>";
    
      return;
    }
    add_action( 'edit_form_advanced', 'my_set_fields_to_read_only' );
    /* END: Disables some fields until certain criteria is met */
    

    I know that’s a lot thrown in one reply. I hope that you can pick it apart and are able to move forward with your project! Let me know.

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

You must be logged in to reply to this topic.