Jump to content

Profields Repeater Matrix - limiting one matrix type within a repeater to the first entry


hollyvalero
 Share

Recommended Posts

I have a pro fields repeater field that I am using for page top alerts on a home page (thank you, COVID-19) ... this dedicated set of repeaters could have more than one type. 

--> repeater field is called cblock_alerts
--> the first matrix type is alert 

I have limited the entry to 2 in the back end (assuming one live and one in reserve) but I only want ONE to show up so you don't get stacked alerts.

Normally I repeat everything in my repeaters so the code looks like:

if ($page->cblock_alerts) {
    foreach ($page->cblock_alerts as $item) {
        if ($item->type == 'alert') {
            echo "

I've tried several find, get, limit, and they all either don't work, show nothing, or show both repeaters. I don't really need the foreach here, but there could be another matrix item so the item type == alert is needed.

I'm looking for something like this:

if ($page->cblock_alerts) {
    foreach ($page->cblock_alerts as $item) {
        if ($item->type == 'alert') {
            $firstalert = $item->first();
            echo "

 

or            

$firstalert = $item('limit = 1');

Lots of variations on these with no luck.  Tried random... grrrr.

 

 

Link to comment
Share on other sites

Do you just want to get the first item? This seems to do the job for me:

$alert = $page->cblock_alerts->findOne("type=alert");
if ($alert) {
	echo "Notice: {$alert}";
}

 

Link to comment
Share on other sites

15 minutes ago, Craig said:

Do you just want to get the first item? This seems to do the job for me:


$alert = $page->cblock_alerts->findOne("type=alert");
if ($alert) {
	echo "Notice: {$alert}";
}

 

Thanks, Craig!  That's exactly what I needed. ?

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...