You are here

public function WebformElementBase::prepare in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElementBase.php \Drupal\webform\Plugin\WebformElementBase::prepare()

Prepare an element to be rendered within a webform.

Parameters

array $element: An element.

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission. Webform submission is optional since it is not used by composite sub elements.

Overrides WebformElementInterface::prepare

See also

\Drupal\webform\Element\WebformCompositeBase::processWebformComposite

25 calls to WebformElementBase::prepare()
Captcha::prepare in src/Plugin/WebformElement/Captcha.php
Prepare an element to be rendered within a webform.
Color::prepare in src/Plugin/WebformElement/Color.php
Prepare an element to be rendered within a webform.
ContainerBase::prepare in src/Plugin/WebformElement/ContainerBase.php
Prepare an element to be rendered within a webform.
DateBase::prepare in src/Plugin/WebformElement/DateBase.php
Prepare an element to be rendered within a webform.
EntityAutocomplete::prepare in src/Plugin/WebformElement/EntityAutocomplete.php
Prepare an element to be rendered within a webform.

... See full list

22 methods override WebformElementBase::prepare()
Captcha::prepare in src/Plugin/WebformElement/Captcha.php
Prepare an element to be rendered within a webform.
Color::prepare in src/Plugin/WebformElement/Color.php
Prepare an element to be rendered within a webform.
ContainerBase::prepare in src/Plugin/WebformElement/ContainerBase.php
Prepare an element to be rendered within a webform.
DateBase::prepare in src/Plugin/WebformElement/DateBase.php
Prepare an element to be rendered within a webform.
EntityAutocomplete::prepare in src/Plugin/WebformElement/EntityAutocomplete.php
Prepare an element to be rendered within a webform.

... See full list

File

src/Plugin/WebformElementBase.php, line 752

Class

WebformElementBase
Provides a base class for a webform element.

Namespace

Drupal\webform\Plugin

Code

public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
  $attributes_property = $this
    ->hasWrapper($element) ? '#wrapper_attributes' : '#attributes';
  if ($webform_submission) {

    // Add webform and webform_submission IDs to every element.
    $element['#webform'] = $webform_submission
      ->getWebform()
      ->id();
    $element['#webform_submission'] = $webform_submission
      ->id();

    // Check is the element is disabled and hide it.
    if ($this
      ->isDisabled()) {
      if ($webform_submission
        ->getWebform()
        ->access('edit')) {
        $this
          ->displayDisabledWarning($element);
      }
      $element['#access'] = FALSE;
    }

    // Apply element specific access rules.
    $operation = $webform_submission
      ->isCompleted() ? 'update' : 'create';

    // Make sure the webform and submission is set before
    // checking access rules.
    $this
      ->setEntities($webform_submission);
    $element['#access'] = $this
      ->checkAccessRules($operation, $element);
  }

  // Enable webform template preprocessing enhancements.
  // @see \Drupal\webform\Utility\WebformElementHelper::isWebformElement
  $element['#webform_element'] = TRUE;

  // Add #allowed_tags.
  $allowed_tags = $this->configFactory
    ->get('webform.settings')
    ->get('element.allowed_tags');
  switch ($allowed_tags) {
    case 'admin':
      $element['#allowed_tags'] = Xss::getAdminTagList();
      break;
    case 'html':
      $element['#allowed_tags'] = Xss::getHtmlTagList();
      break;
    default:
      $element['#allowed_tags'] = preg_split('/ +/', $allowed_tags);
      break;
  }

  // Add autocomplete attribute.
  if (isset($element['#autocomplete'])) {
    $element['#attributes']['autocomplete'] = $element['#autocomplete'];
  }

  // Add inline title display support.
  // Inline fieldset layout is handled via webform_preprocess_fieldset().
  // @see webform_preprocess_fieldset()
  if (isset($element['#title_display']) && $element['#title_display'] === 'inline') {

    // Store reference to unset #title_display.
    $element['#_title_display'] = $element['#title_display'];
    unset($element['#title_display']);
    $element['#wrapper_attributes']['class'][] = 'webform-element--title-inline';
  }

  // Add default description display.
  $default_description_display = $this->configFactory
    ->get('webform.settings')
    ->get('element.default_description_display');
  if ($default_description_display && !isset($element['#description_display']) && $this
    ->hasProperty('description_display')) {
    $element['#description_display'] = $default_description_display;
  }

  // Add tooltip description display support.
  if (isset($element['#description_display']) && $element['#description_display'] === 'tooltip') {
    $element['#description_display'] = 'invisible';
    $element[$attributes_property]['class'][] = 'js-webform-tooltip-element';
    $element[$attributes_property]['class'][] = 'webform-tooltip-element';
    $element['#attached']['library'][] = 'webform/webform.tooltip';
  }

  // Add .webform-has-field-prefix and .webform-has-field-suffix class.
  if (!empty($element['#field_prefix'])) {
    $element[$attributes_property]['class'][] = 'webform-has-field-prefix';
  }
  if (!empty($element['#field_suffix'])) {
    $element[$attributes_property]['class'][] = 'webform-has-field-suffix';
  }

  // Add 'data-webform-states-no-clear' attribute if #states_clear is FALSE.
  if (isset($element['#states_clear']) && $element['#states_clear'] === FALSE) {
    $element[$attributes_property]['data-webform-states-no-clear'] = TRUE;
  }

  // Set element's #element_validate callback so that is not replaced when
  // we append additional #element_validate callbacks.
  $this
    ->setElementDefaultCallback($element, 'element_validate');
  $this
    ->prepareElementValidateCallbacks($element, $webform_submission);
  if ($this
    ->isInput($element)) {

    // Handle #readonly support.
    // @see \Drupal\Core\Form\FormBuilder::handleInputElement
    if (!empty($element['#readonly'])) {
      $element['#attributes']['readonly'] = 'readonly';
      if ($this
        ->hasProperty('wrapper_attributes')) {
        $element['#wrapper_attributes']['class'][] = 'webform-readonly';
      }
    }

    // Set custom required error message as 'data-required-error' attribute.
    // @see Drupal.behaviors.webformRequiredError
    // @see webform.form.js
    if (!empty($element['#required_error'])) {
      $element['#attributes']['data-webform-required-error'] = WebformHtmlHelper::toPlainText($element['#required_error']);
      $element['#required_error'] = WebformHtmlHelper::toHtmlMarkup($element['#required_error']);
    }
  }

  // Replace tokens for all properties.
  if ($webform_submission) {
    $this
      ->replaceTokens($element, $webform_submission);
  }

  // Check markup properties after token replacement just-in-case markup
  // is empty.
  $markup_properties = [
    '#description',
    '#help',
    '#more',
    '#multiple__no_items_message',
  ];
  foreach ($markup_properties as $markup_property) {
    if (!empty($element[$markup_property]) && !is_array($element[$markup_property])) {
      $element[$markup_property] = WebformHtmlEditor::checkMarkup($element[$markup_property]);
    }
  }
}