a-ok Posted January 10, 2018 Posted January 10, 2018 Bit of an odd one here. I'm using a RepeaterMatrix which has various 'types' (text, image, video etc) and on the last iteration of one of the types (text) I want to add an element (for example, a share element... but this has to come after the last text type... not the last RepeaterMatrix row). I'm struggling a bit on how to achieve this? Is there a way to count the total 'text' types then create a PHP count and if the two match then it'll show? foreach ($page->flexible_content as $content) { if ($content->type === 'text') { // } elseif ($content->type === 'images') { // } }
dragan Posted January 10, 2018 Posted January 10, 2018 You mean adding something in the frontend template? Did you try a count() ? You could use a good old counter var and if/else inside your text repeater loop.
a-ok Posted January 10, 2018 Author Posted January 10, 2018 6 minutes ago, dragan said: You mean adding something in the frontend template? Did you try a count() ? You could use a good old counter var and if/else inside your text repeater loop. Yep, sorry. Front end template. I could use a count() but it's getting a count of the repeater matrix's type that's 'text' only.
louisstephens Posted January 10, 2018 Posted January 10, 2018 A quick/dirty method would be to add a class of "something-$counter" and use display none except on the last element with css. $counter = 0; foreach ($page->flexible_content as $content) { if ($content->type === 'text') { <div class="something-$counter"> //share button </div> $counter++; } elseif ($content->type === 'images') { // } } Sorry, it is really rough.
dragan Posted January 10, 2018 Posted January 10, 2018 If I understood correctly, @oma wants to add something after the last matrix item of type x. That would be rather tedious with JS. I tried the following in a test-setup (albeit I used richtext as example type, not text): foreach ($page->matrix as $matrix) { $matrixTypes[$matrix->id] = $matrix->type; } $filtered = array_filter($matrixTypes, function ($var) { return ($var == 'richtext'); }); $lastElementKey = array_pop(array_keys($filtered)); Now you can do an if/else with the ID you have from $lastElementKey.
BitPoet Posted January 10, 2018 Posted January 10, 2018 I don't have repeater matrix here to test, but shouldn't this work (all PW API)? $lastTextItem = $page->flexible_content->find("type=text")->last(); foreach($page->flexible_content as $content) { //... do whatever if($content == $lastTextItem) //... do something only after last text } 3
a-ok Posted January 16, 2018 Author Posted January 16, 2018 On 10/01/2018 at 6:40 PM, BitPoet said: I don't have repeater matrix here to test, but shouldn't this work (all PW API)? $lastTextItem = $page->flexible_content->find("type=text")->last(); foreach($page->flexible_content as $content) { //... do whatever if($content == $lastTextItem) //... do something only after last text } Apologies for the late reply. Yes this is perfect... great to use just the PW API. Thanks @BitPoet
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now