You are here

class WebformElementAttributes in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Element/WebformElementAttributes.php \Drupal\webform\Element\WebformElementAttributes

Provides a webform element for element attributes.

Plugin annotation

@FormElement("webform_element_attributes");

Hierarchy

Expanded class hierarchy of WebformElementAttributes

11 #type uses of WebformElementAttributes
Range::form in src/Plugin/WebformElement/Range.php
Gets the actual configuration webform array to be built.
WebformActions::form in src/Plugin/WebformElement/WebformActions.php
Gets the actual configuration webform array to be built.
WebformElementBase::form in src/Plugin/WebformElementBase.php
Gets the actual configuration webform array to be built.
WebformEntityReferenceLinkFormatter::settingsForm in src/Plugin/Field/FieldFormatter/WebformEntityReferenceLinkFormatter.php
Returns a form to configure settings for the formatter.
WebformEntitySettingsConfirmationForm::form in src/EntitySettings/WebformEntitySettingsConfirmationForm.php
Gets the actual form array to be built.

... See full list

File

src/Element/WebformElementAttributes.php, line 16

Namespace

Drupal\webform\Element
View source
class WebformElementAttributes extends FormElement {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return [
      '#input' => TRUE,
      '#process' => [
        [
          $class,
          'processWebformElementAttributes',
        ],
      ],
      '#pre_render' => [
        [
          $class,
          'preRenderWebformElementAttributes',
        ],
      ],
      '#theme_wrappers' => [
        'container',
      ],
      '#classes' => '',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
    $element += [
      '#default_value' => [],
    ];
    $element['#default_value'] += [
      'class' => [],
      'style' => '',
    ];
    return NULL;
  }

  /**
   * Processes element attributes.
   */
  public static function processWebformElementAttributes(&$element, FormStateInterface $form_state, &$complete_form) {
    $element['#tree'] = TRUE;

    // Determine what type of HTML element the attributes are being applied to.
    $type = t('element');
    $types = [
      preg_quote(t('webform')),
      preg_quote(t('link')),
      preg_quote(t('button')),
    ];
    if (preg_match('/\\b(' . implode('|', $types) . ')\\b/i', $element['#title'], $match)) {
      $type = $match[1];
    }
    $t_args = [
      '@title' => $element['#title'],
      '@type' => mb_strtolower($type),
    ];

    // Class.
    $element['#classes'] = trim($element['#classes']);
    if ($element['#classes']) {
      $classes = preg_split('/\\r?\\n/', $element['#classes']);
      $element['class'] = [
        '#type' => 'webform_select_other',
        '#title' => t('@title CSS classes', $t_args),
        '#description' => t("Apply classes to the @type. Select 'custom…' to enter custom classes.", $t_args),
        '#multiple' => TRUE,
        '#options' => [
          WebformSelectOther::OTHER_OPTION => t('custom…'),
        ] + array_combine($classes, $classes),
        '#other__placeholder' => t('Enter custom classes…'),
        '#other__option_delimiter' => ' ',
        '#attributes' => [
          'class' => [
            'js-' . $element['#id'] . '-attributes-style',
          ],
        ],
        '#select2' => TRUE,
        '#default_value' => $element['#default_value']['class'],
      ];
      WebformElementHelper::process($element['class']);
    }
    else {
      $element['class'] = [
        '#type' => 'textfield',
        '#title' => t('@title CSS classes', $t_args),
        '#description' => t("Apply classes to the @type.", $t_args),
        '#default_value' => implode(' ', $element['#default_value']['class']),
      ];
    }

    // Custom options.
    $element['custom'] = [
      '#type' => 'texfield',
      '#placeholder' => t('Enter custom classes…'),
      '#states' => [
        'visible' => [
          'select.js-' . $element['#id'] . '-attributes-style' => [
            'value' => '_custom_',
          ],
        ],
      ],
      '#error_no_message' => TRUE,
      '#default_value' => '',
    ];

    // Style.
    $element['style'] = [
      '#type' => 'textfield',
      '#title' => t('@title CSS style', $t_args),
      '#description' => t('Apply custom styles to the @type.', $t_args),
      '#default_value' => $element['#default_value']['style'],
    ];

    // Attributes.
    $attributes = $element['#default_value'];
    unset($attributes['class'], $attributes['style']);
    $element['attributes'] = [
      '#type' => 'webform_codemirror',
      '#mode' => 'yaml',
      '#title' => t('@title custom attributes (YAML)', $t_args),
      '#description' => t('Enter additional attributes to be added the @type.', $t_args),
      '#attributes__access' => !\Drupal::moduleHandler()
        ->moduleExists('webform_ui') || \Drupal::currentUser()
        ->hasPermission('edit webform source'),
      '#default_value' => WebformYaml::encode($attributes),
    ];

    // Apply custom properties. Typically used for descriptions.
    foreach ($element as $key => $value) {
      if (strpos($key, '__') !== FALSE) {
        list($element_key, $property_key) = explode('__', ltrim($key, '#'));
        $element[$element_key]["#{$property_key}"] = $value;
      }
    }

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

  /**
   * Validates element attributes.
   */
  public static function validateWebformElementAttributes(&$element, FormStateInterface $form_state, &$complete_form) {
    $values = $element['#value'];
    $attributes = [];
    if ($values['class']) {
      if (isset($element['class']['select'])) {
        $class = $element['class']['select']['#value'];
        $class_other = $element['class']['other']['#value'];
        if (isset($class[WebformSelectOther::OTHER_OPTION])) {
          unset($class[WebformSelectOther::OTHER_OPTION]);
          $class[$class_other] = $class_other;
        }
        if ($class) {
          $attributes['class'] = array_values($class);
        }
      }
      else {
        $attributes['class'] = [
          $values['class'],
        ];
      }
    }
    if ($values['style']) {
      $attributes['style'] = $values['style'];
    }
    if (!empty($values['attributes'])) {
      $attributes += Yaml::decode($values['attributes']);
    }
    $form_state
      ->setValueForElement($element['class'], NULL);
    $form_state
      ->setValueForElement($element['style'], NULL);
    $form_state
      ->setValueForElement($element['attributes'], NULL);
    $element['#value'] = $attributes;
    $form_state
      ->setValueForElement($element, $attributes);
  }

  /**
   * Prepares a #type 'webform_element_attributes' render element.
   *
   * @param array $element
   *   An associative array containing the properties of the element.
   *
   * @return array
   *   The $element.
   */
  public static function preRenderWebformElementAttributes($element) {
    static::setAttributes($element, [
      'webform-element-attributes',
    ]);
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormElement::processAutocomplete public static function Adds autocomplete functionality to elements.
FormElement::processPattern public static function #process callback for #pattern form element property.
FormElement::validatePattern public static function #element_validate callback for #pattern form element property.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WebformElementAttributes::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
WebformElementAttributes::preRenderWebformElementAttributes public static function Prepares a #type 'webform_element_attributes' render element.
WebformElementAttributes::processWebformElementAttributes public static function Processes element attributes.
WebformElementAttributes::validateWebformElementAttributes public static function Validates element attributes.
WebformElementAttributes::valueCallback public static function Determines how user input is mapped to an element's #value property. Overrides FormElement::valueCallback