public function WebformEntityReferenceEntityFormatter::viewElements in Webform 8.5
Same name and namespace in other branches
- 6.x src/Plugin/Field/FieldFormatter/WebformEntityReferenceEntityFormatter.php \Drupal\webform\Plugin\Field\FieldFormatter\WebformEntityReferenceEntityFormatter::viewElements()
Builds a renderable array for a field value.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
string $langcode: The language that should be used to render the field.
Return value
array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.
Overrides FormatterInterface::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ WebformEntityReferenceEntityFormatter.php, line 168
Class
- WebformEntityReferenceEntityFormatter
- Plugin implementation of the 'Webform rendered entity' formatter.
Namespace
Drupal\webform\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
// Get items entity, which is the entity that the webform
// is directly attached to. For Paragraphs this would be the field's
// paragraph entity.
$items_entity = $items
->getEntity();
// Get the items main entity. For Paragraphs this would be the parent entity
// of the the paragraph field.
$items_main_entity = WebformSourceEntityManager::getMainSourceEntity($items_entity);
$request_source_entity = $this->requestHandler
->getCurrentSourceEntity();
// Determine if webform is previewed within a Paragraph on
// entity edit forms (via *.edit_form or .content_translation_add routes).
$route_name = $this->routeMatch
->getRouteName();
$is_entity_edit_form = preg_match('/\\.edit_form$/', $route_name) || preg_match('/\\.content_translation_add$/', $route_name) || in_array($route_name, [
'entity.block_content.canonical',
]);
// Same goes for entity add forms.
$is_entity_add_form = preg_match('/\\.add$/', $route_name);
$is_paragraph = $items_entity && $items_entity
->getEntityTypeId() === 'paragraph';
$is_paragraph_current_source_entity = $items_main_entity && $request_source_entity && $items_main_entity
->getEntityTypeId() === $request_source_entity
->getEntityTypeId() && $items_main_entity
->id() === $request_source_entity
->id();
$is_paragraph_entity_edit_form = $is_entity_edit_form && $is_paragraph && $is_paragraph_current_source_entity;
$is_paragraph_entity_add_form = $is_entity_add_form && $is_paragraph;
$elements = [];
foreach ($this
->getEntitiesToView($items, $langcode) as $delta => $entity) {
if ($is_paragraph_entity_edit_form || $is_paragraph_entity_add_form) {
// Webform can not be nested within node edit form because the nested
// <form> tags will cause unexpected validation issues.
$elements[$delta] = [
'#type' => 'webform_message',
'#message_message' => $this
->t('%label webform can not be previewed when editing content.', [
'%label' => $entity
->label(),
]),
'#message_type' => 'info',
];
}
else {
$elements[$delta] = [
'#type' => 'webform',
'#webform' => $entity,
'#default_data' => !empty($items[$delta]->default_data) ? Yaml::decode($items[$delta]->default_data) : [],
'#entity' => $this
->getSetting('source_entity') ? $items_entity : NULL,
];
}
$this
->setCacheContext($elements[$delta], $entity, $items[$delta]);
}
return $elements;
}