Get Named Blocks Content from Page Composer
PermalinkI created a custom PageType (News Entry) from scratch where I added fields for the composer with named entities (e.g. "News Content").
After that I could successfully create standard output using a Composer-Block.
But now I want to be able to get the content of "News Content" programmatically for a given Page.
I can use
$page->getBlocks()
to get all Blocks on a page.
Using some code I found in the Composer Block I also can identify Composer controls with Custum Labels:
$pt = PageTemplate::getByID($page->getPageTemplateID()); $ptt = PageType::getByID($page->getPageTypeID()); $controls = PageTypeComposerOutputControl::getList($ptt, $pt); foreach($controls as $control) { $fls = PageTypeComposerFormLayoutSetControl::getByID($control->getPageTypeComposerFormLayoutSetControlID()); if($fls->getPageTypeComposerFormLayoutSetControlCustomLabel()) { echo $fls->getPageTypeComposerFormLayoutSetControlCustomLabel(); } }
But how can I merge this information to get the content for the control labeled "News Content"?
Thanks for your help!
What version of concrete5 are you using?
Im using version 5.7.3.1
An example, let's say the text area attribute called News Teaser has the handle "news_teaser".
One way to access that attribute value is with getCollectionAttributeValue().
Example:
$c->getCollectionAttributeValue('news_teaser')
...this is not an attribute, but a block from composer. As I understand the content is copied to a standard block when a page is published.
I found the class handling to get the block when the control is known (\Concrete\Core\Page\Type\Composer\Control\BlockControl), but the relevant function ( getPageTypeComposerControlBlockObject(Page $c)) is protected.
Is there any way to extend this class?
// assuming $page contains a valid page object // get page template and type $pt = PageTemplate::getByID($page->getPageTemplateID()); $ptt = PageType::getByID($page->getPageTypeID()); // get all contrrols $controls = PageTypeComposerOutputControl::getList($ptt, $pt); foreach($controls as $control) { $fls = PageTypeComposerFormLayoutSetControl::getByID($control->getPageTypeComposerFormLayoutSetControlID()); // check if control has custom label if($fls->getPageTypeComposerFormLayoutSetControlCustomLabel() == "News Content") { $bc = $fls->getPageTypeComposerControlObject(); $bc->setPageTypeComposerFormLayoutSetControlObject($fls); // invoke protected function using reflection... $r = new ReflectionMethod('Concrete\Core\Page\Type\Composer\Control\BlockControl', 'getPageTypeComposerControlBlockObject');
Maybe there is a more elegant way, but for the moment it works.
How can I achieve this?