You are here

public static function FormTrait::addDataAttributes in Markdown 8.2

Adds multiple data attributes to an element.

Parameters

array $element: An element, passed by reference.

array $data: The data attributes to add.

2 calls to FormTrait::addDataAttributes()
FormTrait::addDataAttribute in src/Traits/FormTrait.php
Adds a data attribute to an element.
FormTrait::createElement in src/Traits/FormTrait.php
Creates an element, adding data attributes to it if necessary.

File

src/Traits/FormTrait.php, line 77

Class

FormTrait
Trait providing helpful methods when dealing with forms.

Namespace

Drupal\markdown\Traits

Code

public static function addDataAttributes(array &$element, array $data) {
  $converter = new CamelCaseToSnakeCaseNameConverter();
  foreach ($data as $name => $value) {
    $name = str_replace('_', '-', $converter
      ->normalize(preg_replace('/^data-?/', '', $name)));
    $element['#attributes']['data-' . $name] = is_string($value) || $value instanceof MarkupInterface ? (string) $value : Json::encode($value);
  }
}