Support

Account

Home Forums ACF PRO Is it possible to have 2 flexible field groups, but they work like one

Solving

Is it possible to have 2 flexible field groups, but they work like one

  • My problem is that i’d need to separate 2 flexible fields but they need to work as one (for example: Main modules and Custom modules and have the ability to drag & drop the order of them).

    The reason for this is that these will be used as content modules for the webpage but the Main modules will be the same on each site. But the Custom modules will be site specific.

    The idea is to be able to update the Main modules (add new ones, do improvements to the old ones etc.) and be able to just copy over the php templates to those sites and then delete/import the new updates custom fields for the Main modules. The custom modules, fields,php would not be updated to other sites.

    Is this something that’s possible? Or should it be done differently? Any better ideas to do this? 🙂

    Are there any better/cleaner ways to import/export or update things between sites?

    Thanks

  • I tried couple of things, found this plugin https://github.com/mvpdesign/acf-reusable-field-group which in a way would be possible to make it but it doesn’t seem to work (attached the fields structure).

    and here is the code i’m running through the template:

    
    <?php
    
    // check if the repeater field has rows of data
    if( have_rows('modules_all') ):
    
        // loop through the rows of data
        while ( have_rows('modules_all') ) : the_row(); ?>
    
        <?php
        $checkboxcustom = get_sub_field('is_custom_modules');
        if($checkboxcustom) { // is yes ?>
        CUSTOM
                        <?php
                        if( have_rows('modules_custom') ):
    
                            while ( have_rows('modules_custom') ) : the_row(); ?>
    
                              <?php  if( get_row_layout() == 'module_sample' ): ?>
    
                                <?php get_template_part('templates/modules-custom/module', 'sample') ?>
    
                              <?php endif; ?>
    
                          <?php endwhile; ?>
    
                         <?php endif; ?>
    
        <?php } else { ?>
        MAIN
                        <?php
                        if( have_rows('modules_main') ):
    
                            while ( have_rows('modules_main') ) : the_row(); ?>
    
                              <?php  if( get_row_layout() == 'module_main' ): ?>
    
                                <?php get_template_part('templates/modules-main/module', 'main') ?>
    
                              <?php endif; ?>
    
                          <?php endwhile; ?>
    
                         <?php endif; ?>
    
        <?php } ?>
    
       <?php endwhile;
    
    else :
    
    endif;
    
    ?>
    

    IT seems to be working nicely for the Main fields but not the Custom one, as they are not beeing displayed.

  • Hi,

    First, when you use a ACF Reusable Field, you have to know that you have one more imbrication in your template, so you have to do a loop with the Reusable Field to get its childs fields.

    Secondly, I don’t see where you call your ACF Reusable Field in your template, you have to call it : modules_main_reusable.

    Third, I encourage you to use this fork of the plugin if you want to have multiples Reusable field of an ACF Group Field in a flexible field.
    https://github.com/mvpdesign/acf-reusable-field-group

    Hope it helps.

  • @floflo91 thanks for the help!

    I used the plugin you mentioned and the logic seems fine on the backend etc.

    A Repeater field (so i can mix and match the flexible content)
    A.1 Checkbox to choose to have flexible content A or B
    A.2 Reusable field of Flexible content A (core modules for use on other websites)
    A.3 Reusable field of Flexible content B (specific custom modules used for specific website).

    The frontent/template part is a bit tricky.

    Repeater field
    – checkbox
    — Reusable Field group (ex. modules_main_reusable)
    — Flexible content field (ex. modules_main)
    —- Field types for the flexible layout

    How should i call the modules_main_reusable field type? like this:

    if( have_rows('modules_main_reusable') ):
    while ( have_rows('modules_main_reusable') ) : the_row();

    And then add the flexible repeater code inside it?

    
                        <?php
                        if( have_rows('modules_main') ):
    					while ( have_rows('modules_main') ) : the_row(); ?>
                              <?php  if( get_row_layout() == 'module_sample' ): ?>
                                <?php get_template_part('templates/modules-main/module', 'sample') ?>
                              <?php endif; ?>
                         <?php endwhile; endif; ?>
    
  • Yes that’s it.

    But maybe it’s not the right thing to wrap a flexible field on a Reusable Field I guess.

    If it doesn’t work, try to wrap the reusable into a flexible field in your repeater field.

  • @floflo91 hm.. couldn’t make it to work.

    I also started from scratch and made a field group with just 2 text fields.

    Made a second field group with the reusable group and called the first one.

    Made 2 tests in the template:

    Test A
    <?php the_field('reusable_field_group_for_text'); ?>
    
    Test B
    <?php
    if( have_rows('reusable_field_group_for_text') ):
    while ( have_rows('reusable_field_group_for_text') ) : the_row();
    echo "TEST";
    endwhile; endif;
    ?>

    TEST A output was and array of both text fields, although i would want to call them separately.
    TEST B code didn’t work so maybe i just don’t know how to call the Reusable field type and it’s contents correctly :/ Even without any repeater/flexible.

    Too bad the plugin docs don’t have any usage code for it.

  • Test with get_sub_field function

    if (get_sub_field('reusable_field_group_for_text')) {
    	foreach (get_sub_field('reusable_field_group_for_text') as $p) {
    		echo $p;
    		the_sub_field('subfieldName');
    		the_sub_field('subFieldName2');
    	}
    }
  • @floflo91 thanks for the help! That one works well for the normal fields inside the Reusable field but not for a flexible field inside a Reusable field.

    i tried to use print_r
    <?php print_r(get_sub_field('modules_custom_reusable')); ?>
    which outputted:

    Array ( [modules_custom] => Array ( [0] => Array ( [acf_fc_layout] => module_sample [content_title] => text field inside flexible ) ) [content_title] => text field outside flexible )

    So it is picking up the content of the flexible field i just don’t know how to pull them out separately and apply the layout fields. :/ Will try it more.

    Here are the templates for the normal fields in case anyone will need them.

    Inside a repeater:

          <?php if( have_rows('repeater') ): ?>
    
              <?php while( have_rows('repeater') ): the_row(); ?>
    
                  <?php
                  if (get_sub_field('reusable_field')) {
                    foreach (get_sub_field('reusable_field') as $p) {
                      echo $p;
                      the_sub_field('subfieldName');
                      the_sub_field('subFieldName2');
                    }
                  } ?>
    
              <?php endwhile; ?>
    
          <?php endif; ?>

    inside a flexible field type:

    <?php
    
    if( have_rows('flexible') ):
    
        while ( have_rows('flexible') ) : the_row();
    
            if( get_row_layout() == 'layout_flexible' ): ?>
    
                  <?php
                  if (get_sub_field('reusable_field_group_')) {
                    foreach (get_sub_field('reusable_field_group') as $p) {
                      echo $p;
                      the_sub_field('subfieldName');
                      echo "<br>";
                      the_sub_field('subFieldName2');
                    }
                  } ?>
    
           <?php elseif( get_row_layout() == 'other' ):
    
            endif;
    
        endwhile;
    
    else :
    
    endif;
    
    ?>
  • Hello,

    You said your Flexible field is in a Reusable Field, so I don’t understand why you invert both in your template.

    First call your Reusable field and then call your flexible field inside

  • Ah, sorry, must have explained it bad. I made a PDF with the logic/concept behind it.

    Hope it helps 🙂 If there is a better/easier way to do this, that would be great.

  • Something like that no ?

    <?php if( have_rows('repeater') ): ?>
    
       <?php while( have_rows('repeater') ): the_row(); ?>
    
          <?php
          if (get_sub_field('reusable_field')) {
            foreach (get_sub_field('reusable_field') as $p) {
               if( have_rows('flexible') ):
    
                  while ( have_rows('flexible') ) : the_row();
    
                      if( get_row_layout() == 'layout_flexible' ):
    
                              foreach (get_sub_field('subfields_of_layout_flexible') as $p) {
                                echo $p;
                                the_sub_field('subfieldName');
                                echo "<br>";
                                the_sub_field('subFieldName2');
                              }
                              ?>
    
                     <?php elseif( get_row_layout() == 'other' ):
    
                      endif;
    
                  endwhile;
    
              else :
    
              endif;
    
            }
          } ?>
    
       <?php endwhile; ?>
    
    <?php endif; ?>
  • @floflo91 thanks, i thought so too and had quite a few similar tries yesterday, but it seems it doesn’t want to pick up the if( have_rows(‘flexible’) ):

    added the custom fields export/template.php i’m using with your example.

    The logic is there, it just doesn’t pick it up :/

  • Tried to get this working again and i seem to get it. It’s an array inside and array so i used this code to pull out the data (i used the ZIP file i have above).

     
    <?php if( have_rows('modules_all') ): 
      while( have_rows('modules_all') ): the_row();
    
              $checkbox = get_sub_field('is_custom_modules');
              if($checkbox) { // is yes use custom modules ?>
    
                   <?php
    
                    //print_r(get_sub_field('modules_custom_reusable'));
                    $contentArray = get_sub_field('modules_custom_reusable');
                    
                    $contentTitle = $contentArray['modules_custom'][0];
                    $contentTitle = print_r($contentTitle['content_title']); 
    
                    ?>
    
                    <?php $contentTitle; ?>
    

    You can uncomment the first line to see how the array looks like or use var_dump instead print_r.

    It may not be the perfect solution as i’m not a real PHPer but it seems to be working/taking me further.

  • This weekend I started looking at the available plugins for reusable field groups. Testing and research turned into an entire weekend and at the end I had created my own version of a reusable field group field.

    It works a bit differently than the others, it works by rebuilding field groups that include a reusable field group field ans local field groups which override the original field groups. This only works in ACF5, ACF4 does not support acf_local().

    I have not completely tested it and would be interested in feedback or bug reports. I’ll also answer question. There’s not documentation yet, mostly because I think that the instructions when creating the field are pretty self explanatory.

    https://github.com/Hube2/acf-reusable-field-group-field

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

The topic ‘Is it possible to have 2 flexible field groups, but they work like one’ is closed to new replies.