Jump to content

[SOLVED] API for get or find blocks by type or class


gebeer
 Share

Recommended Posts

Hi there,

$page->rockpagebuilder_blocks is of type RockPageBuilder\FieldData which extends PageArray and and can be searched like PageArray by id, title etc.

Is there a way to get/find blocks by there block type (class name)?

Maybe I overlooked something, but couldn't find anything in the docs.

I tried things like $page->rockpagebuilder_blocks->get('type|pageClass=Contact');

Would be a neat feature :-)

 

Link to comment
Share on other sites

3 hours ago, bernhard said:

Would you mind sharing your use case? I've never needed this

Great! Thank you for the quick addition. Very much appreciated.

I have a SPA where I have URL hashes to jump to different section on the site like /#kontakt

In the info() method for each block I added a property id. For the ContactForm block the id is "kontakt"

Now I have link buttons in other blocks that should point to /#kontakt

I could have hard coded that. But I wanted to get the block by class name to get the id, just in case it changes one day

So in my template that holds the blocks I had this

$blocks = $page->rockpagebuilder_blocks;
$contactformBlock = $blocks->get('id=1195');
// add the hash to the page object to make it available further down the call stack in all block views
$page->contactHash = $contactformBlock ? '#' . $contactformBlock->getInfo('id') : '#kontakt';
?>

<main id="content" sortable>
  <?= $rockpagebuilder->render(true) ?>
</main>

With your update I can do $blocks->get('type=ContactForm') which is much safer in my opinion. In case the block gets deleted and recreated, the id would change.

Link to comment
Share on other sites

Ok one thing you have to be careful about is block visibility. What if an editor hides the contact block and add's another visible later on that page. I think your implementation doesn't account for that?

Link to comment
Share on other sites

10 hours ago, bernhard said:

Ok one thing you have to be careful about is block visibility. What if an editor hides the contact block and add's another visible later on that page. I think your implementation doesn't account for that?

Yes, that is correct. I would like to handle this through the block's status, if possible. Since Block inherits from Page that should be possible?

Something like $contactformBlock = $blocks->get('type=ContactForm, status!=unpublished');

But:
When I hide the block, I get $contactForm->status = 1
When I unhide it, I get same status 1.
Looking at the RockPageBuilderBlocks pages in the page tree, they don't seem to have a regular Page status

image.png.39126a8bceb36cbe287f75b65f755ed4.png

So status in Blocks seems to be handled differently from Page status.

Is there already a selector that we could use for finding only unhidden blocks?

Link to comment
Share on other sites

16 hours ago, bernhard said:

Have a look at FieldData::wakeup - it's stored in the _mxhidden prop.

Great hint, thank you.

My logic for getting the block now looks like

/** @var FieldData $blocks */
$blocks = $page->rockpagebuilder_blocks;
/** @var Block $contactformBlock */
$contactformBlock = $blocks->get('type=ContactForm, _mxhidden=0');

This gets only non-hidden blocks which is perfect for my use case.

  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...
  • gebeer changed the title to [SOLVED] API for get or find blocks by type or class

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