Jump to content

[solved] Rock page builder & page references


Recommended Posts

While using the page reference from vanilla processwire with Rock page builder, I have a hard time finding the right syntax.

$link2 = $pages->find("page_reference_01=$page, sort=parent.sort, sort=sort");
 

Normally, I would use a line like this. But this does not quite work, as the page references are on a page in a block(on another page), while using this, I sometimes could target $block instead of $page, but it does not work this time.

What are the right way to find a page reference within an other page within a block.

$link2 = $pages->blocks->find("page_reference_01=$page, sort=parent.sort, sort=sort");
 
Edited by Atlasfreeman
Link to comment
Share on other sites

Hey @Atlasfreeman thx for your question.

As you already mentioned correctly if the page reference field is in a block, when using a selector with that page reference field, you will get the $block and not the $page where the $block lives on.

Let's say we have a block "Authors" and there we have a page reference field to select an author.

We can get all these blocks like this:

$blocks = $pages->find('my_author_field=123');

Then you can loop over these blocks and get the related $page:

echo "<p>Blog Posts by this Author:</p>";
foreach($blocks as $block) {
	$page = $block->getBlockPage();

	// maybe add some additional checks here, eg check if $page is published etc.

	echo "<div><a href='{$page->url}'>{$page->title}</a></div>";
}

That should be all you need.

If that is not performant enough because you have to query a lot of pages let me know and I'll show you a more advanced technique. Also it might be necessary to add something like "include=hidden" or "include=all" to the selector of the blocks, I'm not sure.

If you need any more help with that let me know. Otherwise please share the working example once you are done and mark the topic [solved] thx 🙂 

Link to comment
Share on other sites

Ok while reading this I think it's worth sharing the other technique as well, as this might be even easier...

The trick is to add a hidden field to your $page, like "post_author". That could be a page reference field.

Then you add a hook on "Pages::saved" and you populate that field based on the $block's selected author.

Then you can use a selector as usual:

$posts = $pages->find("template=blogpost, post_author=123");

This has the benefit that you can use efficient queries and sorting etc. and you can rely on PW's find() for only finding published pages and such.

Link to comment
Share on other sites

I have gone with the second solution. And this does work. But does require using the backend. But that's okay as you can do this when creating the page in the first place.

Thanks again okay, and one last embarrassing question how do I change the header to [solved] in this forum.?

<?php namespace ProcessWire;
use RockPageBuilderBlock\ArchiveFilter;
/** @var Page $page */
/** @var ArchiveFilter $block */
/**
* By default rockpagebuilder will create a *.view.php file on block creation
* if you want to create *.latte files instead go to your module's settings page
* or set the "createView" property to "latte" in your config.php:
* $config->rockpagebuilder = [
* "createView" => "latte",
* ];
*
* If you set it to "php" this message will be hidden on new .php view files.
*/
?>
<section class="rpb-archivefilter" <?= $block->styles() ?> <?= alfred($block) ?>>
<?php
echo "<div class='archive_wrapper'>";
$link = $pages->find("page_reference_02=$page, sort=parent.sort, sort=sort");
 
foreach ($link as $content){
echo "<div class='content_link'>";
echo "<div class='content_link_wrap'>";
 
$block_img = $content->cover_img;
if ($block_img) {
echo "<a href='{$content->url}'><img class='maxheight' src='{$block_img->url}' alt='{$block_img->description}'></a>";
}
 
echo "<a class='title' href='{$content->url}'><h2 class='content_text'>{$content->title}</h2></a>";
 
echo "</div>";
echo "</div>";
 
}
echo "</div>";
?>
</section>
 
Link to comment
Share on other sites

1 minute ago, Atlasfreeman said:

I have gone with the second solution. And this does work. But does require using the backend. But that's okay as you can do this when creating the page in the first place.

Do you mean that the hidden field only get's updated when the page is saved in the backend and not when the block is edited via frontend editing?

This is correct but can be optimised so that you don't even need a backend save. Are you using custom page classes for the page that the ArchiveFilter block lives on?

Link to comment
Share on other sites

On 10/9/2024 at 2:04 PM, bernhard said:

Are you using custom page classes for the page that the ArchiveFilter block lives on?

I don't really understand what you are talking about or what this page does.

But i searched and found this one i this what you talked about?

 

Screenshot 2024-10-14 at 09.03.51.png

Link to comment
Share on other sites

  • Atlasfreeman changed the title to [solved] Rock page builder & page references

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

×
×
  • Create New...