trait WebformDisplayOnTrait in Webform 6.x
Same name and namespace in other branches
- 8.5 src/Plugin/WebformElement/WebformDisplayOnTrait.php \Drupal\webform\Plugin\WebformElement\WebformDisplayOnTrait
Provides an 'display_on' trait.
Hierarchy
- trait \Drupal\webform\Plugin\WebformElement\WebformDisplayOnTrait
1 file declares its use of WebformDisplayOnTrait
- WebformAttachmentBase.php in modules/
webform_attachment/ src/ Plugin/ WebformElement/ WebformAttachmentBase.php
File
- src/
Plugin/ WebformElement/ WebformDisplayOnTrait.php, line 11
Namespace
Drupal\webform\Plugin\WebformElementView source
trait WebformDisplayOnTrait {
/**
* {@inheritdoc}
*/
public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
parent::prepare($element, $webform_submission);
// Hide element if it should not be displayed on 'form'.
if (!$this
->isDisplayOn($element, WebformElementDisplayOnInterface::DISPLAY_ON_FORM)) {
$element['#access'] = FALSE;
}
}
/**
* {@inheritdoc}
*/
public function buildHtml(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
// Hide element if it should not be displayed on 'view'.
if (!$this
->isDisplayOn($element, WebformElementDisplayOnInterface::DISPLAY_ON_VIEW)) {
return [];
}
return parent::buildHtml($element, $webform_submission, $options);
}
/**
* {@inheritdoc}
*/
public function buildText(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
// Hide element if it should not be displayed on 'view'.
if (!$this
->isDisplayOn($element, WebformElementDisplayOnInterface::DISPLAY_ON_VIEW)) {
return [];
}
return parent::buildText($element, $webform_submission, $options);
}
/**
* Check is the element is display on form, view, or both.
*
* @param array $element
* An element.
* @param string $display_on
* Display on form or view.
*
* @return bool
* TRUE if the element should be displayed on the form or view.
*/
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;
}
/**
* Get display on options.
*
* @param bool $none
* If TRUE none is include.
*
* @return array
* An associative array of display on options.
*/
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
WebformDisplayOnTrait:: |
public | function | 3 | |
WebformDisplayOnTrait:: |
public | function | 3 | |
WebformDisplayOnTrait:: |
protected | function | Get display on options. | |
WebformDisplayOnTrait:: |
protected | function | Check is the element is display on form, view, or both. | |
WebformDisplayOnTrait:: |
public | function | 4 |