public function FixedToContentMappingHandler::setBlockContent in Fixed Block Content 8
Links a fixed block to a block content.
Existing block content will be released if present.
Parameters
\Drupal\fixed_block_content\FixedBlockContentInterface $fixed_block: The fixed block.
\Drupal\block_content\BlockContentInterface $block_content: The block content.
Overrides FixedToContentMappingHandlerInterface::setBlockContent
File
- src/
FixedToContentMappingHandler.php, line 70
Class
- FixedToContentMappingHandler
- Fixed block to block content mapping entity handler.
Namespace
Drupal\fixed_block_contentCode
public function setBlockContent(FixedBlockContentInterface $fixed_block, BlockContentInterface $block_content) {
// Bundle validation.
if ($fixed_block
->getBlockContentBundle() != $block_content
->bundle()) {
throw new \InvalidArgumentException(sprintf('The type of the given block "%s" does not match the configured block type "%s".', $block_content
->bundle(), $fixed_block
->getBlockContentBundle()));
}
if ($block_content
->isNew()) {
// Save the new block to get an ID, it is required to mapping.
$block_content
->save();
// New blocks (not read from the storage) must be added to the entity
// memory cache to maintain the same object across the execution.
$cid = 'values:block_content:' . $block_content
->id();
$this->memoryCache
->set($cid, $block_content);
}
if ($current_block = $this
->getBlockContent($fixed_block
->id())) {
// If linking the same block, no action needed.
if ($current_block
->id() == $block_content
->id()) {
return;
}
// Replacing existing block content with another, we need first to
// release the old one.
$this
->releaseBlockContent($fixed_block);
}
// Add fixed to content record in the mapping DB.
$this->database
->insert('fixed_block_content')
->fields([
'fbid' => $fixed_block
->id(),
'bid' => $block_content
->id(),
])
->execute();
}