You are here

media_library_form_element.module in Media Library Form API Element 8

Same filename and directory in other branches
  1. 2.x media_library_form_element.module

The Media Library Form Element module file.

File

media_library_form_element.module
View source
<?php

/**
 * @file
 * The Media Library Form Element module file.
 */
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Element\RenderElement;
use Drupal\Core\Template\Attribute;

/**
 * Implements hook_theme().
 */
function media_library_form_element_theme($existing, $type, $theme, $path) {
  return [
    'media_library_element' => [
      'render element' => 'element',
    ],
  ];
}

/**
 * Prepares variables for media_library_element templates.
 *
 * Default template: media-library-element.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - element: An associative array containing the properties of the element.
 *     Properties used: #attributes.
 */
function template_preprocess_media_library_element(&$variables) {
  $element = $variables['element'];
  Element::setAttributes($element, [
    'id',
  ]);
  RenderElement::setAttributes($element);
  $variables['attributes'] = isset($element['#attributes']) ? $element['#attributes'] : [];
  $variables['prefix'] = isset($element['#field_prefix']) ? $element['#field_prefix'] : NULL;
  $variables['suffix'] = isset($element['#field_suffix']) ? $element['#field_suffix'] : NULL;
  $variables['title_display'] = isset($element['#title_display']) ? $element['#title_display'] : NULL;

  // $variables['children'] = $element['#children'];
  $variables['required'] = !empty($element['#required']) ? $element['#required'] : NULL;
  if (isset($element['#title']) && $element['#title'] !== '') {
    $variables['legend']['title'] = [
      '#markup' => $element['#title'],
    ];
  }
  $variables['legend']['attributes'] = new Attribute();

  // Add 'visually-hidden' class to legend span.
  if ($variables['title_display'] == 'invisible') {
    $variables['legend_span']['attributes'] = new Attribute([
      'class' => [
        'visually-hidden',
      ],
    ]);
  }
  else {
    $variables['legend_span']['attributes'] = new Attribute();
  }
  if (!empty($element['#description'])) {
    $description_id = $element['#attributes']['id'] . '--description';
    $description_attributes['id'] = $description_id;
    $variables['description']['attributes'] = new Attribute($description_attributes);
    $variables['description']['content'] = $element['#description'];

    // Add the description's id to the fieldset aria attributes.
    $variables['attributes']['aria-describedby'] = $description_id;
  }
  $variables['content'] = $element;

  // Suppress error messages.
  $variables['errors'] = NULL;
}

Functions

Namesort descending Description
media_library_form_element_theme Implements hook_theme().
template_preprocess_media_library_element Prepares variables for media_library_element templates.