You are here

abstract class WebformElementViewsAbstract in Webform Views Integration 8.5

Abstract implementation of webform element views handler.

Hierarchy

Expanded class hierarchy of WebformElementViewsAbstract

File

src/WebformElementViews/WebformElementViewsAbstract.php, line 18

Namespace

Drupal\webform_views\WebformElementViews
View source
abstract class WebformElementViewsAbstract implements WebformElementViewsInterface, ContainerInjectionInterface {
  use StringTranslationTrait;

  /**
   * Webform submission entity type.
   *
   * @var EntityTypeInterface
   */
  protected $entityType;

  /**
   * @var WebformElementManagerInterface
   */
  protected $webformElementManager;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('plugin.manager.webform.element'));
  }

  /**
   * WebformElementViewsAbstract 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) {
    $this->entityType = $entity_type_manager
      ->getDefinition('webform_submission');
    $this->webformElementManager = $webform_element_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function getViewsData($element, WebformInterface $webform) {
    $table_alias = $this
      ->tableName($element, $webform);
    $element_title = isset($element['#title']) && $element['#title'] ? $element['#title'] : $element['#webform_key'];
    $element_plugin = $this->webformElementManager
      ->getElementInstance($element);
    $data[$table_alias]['table']['group'] = $this
      ->t('Webform @webform submission data', [
      '@webform' => $webform
        ->label(),
    ]);

    // For each webform submission element we create a table alias and then
    // explain to Views how to join {webform_submission_data} onto
    // {webform_submission}.
    $data[$table_alias]['table']['join'][$this->entityType
      ->getBaseTable()] = [
      'table' => 'webform_submission_data',
      'field' => $this->entityType
        ->getKey('id'),
      'left_field' => $this->entityType
        ->getKey('id'),
      'extra' => [
        [
          'field' => 'name',
          'value' => $element['#webform_key'],
        ],
      ],
    ];
    $data[$table_alias]['webform_submission_value'] = [
      'title' => Html::escape($element_title),
      'help' => $this
        ->t('Value of the field %field in webform %webform submission.', [
        '%field' => $element_title,
        '%webform' => $webform
          ->label(),
      ]),
    ];
    foreach ($this
      ->getElementViewsData($element_plugin, $element) as $k => $v) {
      $v += [
        'webform_id' => $webform
          ->id(),
        'webform_submission_field' => $element['#webform_key'],
      ];
      $data[$table_alias]['webform_submission_value'][$k] = $v;
    }
    return $data;
  }

  /**
   * {@inheritdoc}
   */
  public function getElementViewsData(WebformElementInterface $element_plugin, array $element) {
    return [
      'field' => [
        'id' => 'webform_submission_field',
        'real field' => 'value',
        'click sortable' => !$element_plugin
          ->isContainer($element) && !$element_plugin
          ->hasMultipleValues($element),
        'multiple' => $element_plugin
          ->hasMultipleValues($element),
      ],
    ];
  }

  /**
   * Generate views table name that represents a given element within a webform.
   *
   * @param array $element
   *   Webform element for which to generate views table name.
   * @param \Drupal\webform\WebformInterface $webform
   *   Webform for which to generate views table name.
   *
   * @return string
   *   Views table name that represents provided webform element within provided
   *   webform.
   */
  protected function tableName($element, WebformInterface $webform) {
    return 'webform_submission_field_' . $webform
      ->id() . '_' . $element['#webform_key'];
  }

}

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::getElementViewsData public function Generate views data definition that corresponds to given webform element. Overrides WebformElementViewsInterface::getElementViewsData 5
WebformElementViewsAbstract::getViewsData public function Generate views data related to a given element of a given webform. Overrides WebformElementViewsInterface::getViewsData 2
WebformElementViewsAbstract::tableName protected function Generate views table name that represents a given element within a webform.
WebformElementViewsAbstract::__construct public function WebformElementViewsAbstract constructor. 2