Skip to main content
Version: 2023.3

Blocks

The block data type acts as a simple container for other data fields. Similar to a field collection, an unlimited number of block elements can be created.

A block element can be placed into a localized field but can also contain a localized field itself. Nesting is not possible.

Block data typeBlock data typeBlock data type

Block, edit peviewBlock, edit peviewBlock, edit peview

The block data basically just gets serialized into a single database column. As a consequence, this container type is not suitable, if you are planning to query the data.

API Usage

Let us consider the following class definition

Class DefinitionClass DefinitionClass Definition

and object data

Class DefinitionClass DefinitionClass Definition

The value of the second input field can be retrieved as follows:

        $object = DataObject\BlockClass::getById(48);
$blockItems = $object->getBlockElement1();
$firstBlockItem = $blockItems[0];
echo($firstBlockItem["input2"]->getData());

The output will be "value2".

The value can be updated in a similar way.

        // ... same as above ...
$firstBlockItem["input2"]->setData(time());
$object->save();

Get Values of Localized Block Entries:

        $object = DataObject\BlockClass::getById(48);
$blockItems = $object->getBlockElement1();
$firstBlockItem = $blockItems[0];
$localizedfields = $firstBlockItem["localizedfields"]->getData();
// after that use it as you would to it with `Pimcore\Model\DataObject\LocalizedField::getLocalizedValue`

Create a Block:

/** 
* @var DataObject\Data\BlockElement $blockElement
*
* BlockElement( name, type, data )
*/
$object = DataObject::getById(4);

$data = [
"input1" => new BlockElement('input1', 'input', 'NewValue1'),
"input2" => new BlockElement('input2', 'input', 'NewValue2'),
"myhref" => new BlockElement('myhref', 'manyToManyRelation', [$object])
];

$blockElement = new BlockClass();
$blockElement->setBlockElement1([$data]);

Create a Block with localized fields:

$object = DataObject::getById(4);

$data = [
"input1" => new BlockElement('input1', 'input', 'NewValue1'),
"input2" => new BlockElement('input2', 'input', 'NewValue2'),
"myhref" => new BlockElement('myhref', 'manyToManyRelation', [$object]),
"localizedfields" => new BlockElement('localizedfields', 'localizedfields', new Localizedfield([
"de" => [
"localizedInput" => "Mein deutscher Text"
],
"en" => [
"localizedInput" => "My English text"
]
]))
];

$object->setMyBlockField([$data]);