You are here

class YamlFormElementAttributes in YAML Form 8

Provides a form element for element attributes.

Plugin annotation

@FormElement("yamlform_element_attributes");

Hierarchy

Expanded class hierarchy of YamlFormElementAttributes

2 #type uses of YamlFormElementAttributes
YamlFormElementBase::form in src/YamlFormElementBase.php
Gets the actual configuration form array to be built.
YamlFormEntitySettingsForm::form in src/YamlFormEntitySettingsForm.php
Gets the actual form array to be built.

File

src/Element/YamlFormElementAttributes.php, line 16

Namespace

Drupal\yamlform\Element
View source
class YamlFormElementAttributes extends FormElement {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return [
      '#input' => TRUE,
      '#process' => [
        [
          $class,
          'processYamlFormElementAttributes',
        ],
      ],
      '#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 processYamlFormElementAttributes(&$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('form')),
      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' => Unicode::strtolower($type),
    ];

    // Class.
    $element['#classes'] = trim($element['#classes']);
    if ($element['#classes']) {
      $classes = preg_split('/\\r?\\n/', $element['#classes']);
      $element['class'] = [
        '#type' => 'yamlform_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' => [
          YamlFormSelectOther::OTHER_OPTION => t('custom...'),
        ] + array_combine($classes, $classes),
        '#other__option_delimiter' => ' ',
        '#attributes' => [
          'class' => [
            'js-yamlform-select2',
            'yamlform-select2',
            'js-' . $element['#id'] . '-attributes-style',
          ],
        ],
        '#attached' => [
          'library' => [
            'yamlform/yamlform.element.select2',
          ],
        ],
        '#default_value' => $element['#default_value']['class'],
      ];

      // ISSUE:
      // Nested element with #element_validate callback that alter an
      // element's value can break the returned value.
      //
      // WORKAROUND:
      // Manually process the 'yamlform_select_other' element.
      $element['class'] = YamlFormSelectOther::valueCallback($element['class'], FALSE, $form_state);
      $element['class'] = YamlFormSelectOther::processYamlFormOther($element['class'], $form_state, $complete_form);
      $element['class']['#type'] = 'item';
      unset($element['class']['#element_validate']);
    }
    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' => 'yamlform_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('yamlform_ui') || \Drupal::currentUser()
        ->hasPermission('edit yamlform source'),
      '#default_value' => YamlFormTidy::tidy(Yaml::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;
      }
    }
    $element['#element_validate'] = [
      [
        get_called_class(),
        'validateYamlFormElementAttributes',
      ],
    ];
    return $element;
  }

  /**
   * Validates element attributes.
   */
  public static function validateYamlFormElementAttributes(&$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[YamlFormSelectOther::OTHER_OPTION])) {
          unset($class[YamlFormSelectOther::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);
    $form_state
      ->setValueForElement($element, $attributes);
  }

}

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.
YamlFormElementAttributes::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
YamlFormElementAttributes::processYamlFormElementAttributes public static function Processes element attributes.
YamlFormElementAttributes::validateYamlFormElementAttributes public static function Validates element attributes.
YamlFormElementAttributes::valueCallback public static function Determines how user input is mapped to an element's #value property. Overrides FormElement::valueCallback