If I use the width and/or height dimension views fields, instead of showing the width and height of the image (in text) I instead get the full image.

Steps to reproduce:

1) Install media_entity_image and it's dependencies
2) Create image media bundle
3) Add image field
4) Add a image entity
5) Create a view, showing media entities
6) Include a field, either Media: Thumbnail (height) or Media: Thumbnail (width)

You will notice the field formatter in use is for the image field, and not that of a text field.

Expected result:
I should get the image dimensions, in text

Actual result:
The actual image is displayed.

CommentFileSizeAuthor
#4 Untitled.pdf117.85 KBAllie Micka
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Leon Kessler created an issue. See original summary.

Dippers’s picture

I needed the image height and width in a view and needed to add the following code to expose them.

/**
 * Implements hook_views_data_alter().
 * 
 * Expose image height and width to views
 */
function hook_views_data_alter(&$data) {
  $field_manager = \Drupal::service('entity_field.manager');
  $fieldsArray = $field_manager->getFieldMapByFieldType('image');
  foreach ($fieldsArray as $entity_type => $fields) {
    foreach ($fields as $field_name => $field) {
      $field_storage = \Drupal\field\Entity\FieldStorageConfig::loadByName($entity_type, $field_name);
      if (!empty($field_storage)) {
        $views_data = views_field_default_views_data($field_storage);
        foreach ($views_data as $table_name => $table_data) {
          if (isset($data[$table_name])) {
            $group = $data[$table_name][$field_storage->getName() . '_target_id']['group'];
            $data[$table_name][$field_storage->getName() . '_width'] = [
              'title' => t('Image width'),
              'group' => $group,  
              'field' => ['id'  => 'numeric'],
            ];
            $data[$table_name][$field_storage->getName() . '_height'] = [
              'title' => t('Image height'),
              'group' => $group,  
              'field' => ['id' => 'numeric'],
            ];
          }
        }
      }
    }
  }
}
SebaZ’s picture

I can confirm that it still works wrong as described in issue, Drupal 8.9 core Media module, not only this contrib.

Allie Micka’s picture

FileSize
117.85 KB

While not ideal, you can use the thumbnail height/width fields and access the raw height/width values as tokens. To output this information, go to 'Rewrite results' in the field settings and add the token.

vlad.dancer’s picture

Thanks @Dippers, works even in 2023!

I've used this hook for paragraph image field. I think this issue should be moved into core.

vamirbekyan’s picture

this method outputs width and height of the original image and not of the selected thumbnail selected in the Formatter.
Anyone knows how to output the selected thumbnail height and width?