How to access areabrick field values inside the areabrick action?

So we are using areabricks to allow a site manager to construct pages themselves. What we’d like to do is access the values stored by their updating the brick values via edit.html.php, and view.html.php through the areabrickname.php file using the public function action( Info $info ) {…} method.

Any ideas?

Was able to achieve this via:

// Loop through the block and pull the values...
$block_name = 'main';
if( !$info->getDocument()->getElement( $block_name )->isEmpty() ) {
	foreach( $info->getDocument()->getElement( $block_name )->getData() as $i => $brick ) {
		if( $i !== $info->getIndex() || $brick[ 'type' ] !== $info->getId() ) continue;
		foreach( $editables as $name => &$value ) {
			$value = $info->getDocument()->getElement( $block_name.':'.$brick[ 'key' ].'.'.$name )->getData();
		}
	}
}

I found an alternative approach using getDocumentTag. Not sure if this example is exactly the same as yours but it uses a relation editable in a brick where the editor can drag in an object.

public function action(Info $info)
{
    $relationEditableName;
    $tag = $this->getDocumentTag($info->getDocument(), 'relation', $relationEditableName);
    $tagConfig = $tag->getData();  // returns array containing id, type and subttype
    if (intval($tagConfig['id']) > 0 && $tagConfig['type'] == 'object') {
        $info->getView()->item = \Pimcore\Model\DataObject\Concrete::getById($tagConfig['id']);
    }
}

It works even with multiple instances of the brick on one page. Hope this might be of use to someone.

1 Like

@alasdair-shields I tried your approach but it didn’t work in $tagConfig, it always returns an array with null id, type, and subtype. But the value is right there in the editmode :’(

Update: It’s because my editable is inside an iterable block, which means the full path to the element(s) is like content:1.block:1.obj.

This is what I use now:

public function action(Info $info) {
    // https://talk.pimcore.org/t/how-to-access-areabrick-field-values-inside-the-areabrick-action/777/3?u=ceefour
    $iterableBlock = $this->getDocumentTag($info->getDocument(), 'block', 'block');
    $pois = [];
    foreach ($iterableBlock->indices as $key) {
        $editablePath = "{$iterableBlock->getName()}:{$key}.obj";
        $element = $info->getDocument()->getElement($editablePath);
        $poiId = $element ? $element->getData()['id'] : null;
        $pois[$key] = $poiId ? Poi::getById($poiId) : null;
    }
    $info->getView()->pois = $pois;
}

Another approach for accessing brick block data without having to manually reconstruct the internal database identifiers for block item names:

Edit File:

<div>
    <strong>Sections:</strong>
    <?php while( $this->block( 'sections' )->loop() ) : ?>
        <div>Title:<br/><?= $this->input( 'title' ) ?></div>
        <div>Content:<br/><?= $this->wysiwyg( 'content' ) ?></div>
    <?php endwhile; ?>
</div>

Brick File:

public function action( Info $info ) {

    // Reusables...
    $view = $info->getView();
    $doc  = $info->getDocument();

    // Retrieve the blocks...
    $sections = [];
    $block = $doc
        ->getElement( 'my_areabrick_name' )
        ->getElement( $info->getId() )[ $info->getIndex() ]
        ->getBlock( 'sections' )->getElements();
    foreach( $block as $item ) {
        $sections[] = [
            'title'   => $item->getInput( 'title' )->getData(),
            'content' => $item->getWysiwyg( 'content' )->getData()
        ];
    }

    // Send data to view...
    $view->sections = $sections;

}

View File:

<div class="my-component">
    <?php foreach( $this->sections as $section ) : ?>
        <?php if( !empty( $section[ 'title' ] ) ) : ?>
            <h2 class="title"><?= $section[ 'title' ] ?></h2>
        <?php endif; ?>
        <?php if( !empty( $section[ 'content' ] ) ) : ?>
            <div class="content"><?= $section[ 'content' ] ?></div>
        <?php endif; ?>
    <?php endforeach; ?>
</div>

Hope whomever sees this finds it useful! Happy coding!

$isNew = $this->getDocumentTag($info->getDocument(), 'checkbox', 'is_new');

where checkbox is the type and s_new the field name