You are here

function template_preprocess_fine_image_widget in Fine Image Upload 8

Same name and namespace in other branches
  1. 8.2 fiu.field.inc \template_preprocess_fine_image_widget()

Prepares variables for fine image widget templates.

Default template: fine-image-widget.html.twig.

Parameters

array $variables: An associative array containing:

  • element: A render element representing the image field widget.

File

./fiu.field.inc, line 19
Implement a fine image field, based on the file module's file field.

Code

function template_preprocess_fine_image_widget(array &$variables) {
  $element = $variables['element'];
  $variables['attributes'] = [
    'class' => [
      'fine-image-widget',
      'js-form-managed-file',
      'form-managed-file',
      'clearfix',
    ],
  ];
  $variables['data'] = [];
  foreach (Element::children($element) as $child) {
    $variables['data'][$child] = $element[$child];
    if ($child == 'alt' || $child == 'title') {
      $variables['data'][$child]['#attributes']['data-item-number'] = $element['#delta'];
    }

    // Remove file name.
    if (strpos($child, 'file_') !== FALSE && isset($variables['data'][$child]['filename'])) {
      $fileData = $variables['data'][$child]['filename'];
      if ($file = $fileData['#file']) {
        $variables['info']['mime'] = [
          'title' => t('Image mime type'),
          'value' => $file
            ->getMimeType(),
        ];
        $variables['info']['size'] = [
          'title' => t('Image size'),
          'value' => $file
            ->getSize(),
        ];
        $variables['info']['name'] = [
          'title' => t('Image fine name'),
          'value' => $file
            ->getFilename(),
        ];
        $variables['info']['url'] = [
          'title' => t('Image url'),
          'value' => $file
            ->url(),
        ];
      }
      unset($variables['data'][$child]['filename']);
    }
  }
  if (isset($variables['data']['width']['#value'])) {
    $variables['info']['width'] = [
      'title' => t('Image width'),
      'value' => $variables['data']['width']['#value'],
    ];
  }
  if (isset($variables['data']['height']['#value'])) {
    $variables['info']['height'] = [
      'title' => t('Image height'),
      'value' => $variables['data']['height']['#value'],
    ];
  }
  if (!empty($element['#errors']) && !empty($element['#value']['fids'])) {
    $variables['attributes']['class'][] = 'fiu-broken-file';
    unset($variables['info']);
  }
  if ($element['#cardinality'] == 1 && !empty($variables['element']['#value']['fids'])) {
    $variables['attributes']['class'][] = 'fiu-single-item';
  }

  // Add details button.
  if (!empty($variables['element']['#value']['fids'])) {
    $variables['details'] = t('details');
  }
}