You are here

public function Image::getMetadata in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/media/src/Plugin/media/Source/Image.php \Drupal\media\Plugin\media\Source\Image::getMetadata()
  2. 9 core/modules/media/src/Plugin/media/Source/Image.php \Drupal\media\Plugin\media\Source\Image::getMetadata()

Gets the value for a metadata attribute for a given media item.

Parameters

\Drupal\media\MediaInterface $media: A media item.

string $attribute_name: Name of the attribute to fetch.

Return value

mixed|null Metadata attribute value or NULL if unavailable.

Overrides File::getMetadata

File

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

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);
}