public function ParagraphBlock::build in Paragraph blocks 3.x
Same name and namespace in other branches
- 8.2 src/Plugin/Block/ParagraphBlock.php \Drupal\paragraph_blocks\Plugin\Block\ParagraphBlock::build()
- 8 src/Plugin/Block/ParagraphBlock.php \Drupal\paragraph_blocks\Plugin\Block\ParagraphBlock::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/ ParagraphBlock.php, line 91
Class
- ParagraphBlock
- Provides a block to a paragraph value on an entity.
Namespace
Drupal\paragraph_blocks\Plugin\BlockCode
public function build() {
// Get the referencing and referenced entity.
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$paragraph = NULL;
$entity = $this
->getContextEntity();
if ($entity) {
$referenced_entities = $entity
->get($this->fieldName)
->referencedEntities();
if (isset($referenced_entities[$this->fieldDelta])) {
$paragraph = $referenced_entities[$this->fieldDelta];
}
}
if (!$paragraph) {
// The Content group block exists on the page, but the page's Content
// group has been removed.
return [
'#markup' => $this
->t('This block is broken. The Content group or the paragraph does not exist.'),
];
}
// Build the render array.
/** @var \Drupal\Core\Entity\EntityViewBuilder $view_builder */
$view_builder = $this->entityTypeManager
->getViewBuilder($paragraph
->getEntityTypeId());
$build = $view_builder
->view($paragraph, 'default');
// Add geysir contextual links.
if (function_exists('geysir_contextual_links')) {
$link_options = [
'parent_entity_type' => $entity
->getEntityType()
->id(),
'parent_entity' => $entity
->id(),
'field' => $this->fieldName,
'field_wrapper_id' => Html::getUniqueId('geysir--' . $this->fieldName),
'delta' => $this->fieldDelta,
'js' => 'nojs',
'paragraph' => $paragraph
->id(),
];
$build['#geysir_field_paragraph_links'] = geysir_contextual_links($link_options);
$build['#theme_wrappers'][] = 'geysir_field_paragraph_wrapper';
$build['#attributes']['data-geysir-field-paragraph-field-wrapper'] = $link_options['field_wrapper_id'];
}
// Set the cache data appropriately.
CacheableMetadata::createFromObject($this
->getContext('entity'))
->applyTo($build);
return $build;
}