You are here

public function Pinterest::getMetadata in Media entity Pinterest 8.2

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 MediaSourceBase::getMetadata

File

src/Plugin/media/Source/Pinterest.php, line 63

Class

Pinterest
Provides media type plugin for Pinterest.

Namespace

Drupal\media_entity_pinterest\Plugin\media\Source

Code

public function getMetadata(MediaInterface $media, $name) {
  $matches = $this
    ->matchRegexp($media);
  if (empty($matches)) {
    return NULL;
  }

  // First we return the fields that are available from regex.
  switch ($name) {
    case 'thumbnail_uri':
      if ($local_image = $this
        ->getMetadata($media, 'thumbnail_local')) {
        return $local_image;
      }
      return parent::getMetadata($media, 'thumbnail_uri');
    case 'default_name':
      $id = $this
        ->getMetadata($media, 'id');
      $board = $this
        ->getMetadata($media, 'board');
      $section = $this
        ->getMetadata($media, 'section');
      $user = $this
        ->getMetadata($media, 'user');

      // The default name will be the Pin ID for Pins.
      if (!empty($id)) {
        return $id;
      }

      // The default name will be the username, board slug, and section
      // for Sections.
      if (!empty($user) && !empty($board) && !empty($section)) {
        return $user . ' - ' . $board . ' - ' . $section;
      }

      // The default name will be the username and board slug for Boards.
      if (!empty($user) && !empty($board)) {
        return $user . ' - ' . $board;
      }

      // The default name will be the username for Profiles.
      if (!empty($user) && empty($board)) {
        return $user;
      }
      return parent::getMetadata($media, 'default_name');
    case 'id':
      if (!empty($matches['id'])) {
        return $matches['id'];
      }
      return NULL;
    case 'section':
      if (!empty($matches['section'])) {
        return $matches['section'];
      }
      return NULL;
    case 'board':
      if (!empty($matches['slug'])) {
        return $matches['slug'];
      }
      return NULL;
    case 'user':
      if (!empty($matches['username'])) {
        return $matches['username'];
      }
      return NULL;
    default:
      return parent::getMetadata($media, $name);
  }
}