function PreprocessHooks::fileAndImageWidget

Same name and namespace in other branches
  1. 11.x core/themes/admin/src/Hook/PreprocessHooks.php \Drupal\admin\Hook\PreprocessHooks::fileAndImageWidget()

Helper pre-process callback for file_managed_file and image_widget.

Parameters

array $variables: The renderable array of image and file widgets, with 'element' and 'data' keys.

File

core/themes/admin/src/Hook/PreprocessHooks.php, line 1448

Class

PreprocessHooks
Provides preprocess implementations.

Namespace

Drupal\admin\Hook

Code

private function fileAndImageWidget(array &$variables) : void {
  $element = $variables['element'];
  $main_item_keys = [
    'upload',
    'upload_button',
    'remove_button',
  ];
  // Calculate helper values for the template.
  $upload_is_accessible = !isset($element['upload']['#access']) || $element['upload']['#access'] !== FALSE;
  $is_multiple = !empty($element['#cardinality']) && $element['#cardinality'] !== 1;
  $has_value = !empty($element['#value']['fids']);
  // File widget properties.
  $display_can_be_displayed = !empty($element['#display_field']);
  // Display is rendered in a separate table cell for multiple value widgets.
  $display_is_visible = $display_can_be_displayed && !$is_multiple && isset($element['display']['#type']) && $element['display']['#type'] !== 'hidden';
  $description_can_be_displayed = !empty($element['#description_field']);
  $description_is_visible = $description_can_be_displayed && isset($element['description']);
  // Image widget properties.
  $alt_can_be_displayed = !empty($element['#alt_field']);
  $alt_is_visible = $alt_can_be_displayed && (!isset($element['alt']['#access']) || $element['alt']['#access'] !== FALSE);
  $title_can_be_displayed = !empty($element['#title_field']);
  $title_is_visible = $title_can_be_displayed && (!isset($element['title']['#access']) || $element['title']['#access'] !== FALSE);
  $variables['multiple'] = $is_multiple;
  $variables['upload'] = $upload_is_accessible;
  $variables['has_value'] = $has_value;
  $variables['has_meta'] = $alt_is_visible || $title_is_visible || $display_is_visible || $description_is_visible;
  $variables['display'] = $display_is_visible;
  // Handle the default checkbox display after the file is uploaded.
  if (array_key_exists('display', $element)) {
    $variables['data']['display']['#checked'] = $element['display']['#value'];
  }
  // Render file upload input and upload button (or file name and remove
  // button, if the field is not empty) in an emphasized div.
  foreach ($variables['data'] as $key => $item) {
    $item_is_filename = isset($item['filename']['#file']) && $item['filename']['#file'] instanceof FileInterface;
    // Move filename to main items.
    if ($item_is_filename) {
      $variables['main_items']['filename'] = $item;
      unset($variables['data'][$key]);
      continue;
    }
    // Move buttons, upload input and hidden items to main items.
    if (in_array($key, $main_item_keys, TRUE)) {
      $variables['main_items'][$key] = $item;
      unset($variables['data'][$key]);
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.