/** * Transfer the supplied blocks to the specified content entity. * * @param EntityLayoutInterface $entity_layout * The entity layout the entity layout blocks belongs to. * @param ContentEntityInterface $content_entity * The content entity to transfer blocks to. * @param BlockPluginCollection $block_collection * The blocks to transfer. * * @return EntityLayoutBlockInterface[] */ public function transferBlocks(EntityLayoutInterface $entity_layout, ContentEntityInterface $content_entity, BlockPluginCollection $block_collection) { $block_entities = []; // Create a entity layout block entity from each default block // configuration and create a new UUID. /** @var BlockPluginInterface $block */ foreach ($block_collection as $block) { $configuration = $block->getConfiguration(); $block_entity = EntityLayoutBlock::create(['layout' => $entity_layout->id(), 'config' => $configuration, 'entity_id' => $content_entity, 'entity_type' => $content_entity->getEntityTypeId()]); $block_entities[] = $block_entity; } /** @var ContentEntityInterface $block_entity */ foreach ($block_entities as $block_entity) { $block_entity->save(); } return $block_entities; }
/** * Create a new entity layout block. * * @param EntityLayoutInterface $entity_layout * The entity layout object to create a block for. * @param ContentEntityInterface $content_entity * The content entity to associate the block with. * @param array $configuration * The block configuration. * * @return EntityLayoutBlockInterface|static */ public function createContentBlock(EntityLayoutInterface $entity_layout, ContentEntityInterface $content_entity, array $configuration) { $entity_layout_block = EntityLayoutBlock::create(['layout' => $entity_layout->id(), 'config' => $configuration, 'entity_id' => $content_entity, 'entity_type' => $content_entity->getEntityTypeId()]); return $entity_layout_block; }