You are here

class ThunderMediaSchemaExtension in Thunder 6.2.x

The media schema extension.

Plugin annotation


@SchemaExtension(
  id = "thunder_media",
  name = "Media extension",
  description = "Adds media entities and their fields.",
  schema = "thunder"
)

Hierarchy

Expanded class hierarchy of ThunderMediaSchemaExtension

File

modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderMediaSchemaExtension.php, line 20

Namespace

Drupal\thunder_gqls\Plugin\GraphQL\SchemaExtension
View source
class ThunderMediaSchemaExtension extends ThunderSchemaExtensionPluginBase {

  /**
   * {@inheritdoc}
   */
  public function registerResolvers(ResolverRegistryInterface $registry) {
    parent::registerResolvers($registry);
    $this->registry
      ->addTypeResolver('Media', \Closure::fromCallable([
      __CLASS__,
      'resolveMediaTypes',
    ]));
    $this
      ->resolveFields();
  }

  /**
   * Add image media field resolvers.
   */
  protected function resolveFields() {

    // Image.
    $this
      ->resolveMediaInterfaceFields('MediaImage');
    $this
      ->addFieldResolverIfNotExists('MediaImage', 'copyright', $this->builder
      ->fromPath('entity', 'field_copyright.value'));
    $this
      ->addFieldResolverIfNotExists('MediaImage', 'description', $this->builder
      ->fromPath('entity', 'field_description.processed'));
    $this
      ->addFieldResolverIfNotExists('MediaImage', 'src', $this->builder
      ->compose($this->builder
      ->fromPath('entity', 'field_image.entity'), $this->builder
      ->produce('image_url')
      ->map('entity', $this->builder
      ->fromParent())));
    $this
      ->addFieldResolverIfNotExists('MediaImage', 'derivative', $this->builder
      ->compose($this->builder
      ->fromPath('entity', 'field_image.entity'), $this->builder
      ->produce('image_derivative')
      ->map('entity', $this->builder
      ->fromParent())
      ->map('style', $this->builder
      ->fromArgument('style')), $this->builder
      ->callback(function ($values) {
      if (!empty($values['url'])) {
        return $values + [
          'src' => $values['url'],
        ];
      }
      return;
    })));
    $this
      ->addFieldResolverIfNotExists('MediaImage', 'focalPoint', $this->builder
      ->compose($this->builder
      ->fromPath('entity', 'field_image.entity'), $this->builder
      ->produce('focal_point')
      ->map('file', $this->builder
      ->fromParent())));
    $this
      ->addFieldResolverIfNotExists('MediaImage', 'width', $this->builder
      ->fromPath('entity', 'field_image.width'));
    $this
      ->addFieldResolverIfNotExists('MediaImage', 'height', $this->builder
      ->fromPath('entity', 'field_image.height'));
    $this
      ->addFieldResolverIfNotExists('MediaImage', 'title', $this->builder
      ->fromPath('entity', 'field_image.title'));
    $this
      ->addFieldResolverIfNotExists('MediaImage', 'alt', $this->builder
      ->fromPath('entity', 'field_image.alt'));
    $this
      ->addFieldResolverIfNotExists('MediaImage', 'tags', $this
      ->fromEntityReference('field_tags'));
    $this
      ->addFieldResolverIfNotExists('MediaImage', 'source', $this->builder
      ->fromPath('entity', 'field_source.value'));

    // Video.
    $this
      ->resolveMediaInterfaceFields('MediaVideo');
    $this
      ->addFieldResolverIfNotExists('MediaVideo', 'src', $this->builder
      ->fromPath('entity', 'field_media_video_embed_field.value'));
    $this
      ->addFieldResolverIfNotExists('MediaVideo', 'username', $this->builder
      ->fromPath('entity', 'field_author.value'));
    $this
      ->addFieldResolverIfNotExists('MediaVideo', 'caption', $this->builder
      ->fromPath('entity', 'field_caption.processed'));
    $this
      ->addFieldResolverIfNotExists('MediaVideo', 'copyright', $this->builder
      ->fromPath('entity', 'field_copyright.value'));
    $this
      ->addFieldResolverIfNotExists('MediaVideo', 'description', $this->builder
      ->fromPath('entity', 'field_description.processed'));
    $this
      ->addFieldResolverIfNotExists('MediaVideo', 'source', $this->builder
      ->fromPath('entity', 'field_source.value'));
  }

  /**
   * Resolves media types.
   *
   * @param mixed $value
   *   The current value.
   * @param \Drupal\graphql\GraphQL\Execution\ResolveContext $context
   *   The resolve context.
   * @param \GraphQL\Type\Definition\ResolveInfo $info
   *   The resolve information.
   *
   * @return string
   *   Response type.
   *
   * @throws \Exception
   */
  protected function resolveMediaTypes($value, ResolveContext $context, ResolveInfo $info) : string {
    if ($value instanceof MediaInterface) {
      return 'Media' . $this
        ->mapBundleToSchemaName($value
        ->bundle());
    }
    throw new \Exception('Invalid media type.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ResolverHelperTrait::$builder protected property ResolverBuilder.
ResolverHelperTrait::$registry protected property ResolverRegistryInterface.
ResolverHelperTrait::addFieldResolverIfNotExists protected function Add field resolver to registry, if it does not already exist.
ResolverHelperTrait::addSimpleCallbackFields public function Define callback field resolver for a type.
ResolverHelperTrait::createResolverBuilder protected function Create the ResolverBuilder.
ResolverHelperTrait::fromEntityReference public function Produces an entity_reference field.
ResolverHelperTrait::fromEntityReferenceRevisions public function Produces an entity_reference_revisions field.
ResolverHelperTrait::fromRoute public function Produces an entity from a given path.
ThunderMediaSchemaExtension::registerResolvers public function Overrides ThunderSchemaExtensionPluginBase::registerResolvers
ThunderMediaSchemaExtension::resolveFields protected function Add image media field resolvers.
ThunderMediaSchemaExtension::resolveMediaTypes protected function Resolves media types.
ThunderSchemaExtensionPluginBase::$dataProducerManager protected property The data producer plugin manager.
ThunderSchemaExtensionPluginBase::$entityTypeManager protected property The entity type manager service.
ThunderSchemaExtensionPluginBase::create public static function
ThunderSchemaExtensionPluginBase::mapBundleToSchemaName protected function Takes the bundle name and returns the schema name.
ThunderSchemaExtensionPluginBase::resolveBaseFields protected function Add fields common to all entities.
ThunderSchemaExtensionPluginBase::resolveMediaInterfaceFields protected function Add fields common to all media types.
ThunderSchemaExtensionPluginBase::resolvePageInterfaceFields protected function Add fields common to all page types.
ThunderSchemaExtensionPluginBase::resolvePageInterfaceQueryFields protected function Add content query field resolvers.
ThunderSchemaExtensionPluginBase::resolveParagraphInterfaceFields protected function Add fields common to all media types.
ThunderSchemaExtensionPluginBase::setDataProducerManager protected function Set the plugin manager.
ThunderSchemaExtensionPluginBase::setEntityTypeManager protected function Set the entity type manager.