public function Twitter::getMetadata in Media entity Twitter 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/ Twitter.php, line 172
Class
- Twitter entity media source.
Namespace
Drupal\media_entity_twitter\Plugin\media\SourceCode
public function getMetadata(MediaInterface $media, $attribute_name) {
$matches = $this
->matchRegexp($media);
if (!is_array($matches) || empty($matches['id'])) {
return NULL;
}
// First we return the fields that are available from regex.
switch ($attribute_name) {
case 'id':
return $matches['id'];
case 'user':
return $matches['user'] ?: NULL;
case 'thumbnail_uri':
// If there's already a local image, use it.
if ($local_image = $this
->getMetadata($media, 'image_local')) {
return $local_image;
}
// If thumbnail generation is disabled, use the default thumbnail.
if (empty($this->configuration['generate_thumbnails'])) {
return parent::getMetadata($media, $attribute_name);
}
// We might need to generate a thumbnail...
$id = $this
->getMetadata($media, 'id');
$thumbnail_uri = $this
->getLocalImageUri($id, $media);
// ...unless we already have, in which case, use it.
if (file_exists($thumbnail_uri)) {
return $thumbnail_uri;
}
// Render the thumbnail SVG using the theme system.
$thumbnail = [
'#theme' => 'media_entity_twitter_tweet_thumbnail',
'#content' => $this
->getMetadata($media, 'content'),
'#author' => $this
->getMetadata($media, 'user'),
'#avatar' => $this
->getMetadata($media, 'profile_image_url_https'),
];
$svg = $this->renderer
->renderRoot($thumbnail);
return $this->fileSystem
->saveData($svg, $thumbnail_uri, FileSystemInterface::EXISTS_ERROR) ?: parent::getMetadata($media, $attribute_name);
}
// If we have auth settings return the other fields.
if ($this->configuration['use_twitter_api'] && ($tweet = $this
->fetchTweet($matches['id']))) {
switch ($attribute_name) {
case 'image':
if (isset($tweet['extended_entities']['media'][0]['media_url'])) {
return $tweet['extended_entities']['media'][0]['media_url'];
}
return NULL;
case 'image_local':
$local_uri = $this
->getMetadata($media, 'image_local_uri');
if ($local_uri) {
if (file_exists($local_uri)) {
return $local_uri;
}
else {
$image_url = $this
->getMetadata($media, 'image');
// @TODO: Use Guzzle, possibly in a service, for this.
$image_data = file_get_contents($image_url);
if ($image_data) {
return $this->fileSystem
->saveData($image_data, $local_uri, FileSystemInterface::EXISTS_REPLACE);
}
}
}
return NULL;
case 'image_local_uri':
$image_url = $this
->getMetadata($media, 'image');
if ($image_url) {
return $this
->getLocalImageUri($matches['id'], $media, $image_url);
}
return NULL;
case 'content':
if (isset($tweet['full_text'])) {
return $tweet['full_text'];
}
return NULL;
case 'retweet_count':
if (isset($tweet['retweet_count'])) {
return $tweet['retweet_count'];
}
return NULL;
case 'profile_image_url_https':
if (isset($tweet['user']['profile_image_url_https'])) {
return $tweet['user']['profile_image_url_https'];
}
return NULL;
case 'created_time':
if (isset($tweet['created_at'])) {
if ($datetime = DrupalDateTime::createFromFormat('D M d H:i:s O Y', $tweet['created_at'])) {
return $datetime
->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
}
}
return NULL;
case 'user_name':
if (isset($tweet['user']['name'])) {
return $tweet['user']['name'];
}
return NULL;
case 'default_name':
$user = $this
->getMetadata($media, 'user');
$id = $this
->getMetadata($media, 'id');
if (!empty($user) && !empty($id)) {
return $user . ' - ' . $id;
}
return NULL;
}
}
return NULL;
}