You are here

function geofield_map_preprocess_image_widget in Geofield Map 8.2

Implements hook_preprocess_HOOK().

Drupal 8, add an image field from a BuildForm with preview https://stackoverflow.com/questions/34100546/drupal-8-add-an-image-field...

File

./geofield_map.module, line 124
Contains the geofield_map.module.

Code

function geofield_map_preprocess_image_widget(&$variables) {
  $element = $variables['element'];

  // Check and initial setup for SVG file support.
  $svg_image_support = \Drupal::service('module_handler')
    ->moduleExists('svg_image');
  $svg_image_is_file_svg = FALSE;

  // Only act when it is geofield_map_marker_icon_upload case.
  // @see: MarkerIconService->getIconFileManagedElement().
  if (!empty($element['#geofield_map_marker_icon_upload'])) {
    $variables['attributes'] = [
      'class' => [
        'image-widget',
        'js-form-managed-file',
        'form-managed-file',
        'clearfix',
      ],
    ];
    if (!empty($element['fids']['#value'])) {
      $file = reset($element['#files']);
      $element['file_' . $file
        ->id()]['filename']['#suffix'] = ' <span class="file-size">(' . format_size($file
        ->getSize()) . ')</span> ';
      $file_variables = [
        'style_name' => $element['#preview_image_style'],
        'uri' => $file
          ->getFileUri(),
      ];

      /* @var \Drupal\Core\Image\Image $image */
      $image = \Drupal::service('image.factory')
        ->get($file
        ->getFileUri());
      $file_is_manageable = FALSE;
      $file_width = $file_height = 1;
      if ($image
        ->isValid()) {
        $file_is_manageable = TRUE;
        $file_width = $image
          ->getWidth();
        $file_height = $image
          ->getHeight();
      }
      if ($svg_image_support && ($svg_image_is_file_svg = svg_image_is_file_svg($file))) {
        $file_is_manageable = TRUE;
        $file_variables = array_merge(svg_image_get_image_file_dimensions($file), $file_variables);
        $file_width = $file_variables['width'];
        $file_height = $file_variables['height'];
      }
      if ($file_is_manageable) {
        $style = ImageStyle::load($element['#preview_image_style']);
        $image_preview_width = 44;
        $image_preview_height = $image_preview_width * $file_width / $file_height;
        $element['preview'] = [
          '#weight' => -10,
          '#theme' => 'image_style',
          '#width' => $image_preview_width,
          '#height' => $image_preview_height,
          '#style_name' => $file_variables['style_name'],
          '#uri' => $file_variables['uri'],
        ];
        if (!isset($style)) {

          // Inform the site builders why their image didn't work.
          $element['preview']['#theme'] = 'image';
          unset($element['preview']['#style_name']);
          \Drupal::logger('image')
            ->warning('Image style (@style) missing for @image. Please add the missing style under /admin/config/media/image-styles.', [
            '@style' => $file_variables['style_name'],
            '@image' => $file_variables['uri'],
          ]);
        }

        // Manage SVG file Preview.
        if ($svg_image_support && $svg_image_is_file_svg) {
          $element['preview']['#theme'] = 'image';
          unset($element['preview']['#style_name']);

          // Tag the element as SVG.
          $element['#svg'] = TRUE;
        }

        // Store the dimensions in the form so the file doesn't have to be
        // accessed again. This is important for remote files.
        $element['width'] = [
          '#type' => 'hidden',
          '#value' => $image_preview_width,
        ];
        $element['height'] = [
          '#type' => 'hidden',
          '#value' => $image_preview_height,
        ];
      }
    }
    $variables['data'] = [];
    foreach (Element::children($element) as $child) {
      $variables['data'][$child] = $element[$child];
    }
  }
}