You are here

public static function WebformCodeMirror::processWebformCodeMirror in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Element/WebformCodeMirror.php \Drupal\webform\Element\WebformCodeMirror::processWebformCodeMirror()

Processes a 'webform_codemirror' element.

File

src/Element/WebformCodeMirror.php, line 89

Class

WebformCodeMirror
Provides a webform element for using CodeMirror.

Namespace

Drupal\webform\Element

Code

public static function processWebformCodeMirror(&$element, FormStateInterface $form_state, &$complete_form) {

  // Check that mode is defined and valid, if not default to (plain) text.
  if (empty($element['#mode']) || !isset(static::$modes[$element['#mode']])) {
    $element['#mode'] = 'text';
  }

  // Check edit Twig template permission and complete disable editing.
  if ($element['#mode'] === 'twig') {
    if (!WebformTwigExtension::hasEditTwigAccess()) {
      $element['#disable'] = TRUE;
      $element['#attributes']['disabled'] = 'disabled';
      $element['#field_prefix'] = [
        '#type' => 'webform_message',
        '#message_type' => 'warning',
        '#message_message' => t("Only webform administrators and user's assigned the 'Edit webform Twig templates' permission are allowed to edit this Twig template."),
      ];
    }
  }

  // Set wrap off.
  if (empty($element['#wrap'])) {
    $element['#attributes']['wrap'] = 'off';
  }

  // Add validate callback.
  $element += [
    '#element_validate' => [],
  ];
  array_unshift($element['#element_validate'], [
    get_called_class(),
    'validateWebformCodeMirror',
  ]);
  return $element;
}