Skip to content

Instantly share code, notes, and snippets.

@nonom
Created June 17, 2022 06:39
Show Gist options
  • Save nonom/f8167843984f6ab3c6ae3bab39206b77 to your computer and use it in GitHub Desktop.
Save nonom/f8167843984f6ab3c6ae3bab39206b77 to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\ema_document\Plugin\media\Source;
use Drupal\media\MediaInterface;
use Drupal\media\MediaSourceBase;
/**
* EMA file entity media source.
*
* @see \Drupal\file\FileInterface
*
* @MediaSource(
* id = "ema_document_file",
* label = @Translation("EMA Document File"),
* description = @Translation("Embed EMA document files."),
* allowed_field_types = {"string_long"}
* )
*/
class EmaDocumentFile extends MediaSourceBase {
/**
* {@inheritdoc}
*/
public function getMetadataAttributes() {
return [
'title' => $this->t('Resource Title'),
'published' => $this->t('Published date'),
'updated' => $this->t('Updated date'),
];
}
/**
* {@inheritdoc}
*/
public function getMetadata(MediaInterface $media, $attribute_name) {
// Get the text_long field where the JSON object is stored.
$remote_field = $media->get($this->configuration['source_field']);
$json_arr = json_decode($remote_field->value);
// If the source field is not required, it may be empty.
if (!$remote_field) {
return parent::getMetadata($media, $attribute_name);
}
return $json_arr->$attribute_name ?? parent::getMetadata($media, $attribute_name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment