beaverbuilder / feature-requests

Feature request board for Beaver Builder products.
26 stars 4 forks source link

ACF Repeater field shortcode: array index #211

Open intelliweb opened 2 years ago

intelliweb commented 2 years ago

Is your feature request related to a problem? Please describe. When using BB's ACF repeater shortcodes, there is no way to get a unique identifier (e.g. index) for the current item in the repeater loop. For things like sliders, this is usually required.

Example use case: Using the BB Theme and BB Themer's ACF repeater field in an HTML module it is pretty easy to add the HTML to create a Bootstrap carousel. See: https://getbootstrap.com/docs/4.6/components/carousel/ But you need the array index of the item in the repeater loop.

Describe the solution you'd like A new shortcode added that can be used within an ACF repeater shortcode to retrieve the current array index of the item in the repeater loop. Something like:

[wpbb post:acf type='repeater_index']

And also have it work with if/else. Something like:

[wpbb-if post:acf type='repeater_index' exp='equals' value='0']
   This text displays when the index is 0 (first item in repeater field).
[wpbb-else]
   This text displays when the index is not 0 (all items except first in repeater field).
[/wpbb-if]

Describe alternatives you've considered Creating a custom shortcode instead of using BB's ACF repeater shortcodes is really the only alternative.

codente commented 2 years ago

Not sure I understand the use case. Are you referring to being able to pull out a specific row of the repeater?

If so, you can already do this

[wpbb post:acf name='project_2_project_title']

Where project is the repeater name

Where 2 is the row (Remember rows start at 0, not 1)

Where project_title is the sub-field of the repeater you want to display

intelliweb commented 2 years ago

@codente Not exactly. Although that is handy to know. Any chance that can be added to the documentation? If it's in there, I can't find it.

What I'm referring to is being able to pull the value of the index (0, 1, 2, 3). So that in HTML you could do something like:

<li data-target="#carouselExampleIndicators" data-slide-to="[wpbb post:acf type='repeater_index']"></li>

Where the shortcode outputs the index value (e.g. "3").

And you could also do something like:

[wpbb-if post:acf type='repeater_index' exp='equals' value='0']
   <li data-target="#carouselExampleIndicators" data-slide-to="[wpbb post:acf type='repeater_index']" class="active"></li>
[wpbb-else]
   <li data-target="#carouselExampleIndicators" data-slide-to="[wpbb post:acf type='repeater_index']"></li>
[/wpbb-if]

This is handy when you need a unique identifier for each row of the repeater in your HTML for things like carousels (see HTML for Bootstrap carousels in my link above), accordions, etc.

Pross commented 2 years ago

Looks like ACF already have a function for this: https://www.advancedcustomfields.com/resources/get_row_index/

intelliweb commented 2 years ago

@Pross Yes, it does. How does that help within the ACF Repeater shortcode?

zackpyle commented 1 year ago

@intelliweb I agree. A shortcode like [wpbb-acf-repeater-index] would be great! I also want to use it with Bootstrap components. Did anything ever come of this request?

zackpyle commented 1 year ago

@intelliweb I have submitted this to BB support, so let's see what comes of it.

I’m no php expert (by a long shot!), but this is what I have come up with that will add the [wpbb-acf-repeater-index] shortcode functionality to the [wpbb-acf-repeater] syntax.

https://www.diffchecker.com/6lRX4ADY/

Anyone who is better at coding than I, please let me know what you think!

Pross commented 1 year ago

This was fun actually. First thanks Zack for providing some code! Turns out I didnt use any of it as I wanted to get both these things working.

So here is my test shortcodes

[wpbb-acf-repeater name='repeater-test']
  <p>[wpbb post:acf type='text' name='meh'] : [wpbb post:acf type='repeater_index']</p><p>
  [wpbb-if post:acf type='repeater_index' exp='equals' value='1']
   This text displays when the index is 1 (first item in repeater field).
[wpbb-else]
   This text displays when the index is not 1 (all items except first in repeater field).
[/wpbb-if]
</p>
[/wpbb-acf-repeater]

As you can see im using 1 as the first item, this is how ACF does it, I believe there is a filter for ACF to make it use 0 as the first item. Heres a pic of the output

Screenshot 2023-02-23 at 18 26 40

The patch to make this work is actually very simple!

diff --git a/extensions/acf/classes/class-fl-page-data-acf.php b/extensions/acf/classes/class-fl-page-data-acf.php
index d16358be..83e194bc 100644
--- a/extensions/acf/classes/class-fl-page-data-acf.php
+++ b/extensions/acf/classes/class-fl-page-data-acf.php
@@ -42,6 +42,12 @@ static public function string_field( $settings, $property ) {
            $object = get_field_object( $name, self::get_object_id( $property ) );
        }

+       if ( ! $name && isset( $settings->type ) && 'repeater_index' === $settings->type ) {
+           $object = array(
+               'type' => 'repeater_index',
+           );
+       }
+
        if ( empty( $object ) || ! isset( $object['type'] ) ) {
            return $content;
        }
@@ -245,6 +251,9 @@ static public function string_field( $settings, $property ) {
                    $content = strval( $value );
                }
                break;
+           case 'repeater_index':
+               $content = (string) get_row_index();
+               break;
            default:
                $content = '';
        }// End switch().
zackpyle commented 1 year ago

@Pross Cool. I like it! Yeah, when I made mine, I wasn't thinking about @intelliweb's request for using it conditionally 😎

Glad it was helpful (even if it wasn't in the end haha)

Pross commented 1 year ago

Let me know if you test it and i'll update the PR with any feedback

zackpyle commented 1 year ago

Tested. Works perfectly πŸ™ŒπŸ»

zackpyle commented 1 year ago

@Pross Do you think this will be out in 1.4.6?

Pross commented 1 year ago

@Pross Do you think this will be out in 1.4.6?

Possibly, it isnt milestoned yet but I dont see why not.

zackpyle commented 3 months ago

@codente following up here - is this still slated for Themer 1.5?

zackpyle commented 2 weeks ago

@codente Looks like this missed v1.5-dev.1?