You are here

function _claro_preprocess_file_and_image_widget in Drupal 9

Same name and namespace in other branches
  1. 8 core/themes/claro/claro.theme \_claro_preprocess_file_and_image_widget()

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.

2 calls to _claro_preprocess_file_and_image_widget()
claro_preprocess_file_managed_file in core/themes/claro/claro.theme
Implements hook_preprocess_HOOK() for file_managed_file.
claro_preprocess_image_widget in core/themes/claro/claro.theme
Implements hook_preprocess_HOOK() for image_widget.

File

core/themes/claro/claro.theme, line 1249
Functions to support theming in the Claro theme.

Code

function _claro_preprocess_file_and_image_widget(array &$variables) {
  $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 = isset($element['#value']['fids']) && !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;

  // 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)) {
      $variables['main_items'][$key] = $item;
      unset($variables['data'][$key]);
    }
  }
}