function Image::getMetadata

Same name in other branches
  1. 9 core/modules/media/src/Plugin/media/Source/Image.php \Drupal\media\Plugin\media\Source\Image::getMetadata()
  2. 8.9.x core/modules/media/src/Plugin/media/Source/Image.php \Drupal\media\Plugin\media\Source\Image::getMetadata()
  3. 11.x core/modules/media/src/Plugin/media/Source/Image.php \Drupal\media\Plugin\media\Source\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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.