Get Named Blocks Content from Page Composer

Permalink
Hi!

I 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!

View Replies: View Best Answer
maxx2097 replied on at Permalink Reply 1 Attachment
Or, to keep it simple, I want to get the value of e.g. "News Teaser" as shown in the composer (see attachment) programmatically for a given page object.

How can I achieve this?
MrKDilkington replied on at Permalink Reply
MrKDilkington
Hi maxx2097,

What version of concrete5 are you using?
maxx2097 replied on at Permalink Reply
Hi!

Im using version 5.7.3.1
MrKDilkington replied on at Permalink Reply
MrKDilkington
I believe I know what you are asking.

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')
maxx2097 replied on at Permalink Reply
Unfortunatly its not that simple...

...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?
maxx2097 replied on at Permalink Best Answer Reply
OK... as I could not figure out how to extend the BlockControl class, I managed to succeed using reflection to call the protected method:



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