Skip to content

Adding a repeater Accordion module

This article has been updated! Please check the addendum at the bottom.

Go to the addendum

Ever wondered if it was possible to add an ACF Repeater as the source for – for example – an Accordion module? Well.. you can. Here’s how:

There are a few things you need to do in preparation:

  1. Set up the a repeater-field with it’s subfields on the page, post or CPT
  2. Add a connections parameter to the Accordion
  3. Create a filter for the Toolbox Connector, specifically for the repeater

Step 1: set up the repeater-field

Add a Repeater field to your page, post or CPT. When you add the subfields, make sure they have a name that matches the fieldsnames used by the Accordion Module. In this case they should be ‘label’ and ‘content’. Don’t get confused by the “Label” for the Sub Fields, which could by whatever you want. make sure to save it.

Step 2: Add connections parameter

make sure you activated the ‘extra connection for the modules’ on your Toolbox Settings page in the dashboard. If you do, the connections option will be activated by the plugin.

Step 3: Create filter

This is the most interesting part of course. With just a tiny amount of code you can tell the plugin to return the subfields the way Beaver Builder expects it.

<?php

add_filter( 'toolbox/helpers/get_acf_field/type=repeater'       ,       'get_repeater_as_objects' 		, 90, 5 );

/**
 * Get the repeater value as objects, not array
 * @param  [type] $string       [description]
 * @param  [type] $field_object [description]
 * @param  [type] $value        [description]
 * @param  [type] $atts         [description]
 * @param  [type] $postid       [description]
 * @return [type]               [description]
 */
function get_repeater_as_objects( $string , $field_object , $value , $atts , $postid ) {
		// return as array
		if ( $string ) return $string;

		// check if it's an array
		if(is_array($value)) {
			// loop over the array
			foreach( $value as &$sub_item){
				// type juggle the sub_item to an object
				$items[] = (object) $sub_item;
			}
		// return the items
		return $items;
		}
	// fallback to returning whatever was passed in
	// just in case anything changes to the code
	return $string;
}

And that’s it! This filter gets a very high priority (90 so that it runs late) so that any other filter can return a value if that’s what we want and we escape early. So now, if for some reason you DON’T select a subfield in the repeater-connector you can just pass on the whole repeater as objects, Which the Accordion Module uses as the items-list.

Just look at the video to see what that may look like.

Update

Toolbox has added the filter mentioned in step 3 to core. This means you don’t need to paste that code into your functions.php.

The repeater field has 3 options in it’s original form (remember, you can add your own):

  1. You’ve entered ‘something’ in the Twig template field. It will ignore everything else and process it. Even if you just entered a single character space, it is a viable return value and will be returned as a value. Timber Libraby-plugin needs to be activated for this.
  2. Twig template field is completely empty and you entered something into the field called ‘subfield’. If you want it to return a subfield called ‘content’, and the first iteration of that repeater, you enter ‘content[0]’ because it is zero-based.
  3. Both template field and subfield are empty. It will return the repeater field as an object.

 

Posted in , ,

Beaverplugins

Web ninja with PHP/CSS/JS and Wordpress skills. Also stand-in server administrator, father of four kids and husband to a beautiful wife. Always spends too much time figuring out ways to do simple things even quicker. So that you can benefit.

Leave a Comment