View source
<?php
namespace Drupal\webform_views\WebformElementViews;
use Drupal\Component\Utility\Html;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\webform\Plugin\WebformElementInterface;
use Drupal\webform\WebformInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WebformCompositeViews extends WebformElementViewsAbstract {
public function getViewsData($element, WebformInterface $webform) {
$data = parent::getViewsData($element, $webform);
$table_alias = 'webform_submission_field_' . $webform
->id() . '_' . $element['#webform_key'];
$element_title = isset($element['#title']) && $element['#title'] ? $element['#title'] : $element['#webform_key'];
$element_plugin = $this->webformElementManager
->getElementInstance($element);
if (isset($element['#webform_composite_elements'])) {
$composite_elements = array_keys($element['#webform_composite_elements']);
foreach ($composite_elements as $composite_key) {
$data[$table_alias . '__' . $composite_key]['table']['group'] = $this
->t('Webform @webform submission data', [
'@webform' => $webform
->label(),
]);
$data[$table_alias . '__' . $composite_key]['table']['join'][$this->entityType
->getBaseTable()] = [
'table' => 'webform_submission_data',
'field' => 'sid',
'left_field' => 'sid',
'extra' => [
[
'field' => 'name',
'value' => $element['#webform_key'],
],
[
'field' => 'property',
'value' => $composite_key,
],
],
];
$title = isset($element['#webform_composite_elements'][$composite_key]['#title']) ? $element['#webform_composite_elements'][$composite_key]['#title'] : $composite_key;
$data[$table_alias . '__' . $composite_key]['webform_submission_value'] = [
'title' => Html::escape($element_title . ': ' . $title),
'help' => $this
->t('Value of the field %field property in webform %webform submission.', [
'%field' => $element_title,
'%webform' => $webform
->label(),
]),
];
foreach ($this
->getCompositeViewsData($element_plugin, $element, $composite_key) as $k => $v) {
$v += [
'webform_id' => $webform
->id(),
'webform_submission_field' => $element['#webform_key'],
'webform_submission_property' => $composite_key,
];
$data[$table_alias . '__' . $composite_key]['webform_submission_value'][$k] = $v;
}
}
}
return $data;
}
protected function getCompositeViewsData(WebformElementInterface $element_plugin, array $element, $composite_key) {
$views_data = [];
$views_data['field'] = [
'id' => 'webform_submission_composite_field',
'real field' => $this->entityType
->getKey('id'),
'click sortable' => TRUE,
'multiple' => $element_plugin
->hasMultipleValues($element),
];
$views_data['sort'] = [
'id' => 'webform_submission_field_sort',
'real field' => 'value',
];
$views_data['filter'] = [
'id' => 'webform_submission_composite_field_filter',
'real field' => 'value',
];
return $views_data;
}
}