You are here

public function YamlFormElementBase::prepare in YAML Form 8

Prepare an element to be rendered within a form.

Parameters

array $element: An element.

\Drupal\yamlform\YamlFormSubmissionInterface $yamlform_submission: A form submission.

Overrides YamlFormElementInterface::prepare

17 calls to YamlFormElementBase::prepare()
Captcha::prepare in src/Plugin/YamlFormElement/Captcha.php
Prepare an element to be rendered within a form.
Color::prepare in src/Plugin/YamlFormElement/Color.php
Prepare an element to be rendered within a form.
DateBase::prepare in src/Plugin/YamlFormElement/DateBase.php
Prepare an element to be rendered within a form.
Details::prepare in src/Plugin/YamlFormElement/Details.php
Prepare an element to be rendered within a form.
EntityAutocomplete::prepare in src/Plugin/YamlFormElement/EntityAutocomplete.php
Prepare an element to be rendered within a form.

... See full list

18 methods override YamlFormElementBase::prepare()
Captcha::prepare in src/Plugin/YamlFormElement/Captcha.php
Prepare an element to be rendered within a form.
Color::prepare in src/Plugin/YamlFormElement/Color.php
Prepare an element to be rendered within a form.
DateBase::prepare in src/Plugin/YamlFormElement/DateBase.php
Prepare an element to be rendered within a form.
Details::prepare in src/Plugin/YamlFormElement/Details.php
Prepare an element to be rendered within a form.
EntityAutocomplete::prepare in src/Plugin/YamlFormElement/EntityAutocomplete.php
Prepare an element to be rendered within a form.

... See full list

File

src/YamlFormElementBase.php, line 388

Class

YamlFormElementBase
Provides a base class for a form element.

Namespace

Drupal\yamlform

Code

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

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

  // Apply element specific access rules.
  $operation = $yamlform_submission
    ->isCompleted() ? 'update' : 'create';
  $element['#access'] = $this
    ->checkAccessRules($operation, $element);

  // Add #allowed_tags.
  $allowed_tags = $this->configFactory
    ->get('yamlform.settings')
    ->get('elements.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 inline title display support.
  if (isset($element['#title_display']) && $element['#title_display'] == 'inline') {
    unset($element['#title_display']);
    $element['#wrapper_attributes']['class'][] = 'yamlform-element--title-inline';
  }

  // Add default description display.
  $default_description_display = $this->configFactory
    ->get('yamlform.settings')
    ->get('elements.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-yamlform-element-tooltip';
    $element[$attributes_property]['class'][] = 'yamlform-element-tooltip';
    $element['#attached']['library'][] = 'yamlform/yamlform.element.tooltip';
  }

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

  // Add validation handler for #unique value.
  if (!empty($element['#unique']) && !$this
    ->hasMultipleValues($element)) {
    $element['#element_validate'][] = [
      get_class($this),
      'validateUnique',
    ];
    $element['#yamlform'] = $yamlform_submission
      ->getYamlForm()
      ->id();
    $element['#yamlform_submission'] = $yamlform_submission
      ->id();
  }

  // Prepare Flexbox and #states wrapper.
  $this
    ->prepareWrapper($element);

  // Replace tokens for all properties.
  $element = $this->tokenManager
    ->replace($element, $yamlform_submission);
}