How do you display a block from another page... that pulls attributes of it's parent?1 user found helpful
I just posted this as a response to another thread (http://www.concrete5.org/community/forums/customizing_c5/need-advic... ), but thought it might be better to post it as its own topic, too.
In a nutshell... I have a custom Page Type ("Press Room Item"). On that custom page type, I am creating a single block... one of three possible ("Press Room Video", "Press Room Document" or "Press Room Link"). In each block's view.php, it is creating the layout I need for displaying these items. Including grabbing the block's parent page attributes of "Name" and "Public Date".
I then have a custom template for the Page List block that is pulling the block from the "Main" area of my custom "Press Room Item" pages and displaying them in tabular format.
THE PROBLEM: when the custom block is being displayed on the page with the page_list block, it is grabbing THAT page's Name and Public Date attributes... instead of those of the BLOCK'S parent page.
Since I am intertwining the attributes (the block has a text field to get a URL which is then wrapped around the page's title for the link), I don't see how I can do this in the page_list's custom template.
Thoughts? Help! :D
It's to use the "getBlockCollectionObject()" function.
So, in the block's view.php file, I used the following to set the $title attribute to the name of the page on which the block was originally inserted:
$title = $controller->getBlockCollectionObject()->getCollectionName();
And that did it! Now, when the area/block is pulled into the Page_List block on a different page, the block still displays the name of the page on which it was originally inserted.
HOORAY! (Thanks, again, Jordan!)
Even though I had the above working, I found that it was slow. Loading up the Press Room page took longer than I wanted... and clicking through the pagination seemed to have the same delay. I'm not sure, but I'm guessing it was because of a double-whammy of loading up pages: the page_list block on the Press Room page loads up all of the "Press Room Item" children pages, loops through them to grab their content... but that content had to reach back to its parent page again in order to get the page title and public date to display as part of the content.
I had set it up this way because I needed to use the "Press Room Item" page's Page Name attribute as the text that was wrapped by an anchor tag using a URL I was entering in a field in the custom block placed ONTO the "Press Room Item" page... and in the page_list block, I didn't know of any way to access those block field values... so I had to have the BLOCK's view.php file do the layout.
It worked... and it got the site live... but it was slow. And turning on page caching didn't seem to help.
So... in poking around in some other block code, I discovered that I could re-code this in a way that would probably (and, spoiler alert, DID) improve the speed of the Press Room page.
Here's what I did...
I left the block's view.php the same. This way, when viewing any of the "Press Room Item" pages directly (no site visitors will see this, but site editors will), the content displays in the same format as it does in the Press Room page listing.
I DID, however, change the code in the page_list block template. Instead of just retrieving the entirety of the child pages' content and just re-displaying it here, I retrieve all of the individual elements I need from the child page and its embedded block (the page is limited to one in the Main area) and build the layout that I need dynamically.
The trick to being able to do this was this code:
$bt = $block->btHandle; $btc = $block->getInstance(); $link = $btc->field_1_textbox_text; $source = $btc->field_2_textbox_text;
$bt = $block->btHandle;
First, I grab the handle for the block (that I have already gotten earlier in the code).
$btc = $block->getInstance();
Now I get the block's controller.
$link = $btc->field_1_textbox_text;
$source = $btc->field_2_textbox_text;
These two are the "magic" (LOL!). Each of these retrieves the value from the fields in my custom block (created using Jordan Lev's life-saving Designer Content add-on... THANKS JORDAN!) and sets the result into a variable that I can use later in the page_list template where I'm generating the HTML for the page.
Obviously, this isn't the complete code (I can post it if anyone's interested), but you get the idea.
The result is that my Press Room page now loads much faster. The content is displayed, created and edited exactly the same. And my client is MUCH happier. WIN-WIN-WIN! ;D