You are here

public static function ClaroPreRender::managedFile in Drupal 8

Same name and namespace in other branches
  1. 9 core/themes/claro/src/ClaroPreRender.php \Drupal\claro\ClaroPreRender::managedFile()

Prerender callback for managed_file.

File

core/themes/claro/src/ClaroPreRender.php, line 18

Class

ClaroPreRender
Implements trusted prerender callbacks for the Claro theme.

Namespace

Drupal\claro

Code

public static function managedFile($element) {
  if (!empty($element['remove_button']) && is_array($element['remove_button'])) {
    $element['remove_button']['#attributes']['class'][] = 'button--extrasmall';
    $element['remove_button']['#attributes']['class'][] = 'remove-button';
  }
  if (!empty($element['upload_button']) && is_array($element['upload_button'])) {
    $element['upload_button']['#attributes']['class'][] = 'upload-button';
  }

  // Wrap single-cardinality widgets with a details element.
  $single_file_widget = !empty($element['#cardinality']) && $element['#cardinality'] === 1;
  if ($single_file_widget && empty($element['#single_wrapped'])) {
    $element['#theme_wrappers']['details'] = [
      '#title' => $element['#title'],
      '#summary_attributes' => [],
      '#attributes' => [
        'open' => TRUE,
      ],
      '#value' => NULL,
      // The description of the single cardinality file widgets will be
      // displayed by the managed file widget.
      '#description' => NULL,
      '#required' => $element['#required'],
      '#errors' => NULL,
      '#disabled' => !empty($element['#disabled']),
    ];
    $element['#single_wrapped'] = TRUE;
    $upload_is_accessible = empty($element['#default_value']['fids']) && (!isset($element['upload']['#access']) || $element['upload']['#access'] !== FALSE);
    if ($upload_is_accessible) {

      // Change widget title. This is the same title that is used by the
      // multiple file widget.
      // @see https://git.drupalcode.org/project/drupal/blob/ade7b950a1/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php#L192
      $element['#title'] = t('Add a new file');
    }
    else {

      // If the field has a value, the file upload title doesn't have to be
      // visible because the wrapper element will have the same title as the
      // managed file widget. The title is kept in the markup as visually
      // hidden for accessibility.
      $element['#title_display'] = 'invisible';
    }
  }
  return $element;
}