Jump to content

Last RepeaterMatrix row of certain 'type'


a-ok
 Share

Recommended Posts

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') {
		//
	}
}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
}

 

  • Like 3
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...