public function ComponentBlock::build in Component blocks 1.x
Same name and namespace in other branches
- 1.0.x src/Plugin/Block/ComponentBlock.php \Drupal\component_blocks\Plugin\Block\ComponentBlock::build()
- 1.1.x src/Plugin/Block/ComponentBlock.php \Drupal\component_blocks\Plugin\Block\ComponentBlock::build()
Builds and returns the renderable array for this block plugin.
If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).
Return value
array A renderable array representing the content of the block.
Overrides BlockPluginInterface::build
See also
\Drupal\block\BlockViewBuilder
File
- src/
Plugin/ Block/ ComponentBlock.php, line 187
Class
- ComponentBlock
- Defines a class for a specially shaped block.
Namespace
Drupal\component_blocks\Plugin\BlockCode
public function build() {
$definition = $this
->uiPatternsManager()
->getDefinition($this->pluginDefinition['ui_pattern_id']);
$context = [];
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $this
->getContextValue('entity');
$view_builder = $this->entityTypeManager
->getViewBuilder($entity
->getEntityTypeId());
$metadata = new BubbleableMetadata();
$metadata
->addCacheableDependency($entity);
foreach ($this
->getConfiguration()['variables'] as $context_id => $details) {
if ($details['source'] === self::FIXED) {
if (!is_scalar($details['value'])) {
// Allow for array default values.
$context[$context_id] = $details['value'];
continue;
}
try {
$value = $this->token
->replace($details['value'], [
$entity
->getEntityTypeId() => $entity,
], [], $metadata);
if ($value !== $details['value']) {
// Token replacement sanitizes, so we need to flag as such.
$value = Markup::create($value);
}
} catch (EntityMalformedException $e) {
// Attempt to get e.g an entity URL without a saved entity in layout
// builder.
$value = '';
}
$context[$context_id] = $value;
continue;
}
try {
$formatter_output = $view_builder
->viewField($entity
->get($details['source']), array_intersect_key($details, [
'type' => TRUE,
'settings' => TRUE,
]) + [
'label' => 'hidden',
]);
if (Element::isEmpty($formatter_output)) {
// No output other than cache metadata.
$metadata
->merge(CacheableMetadata::createFromRenderArray($formatter_output));
continue;
}
$context[$context_id] = [
'#theme' => 'field__component_block',
] + $formatter_output;
} catch (EntityMalformedException $e) {
// Attempt to get e.g an entity URL without a saved entity in layout
// builder.
$context[$context_id] = '';
}
}
$build = [
'#type' => 'pattern',
'#id' => $this->pluginDefinition['ui_pattern_id'],
'#fields' => $context,
'#context' => [
'type' => 'entity',
'entity' => $entity,
],
];
if (isset($this
->getConfiguration()['variant'])) {
$build['#variant'] = $this
->getConfiguration()['variant'];
}
if (isset($this
->getConfiguration()['settings'])) {
$build['#settings'] = $this
->getConfiguration()['settings'];
}
// Attach libraries to the block;.
if (!empty($definition['libraries'])) {
$metadata
->addAttachments([
'library' => $definition
->getLibrariesNames(),
]);
}
$metadata
->applyTo($build);
return $build;
}