media_library_form_element-2.0.0/media_library_form_element.module
media_library_form_element.module
<?php
/**
* @file
* The Media Library Form Element module file.
*/
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Element\RenderElementBase;
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']);
RenderElementBase::setAttributes($element);
$variables['attributes'] = $element['#attributes'] ?? [];
$variables['prefix'] = $element['#field_prefix'] ?? NULL;
$variables['suffix'] = $element['#field_suffix'] ?? NULL;
$variables['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;
}
