You are here

class WebformEntityReferenceViews in Webform Views Integration 8.5

Webform views handler for entity reference webform elements.

Hierarchy

Expanded class hierarchy of WebformEntityReferenceViews

1 file declares its use of WebformEntityReferenceViews
webform_views.module in ./webform_views.module
Webform integration with views.

File

src/WebformElementViews/WebformEntityReferenceViews.php, line 15

Namespace

Drupal\webform_views\WebformElementViews
View source
class WebformEntityReferenceViews extends WebformDefaultViews {

  /**
   * Entity type manager service.
   *
   * @var EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * WebformEntityReferenceViews constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity type manager service
   * @param \Drupal\webform\Plugin\WebformElementManagerInterface $webform_element_manager
   *   Webform element manager service
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, WebformElementManagerInterface $webform_element_manager) {
    parent::__construct($entity_type_manager, $webform_element_manager);
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function getViewsData($element, WebformInterface $webform) {
    $views_data = parent::getViewsData($element, $webform);

    // Add reverse relationship from referenced entity to webform submission via
    // this entity reference element.
    $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;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * Retrieve target entity type from provided element.
   *
   * @param \Drupal\webform\Plugin\WebformElementEntityReferenceInterface $element_plugin
   *   Element plugin that corresponds to $element
   * @param array $element
   *   Webform element whose target entity type is requested
   *
   * @return \Drupal\Core\Entity\EntityTypeInterface
   *   Target entity type of the provided element
   */
  protected function getTargetEntityType(WebformElementEntityReferenceInterface $element_plugin, array $element) {
    return $this->entityTypeManager
      ->getDefinition($element_plugin
      ->getTargetType($element));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WebformElementViewsAbstract::$entityType protected property Webform submission entity type.
WebformElementViewsAbstract::$webformElementManager protected property
WebformElementViewsAbstract::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
WebformElementViewsAbstract::tableName protected function Generate views table name that represents a given element within a webform.
WebformEntityReferenceViews::$entityTypeManager protected property Entity type manager service.
WebformEntityReferenceViews::getElementViewsData public function Generate views data definition that corresponds to given webform element. Overrides WebformDefaultViews::getElementViewsData 1
WebformEntityReferenceViews::getTargetEntityType protected function Retrieve target entity type from provided element.
WebformEntityReferenceViews::getViewsData public function Generate views data related to a given element of a given webform. Overrides WebformElementViewsAbstract::getViewsData
WebformEntityReferenceViews::__construct public function WebformEntityReferenceViews constructor. Overrides WebformElementViewsAbstract::__construct