WebformDisplayOnTrait.php in Webform 6.x
File
src/Plugin/WebformElement/WebformDisplayOnTrait.php
View source
<?php
namespace Drupal\webform\Plugin\WebformElement;
use Drupal\webform\Plugin\WebformElementDisplayOnInterface;
use Drupal\webform\WebformSubmissionInterface;
trait WebformDisplayOnTrait {
public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
parent::prepare($element, $webform_submission);
if (!$this
->isDisplayOn($element, WebformElementDisplayOnInterface::DISPLAY_ON_FORM)) {
$element['#access'] = FALSE;
}
}
public function buildHtml(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
if (!$this
->isDisplayOn($element, WebformElementDisplayOnInterface::DISPLAY_ON_VIEW)) {
return [];
}
return parent::buildHtml($element, $webform_submission, $options);
}
public function buildText(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
if (!$this
->isDisplayOn($element, WebformElementDisplayOnInterface::DISPLAY_ON_VIEW)) {
return [];
}
return parent::buildText($element, $webform_submission, $options);
}
protected function isDisplayOn(array $element, $display_on) {
$element_display_on = isset($element['#display_on']) ? $element['#display_on'] : $this
->getDefaultProperty('display_on');
return $element_display_on === WebformElementDisplayOnInterface::DISPLAY_ON_BOTH || $element_display_on === $display_on ? TRUE : FALSE;
}
protected function getDisplayOnOptions($none = FALSE) {
$options = [
WebformElementDisplayOnInterface::DISPLAY_ON_FORM => $this
->t('form only'),
WebformElementDisplayOnInterface::DISPLAY_ON_VIEW => $this
->t('viewed submission only'),
WebformElementDisplayOnInterface::DISPLAY_ON_BOTH => $this
->t('both form and viewed submission'),
];
if ($none) {
$options[WebformElementDisplayOnInterface::DISPLAY_ON_NONE] = $this
->t('none');
}
return $options;
}
}