class WebformSubmissionViewBuilder in Webform 8.5
Same name and namespace in other branches
- 6.x src/WebformSubmissionViewBuilder.php \Drupal\webform\WebformSubmissionViewBuilder
Render controller for webform submissions.
Hierarchy
- class \Drupal\Core\Entity\EntityHandlerBase uses DependencySerializationTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityViewBuilder implements EntityHandlerInterface, EntityViewBuilderInterface, TrustedCallbackInterface uses DeprecatedServicePropertyTrait
- class \Drupal\webform\WebformSubmissionViewBuilder implements WebformSubmissionViewBuilderInterface
- class \Drupal\Core\Entity\EntityViewBuilder implements EntityHandlerInterface, EntityViewBuilderInterface, TrustedCallbackInterface uses DeprecatedServicePropertyTrait
Expanded class hierarchy of WebformSubmissionViewBuilder
File
- src/
WebformSubmissionViewBuilder.php, line 23
Namespace
Drupal\webformView source
class WebformSubmissionViewBuilder extends EntityViewBuilder implements WebformSubmissionViewBuilderInterface {
/**
* The route match object.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* The webform request handler.
*
* @var \Drupal\webform\WebformRequestInterface
*/
protected $requestHandler;
/**
* The webform element manager service.
*
* @var \Drupal\webform\Plugin\WebformElementManagerInterface
*/
protected $elementManager;
/**
* The webform submission (server-side) conditions (#states) validator.
*
* @var \Drupal\webform\WebformSubmissionConditionsValidator
*/
protected $conditionsValidator;
/**
* Constructs a WebformSubmissionViewBuilder.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\webform\WebformRequestInterface $webform_request
* The webform request handler.
* @param \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager
* The webform element manager service.
* @param \Drupal\webform\WebformSubmissionConditionsValidatorInterface $conditions_validator
* The webform submission conditions (#states) validator.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match object.
*
* @todo Webform 8.x-6.x: Move $route_match before $webform_request.
*/
public function __construct(EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager, WebformRequestInterface $webform_request, WebformElementManagerInterface $element_manager, WebformSubmissionConditionsValidatorInterface $conditions_validator, RouteMatchInterface $route_match = NULL) {
parent::__construct($entity_type, $entity_manager, $language_manager);
$this->requestHandler = $webform_request;
$this->elementManager = $element_manager;
$this->conditionsValidator = $conditions_validator;
$this->routeMatch = $route_match ?: \Drupal::routeMatch();
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity.manager'), $container
->get('language_manager'), $container
->get('webform.request'), $container
->get('plugin.manager.webform.element'), $container
->get('webform_submission.conditions_validator'), $container
->get('current_route_match'));
}
/**
* {@inheritdoc}
*/
public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
// Allow modules to set custom webform submission view mode.
// @see \Drupal\webform_entity_print\Plugin\WebformExporter\WebformEntityPrintWebformExporter::writeSubmission
if ($webform_submissions_view_mode = \Drupal::request()->request
->get('_webform_submissions_view_mode')) {
$view_mode = $webform_submissions_view_mode;
}
// Apply variants.
/** @var \Drupal\webform\WebformSubmissionInterface $entity */
/** @var \Drupal\webform\WebformInterface $webform */
$webform = $entity
->getWebform();
$webform
->applyVariants($entity);
return parent::view($entity, $view_mode, $langcode);
}
/**
* {@inheritdoc}
*/
protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
$build = parent::getBuildDefaults($entity, $view_mode);
// The webform submission will be rendered in the wrapped webform submission
// template already. Instead we are going to wrap the rendered submission
// in a webform submission data template.
// @see \Drupal\contact_storage\ContactMessageViewBuilder
// @see \Drupal\comment\CommentViewBuilder::getBuildDefaults
// @see \Drupal\block_content\BlockContentViewBuilder::getBuildDefaults
// @see webform-submission-data.html.twig
$build['#theme'] = 'webform_submission_data';
return $build;
}
/**
* {@inheritdoc}
*/
public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
if (empty($entities)) {
return;
}
/** @var \Drupal\webform\WebformSubmissionInterface[] $entities */
foreach ($entities as $id => $webform_submission) {
$webform = $webform_submission
->getWebform();
if ($view_mode === 'preview') {
$options = [
'view_mode' => $view_mode,
'excluded_elements' => $webform
->getSetting('preview_excluded_elements'),
'exclude_empty' => $webform
->getSetting('preview_exclude_empty'),
'exclude_empty_checkbox' => $webform
->getSetting('preview_exclude_empty_checkbox'),
];
}
else {
// Track PDF.
// @see webform_entity_print.module
$route_name = $this->routeMatch
->getRouteName();
$pdf = in_array($route_name, [
'entity_print.view.debug',
'entity_print.view',
]) || \Drupal::request()->request
->get('_webform_entity_print');
$options = [
'view_mode' => $view_mode,
'excluded_elements' => $webform
->getSetting('submission_excluded_elements'),
'exclude_empty' => $webform
->getSetting('submission_exclude_empty'),
'exclude_empty_checkbox' => $webform
->getSetting('submission_exclude_empty_checkbox'),
'pdf' => $pdf,
];
}
switch ($view_mode) {
case 'twig':
// @see \Drupal\webform_entity_print_attachment\Element\WebformEntityPrintAttachment::getFileContent
$build[$id]['data'] = WebformTwigExtension::buildTwigTemplate($webform_submission, $webform_submission->_webform_view_mode_twig);
break;
case 'yaml':
// Note that the YAML view ignores all access controls and excluded
// settings.
$data = $webform_submission
->toArray(TRUE, TRUE);
// Covert computed element value markup to strings to
// 'Object support when dumping a YAML file has been disabled' errors.
WebformElementHelper::convertRenderMarkupToStrings($data);
$build[$id]['data'] = [
'#theme' => 'webform_codemirror',
'#code' => WebformYaml::encode($data),
'#type' => 'yaml',
];
break;
case 'text':
$elements = $webform
->getElementsInitialized();
$build[$id]['data'] = [
'#theme' => 'webform_codemirror',
'#code' => $this
->buildElements($elements, $webform_submission, $options, 'text'),
];
break;
case 'table':
$elements = $webform
->getElementsInitializedFlattenedAndHasValue();
$build[$id]['data'] = $this
->buildTable($elements, $webform_submission, $options);
break;
default:
case 'html':
$elements = $webform
->getElementsInitialized();
$build[$id]['data'] = $this
->buildElements($elements, $webform_submission, $options);
break;
}
}
parent::buildComponents($build, $entities, $displays, $view_mode);
}
/**
* {@inheritdoc}
*/
public function buildElements(array $elements, WebformSubmissionInterface $webform_submission, array $options = [], $format = 'html') {
$build_method = 'build' . ucfirst($format);
$build = [];
foreach ($elements as $key => $element) {
if (!WebformElementHelper::isElement($element, $key)) {
continue;
}
/** @var \Drupal\webform\Plugin\WebformElementInterface $webform_element */
$webform_element = $this->elementManager
->getElementInstance($element);
// Replace tokens before building the element.
$webform_element
->replaceTokens($element, $webform_submission);
if ($build_element = $webform_element
->{$build_method}($element, $webform_submission, $options)) {
$build[$key] = $build_element;
if (!$this
->isElementVisible($element, $webform_submission, $options)) {
$build[$key]['#access'] = FALSE;
}
}
}
return $build;
}
/**
* {@inheritdoc}
*/
public function buildTable(array $elements, WebformSubmissionInterface $webform_submission, array $options = []) {
$rows = [];
foreach ($elements as $key => $element) {
if (!$this
->isElementVisible($element, $webform_submission, $options)) {
continue;
}
/** @var \Drupal\webform\Plugin\WebformElementInterface $webform_element */
$webform_element = $this->elementManager
->getElementInstance($element);
// Replace tokens before building the element.
$webform_element
->replaceTokens($element, $webform_submission);
// Check if empty value is excluded.
if ($webform_element
->isEmptyExcluded($element, $options) && !$webform_element
->getValue($element, $webform_submission, $options)) {
continue;
}
$title = $element['#admin_title'] ?: $element['#title'] ?: '(' . $key . ')';
// Note: Not displaying an empty message since empty values just render
// an empty table cell.
$html = $webform_element
->formatHtml($element, $webform_submission, $options);
$rows[$key] = [
[
'header' => TRUE,
'data' => $title,
],
[
'data' => is_string($html) ? [
'#markup' => $html,
] : $html,
],
];
}
return [
'#type' => 'table',
'#rows' => $rows,
'#attributes' => [
'class' => [
'webform-submission-table',
],
],
];
}
/**
* Determines if an element is visible.
*
* @param array $element
* The element to check for visibility.
* @param \Drupal\webform\WebformSubmissionInterface $webform_submission
* A webform submission.
* @param array $options
* - excluded_elements: An array of elements to be excluded.
* - ignore_access: Flag to ignore private and/or access controls and always
* display the element.
* - email: Format element to be send via email.
*
* @return bool
* TRUE if the element is visible, otherwise FALSE.
*
* @see \Drupal\webform\WebformSubmissionConditionsValidatorInterface::isElementVisible
* @see \Drupal\Core\Render\Element::isVisibleElement
*/
protected function isElementVisible(array $element, WebformSubmissionInterface $webform_submission, array $options) {
// Checked excluded elements.
if (isset($element['#webform_key']) && isset($options['excluded_elements'][$element['#webform_key']])) {
return FALSE;
}
// Checked excluded attachments, except from composite elements.
// @see \Drupal\webform\Plugin\WebformElement\WebformCompositeBase::formatComposite
if (!empty($options['exclude_attachments'])) {
/** @var \Drupal\webform\Plugin\WebformElementInterface $webform_element */
$webform_element = $this->elementManager
->getElementInstance($element, $webform_submission);
if ($webform_element instanceof WebformElementAttachmentInterface && !$webform_element instanceof WebformElementCompositeInterface) {
return FALSE;
}
}
// Check if the element is conditionally hidden.
if (!$this->conditionsValidator
->isElementVisible($element, $webform_submission)) {
return FALSE;
}
// Check if ignore access is set.
// This is used email handlers to include administrative elements in emails.
if (!empty($options['ignore_access'])) {
return TRUE;
}
// Check check the element's #access.
if (isset($element['#access']) && ($element['#access'] instanceof AccessResultInterface && $element['#access']
->isForbidden() || $element['#access'] === FALSE)) {
return FALSE;
}
// Finally, check the element's 'view' access.
/** @var \Drupal\webform\Plugin\WebformElementInterface $webform_element */
$webform_element = $this->elementManager
->getElementInstance($element, $webform_submission);
return $webform_element
->checkAccessRules('view', $element) ? TRUE : FALSE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
DeprecatedServicePropertyTrait:: |
public | function | Allows to access deprecated/removed properties. | |
EntityHandlerBase:: |
protected | property | The module handler to invoke hooks on. | 2 |
EntityHandlerBase:: |
protected | function | Gets the module handler. | 2 |
EntityHandlerBase:: |
public | function | Sets the module handler for this handler. | |
EntityViewBuilder:: |
protected | property | The cache bin used to store the render cache. | |
EntityViewBuilder:: |
protected | property | ||
EntityViewBuilder:: |
protected | property | The entity display repository. | |
EntityViewBuilder:: |
protected | property | The entity repository service. | |
EntityViewBuilder:: |
protected | property | Information about the entity type. | |
EntityViewBuilder:: |
protected | property | The type of entities for which this view builder is instantiated. | |
EntityViewBuilder:: |
protected | property | The language manager. | |
EntityViewBuilder:: |
protected | property | The EntityViewDisplay objects created for individual field rendering. | |
EntityViewBuilder:: |
protected | property | The theme registry. | |
EntityViewBuilder:: |
protected | function | Add contextual links. | |
EntityViewBuilder:: |
protected | function | Specific per-entity building. | 1 |
EntityViewBuilder:: |
public | function | Builds an entity's view; augments entity defaults. | |
EntityViewBuilder:: |
public | function | Builds multiple entities' views; augments entity defaults. | |
EntityViewBuilder:: |
public | function |
The cache tag associated with this entity view builder. Overrides EntityViewBuilderInterface:: |
|
EntityViewBuilder:: |
protected | function | Gets an EntityViewDisplay for rendering an individual field. | |
EntityViewBuilder:: |
protected | function | Determines whether the view mode is cacheable. | |
EntityViewBuilder:: |
public | function |
Resets the entity render cache. Overrides EntityViewBuilderInterface:: |
|
EntityViewBuilder:: |
public static | function |
Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface:: |
2 |
EntityViewBuilder:: |
public | function |
Builds a renderable array for the value of a single field in an entity. Overrides EntityViewBuilderInterface:: |
|
EntityViewBuilder:: |
public | function |
Builds a renderable array for a single field item. Overrides EntityViewBuilderInterface:: |
|
EntityViewBuilder:: |
public | function |
Builds the render array for the provided entities. Overrides EntityViewBuilderInterface:: |
4 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TrustedCallbackInterface:: |
constant | Untrusted callbacks throw exceptions. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger silenced E_USER_DEPRECATION errors. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger E_USER_WARNING errors. | ||
WebformSubmissionViewBuilder:: |
protected | property | The webform submission (server-side) conditions (#states) validator. | |
WebformSubmissionViewBuilder:: |
protected | property | The webform element manager service. | |
WebformSubmissionViewBuilder:: |
protected | property | The webform request handler. | |
WebformSubmissionViewBuilder:: |
protected | property | The route match object. | |
WebformSubmissionViewBuilder:: |
public | function |
Builds the component fields and properties of a set of entities. Overrides EntityViewBuilder:: |
|
WebformSubmissionViewBuilder:: |
public | function |
Build element display items from elements and submitted data. Overrides WebformSubmissionViewBuilderInterface:: |
|
WebformSubmissionViewBuilder:: |
public | function |
Build table display from elements and submitted data. Overrides WebformSubmissionViewBuilderInterface:: |
|
WebformSubmissionViewBuilder:: |
public static | function |
Instantiates a new instance of this entity handler. Overrides EntityViewBuilder:: |
|
WebformSubmissionViewBuilder:: |
protected | function |
Provides entity-specific defaults to the build process. Overrides EntityViewBuilder:: |
|
WebformSubmissionViewBuilder:: |
protected | function | Determines if an element is visible. | |
WebformSubmissionViewBuilder:: |
public | function |
Builds the render array for the provided entity. Overrides EntityViewBuilder:: |
|
WebformSubmissionViewBuilder:: |
public | function |
Constructs a WebformSubmissionViewBuilder. Overrides EntityViewBuilder:: |