public function DefaultContentHandler::exportDefaultContent in Fixed Block Content 8
Export the default content stored in config to a new custom block.
Parameters
\Drupal\fixed_block_content\FixedBlockContentInterface $fixed_block: The fixed block to work with.
Return value
\Drupal\block_content\BlockContentInterface The new custom block content (unsaved), NULL if no default content or there ware errors.
Overrides DefaultContentHandlerInterface::exportDefaultContent
File
- src/
DefaultContentHandler.php, line 82
Class
- DefaultContentHandler
- Fixed block content default content handler.
Namespace
Drupal\fixed_block_contentCode
public function exportDefaultContent(FixedBlockContentInterface $fixed_block) {
if (empty($fixed_block
->get('default_content'))) {
return NULL;
}
$new_block_content = NULL;
try {
// Set the internal link domain.
// @todo take this value from config
$this->linkManager
->setLinkDomain('http://fixed_block_content.drupal.org');
// Invalidates type links cache.
// @todo remove when #2928882 is solved
$this->cache
->invalidate('hal:links:types');
/** @var \Drupal\block_content\BlockContentInterface $new_block_content */
$new_block_content = $this->serializer
->deserialize($fixed_block
->get('default_content'), BlockContent::class, 'hal_json', [
'fixed_block_content' => $fixed_block,
]);
} catch (\Exception $e) {
try {
$message = $e
->getMessage();
// Earlier versions stored the default content in plain JSON (not HAL).
$new_block_content = $this->serializer
->deserialize($fixed_block
->get('default_content'), BlockContent::class, 'json', [
'fixed_block_content' => $fixed_block,
]);
} catch (\Exception $e) {
// Deserialization fails.
$this->logger
->error('Unable to export default content for fixed block %id, deserialization fails with message "%message"', [
'%id' => $fixed_block
->id(),
'%message' => $message,
]);
}
}
// Updates the reusable mark according to the fixed block protected option.
if ($new_block_content) {
$new_block_content
->set('reusable', !$fixed_block
->isProtected());
}
// Restore HAL link domain to default.
$this->linkManager
->setLinkDomain('');
// Invalidates type links cache.
// @todo remove when #2928882 is solved
$this->cache
->invalidate('hal:links:types');
return $new_block_content;
}