Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Image.php

function Image::getMetadata

Overrides File::getMetadata

File

core/modules/media/src/Plugin/media/Source/Image.php, line 124

Class

Image
Image entity media source.

Namespace

Drupal\media\Plugin\media\Source

Code

public function getMetadata(MediaInterface $media, $name) {
    // Get the file and image data.
    
    /** @var \Drupal\file\FileInterface $file */
    $file = $media->get($this->configuration['source_field'])->entity;
    // If the source field is not required, it may be empty.
    if (!$file) {
        return parent::getMetadata($media, $name);
    }
    $uri = $file->getFileUri();
    switch ($name) {
        case static::METADATA_ATTRIBUTE_WIDTH:
            $image = $this->imageFactory
                ->get($uri);
            return $image->getWidth() ?: NULL;
        case static::METADATA_ATTRIBUTE_HEIGHT:
            $image = $this->imageFactory
                ->get($uri);
            return $image->getHeight() ?: NULL;
        case 'thumbnail_uri':
            return $uri;
        case 'thumbnail_alt_value':
            return $media->get($this->configuration['source_field'])->alt ?? parent::getMetadata($media, $name);
    }
    return parent::getMetadata($media, $name);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal