View source
<?php
namespace Drupal\webform_views\WebformElementViews;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\webform\Plugin\WebformElementEntityReferenceInterface;
use Drupal\webform\Plugin\WebformElementInterface;
use Drupal\webform\Plugin\WebformElementManagerInterface;
use Drupal\webform\WebformInterface;
class WebformEntityReferenceViews extends WebformDefaultViews {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, WebformElementManagerInterface $webform_element_manager) {
parent::__construct($entity_type_manager, $webform_element_manager);
$this->entityTypeManager = $entity_type_manager;
}
public function getViewsData($element, WebformInterface $webform) {
$views_data = parent::getViewsData($element, $webform);
$target_entity_type = $this
->getTargetEntityType($this->webformElementManager
->getElementInstance($element), $element);
if ($target_entity_type instanceof ContentEntityTypeInterface) {
$views_data[$target_entity_type
->getDataTable() ?: $target_entity_type
->getBaseTable()]['webform_submission'] = [
'title' => $this
->t('Webform submission'),
'help' => $this
->t('Webform submission(-s) that reference the @entity_label via %element_title element.', [
'@entity_label' => $target_entity_type
->getLabel(),
'%element_title' => $element['#title'],
]),
'relationship' => [
'left_field' => $target_entity_type
->getKey('id'),
'base' => $this->entityType
->getBaseTable(),
'base field' => $this->entityType
->getKey('id'),
'id' => 'webform_views_entity_reverse',
'label' => $this
->t('Webform submission'),
'webform element' => $element['#webform_key'],
'webform' => $webform
->id(),
],
];
}
return $views_data;
}
public function getElementViewsData(WebformElementInterface $element_plugin, array $element) {
$views_data = parent::getElementViewsData($element_plugin, $element);
$target_entity_type = $this
->getTargetEntityType($element_plugin, $element);
if ($target_entity_type instanceof ContentEntityTypeInterface) {
$views_data['relationship'] = [
'base' => $target_entity_type
->getDataTable() ? $target_entity_type
->getDataTable() : $target_entity_type
->getBaseTable(),
'field' => 'value',
'base field' => $target_entity_type
->getKey('id'),
'label' => $target_entity_type
->getLabel(),
'title' => $this
->t('Referenced @entity_label', [
'@entity_label' => $target_entity_type
->getLabel(),
]),
'id' => 'standard',
];
}
return $views_data;
}
protected function getTargetEntityType(WebformElementEntityReferenceInterface $element_plugin, array $element) {
return $this->entityTypeManager
->getDefinition($element_plugin
->getTargetType($element));
}
}