View source
<?php
namespace Drupal\webform\Plugin\Field\FieldFormatter;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\webform\Plugin\WebformSourceEntityManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WebformEntityReferenceEntityFormatter extends WebformEntityReferenceFormatterBase {
protected $routeMatch;
protected $entityTypeManager;
protected $messageManager;
protected $requestHandler;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->routeMatch = $container
->get('current_route_match');
$instance->entityTypeManager = $container
->get('entity_type.manager');
$instance->messageManager = $container
->get('webform.message_manager');
$instance->requestHandler = $container
->get('webform.request');
return $instance;
}
public static function defaultSettings() {
return [
'source_entity' => TRUE,
] + parent::defaultSettings();
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$summary[] = $this
->t('Set submission source entity: @source_entity', [
'@source_entity' => $this
->getSetting('source_entity') ? $this
->t('Yes') : $this
->t('No'),
]);
return $summary;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
if ($this->fieldDefinition
->getTargetEntityTypeId() === 'paragraph') {
$title = $this
->t("Use this paragraph field's main entity as the webform submission's source entity.");
$description = $this
->t("If unchecked, the current page's entity will be used as the webform submission's source entity.");
}
else {
$entity_type_definition = $this->entityTypeManager
->getDefinition($this->fieldDefinition
->getTargetEntityTypeId());
$title = $this
->t("Use this field's %entity_type entity as the webform submission's source entity.", [
'%entity_type' => $entity_type_definition
->getLabel(),
]);
$description = $this
->t("If unchecked, the current page's entity will be used as the webform submission's source entity. For example, if this webform was displayed on a node's page, the current node would be used as the webform submission's source entity.", [
'%entity_type' => $entity_type_definition
->getLabel(),
]);
}
$form = parent::settingsForm($form, $form_state);
$form['source_entity'] = [
'#title' => $title,
'#description' => $description,
'#type' => 'checkbox',
'#return_type' => TRUE,
'#default_value' => $this
->getSetting('source_entity'),
];
return $form;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$items_entity = $items
->getEntity();
$items_main_entity = WebformSourceEntityManager::getMainSourceEntity($items_entity);
$request_source_entity = $this->requestHandler
->getCurrentSourceEntity();
$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',
]);
$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) {
$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;
}
protected function checkAccess(EntityInterface $entity) {
return AccessResult::allowed();
}
}