You are here

class ThunderParagraphsSchemaExtension in Thunder 6.2.x

The paragraph schema extension.

Plugin annotation


@SchemaExtension(
  id = "thunder_paragraphs",
  name = "Paragraph extension",
  description = "Adds paragraphs and their fields.",
  schema = "thunder"
)

Hierarchy

Expanded class hierarchy of ThunderParagraphsSchemaExtension

File

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

Namespace

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

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

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

    // Text.
    $this
      ->resolveParagraphInterfaceFields('ParagraphText');
    $this
      ->addFieldResolverIfNotExists('ParagraphText', 'text', $this->builder
      ->fromPath('entity', 'field_text.processed'));

    // Image.
    $this
      ->resolveParagraphInterfaceFields('ParagraphImage');
    $this
      ->addFieldResolverIfNotExists('ParagraphImage', 'image', $this->builder
      ->fromPath('entity', 'field_image.entity'));

    // Twitter.
    $this
      ->resolveParagraphInterfaceFields('ParagraphTwitter');
    $this
      ->addFieldResolverIfNotExists('ParagraphTwitter', 'url', $this->builder
      ->compose($this->builder
      ->fromPath('entity', 'field_media.entity'), $this->builder
      ->fromPath('entity', 'field_url.value')));

    // Instagram.
    $this
      ->resolveParagraphInterfaceFields('ParagraphInstagram');
    $this
      ->addFieldResolverIfNotExists('ParagraphInstagram', 'url', $this->builder
      ->compose($this->builder
      ->fromPath('entity', 'field_media.entity'), $this->builder
      ->fromPath('entity', 'field_url.value')));

    // Pinterest.
    $this
      ->resolveParagraphInterfaceFields('ParagraphPinterest');
    $this
      ->addFieldResolverIfNotExists('ParagraphPinterest', 'url', $this->builder
      ->compose($this->builder
      ->fromPath('entity', 'field_media.entity'), $this->builder
      ->fromPath('entity', 'field_url.value')));

    // Gallery.
    $this
      ->resolveParagraphInterfaceFields('ParagraphGallery');
    $this
      ->addFieldResolverIfNotExists('ParagraphGallery', 'name', $this->builder
      ->compose($this->builder
      ->fromPath('entity', 'field_media.entity'), $this->builder
      ->produce('entity_label')
      ->map('entity', $this->builder
      ->fromParent())));
    $this
      ->addFieldResolverIfNotExists('ParagraphGallery', 'images', $this->builder
      ->compose($this->builder
      ->fromPath('entity', 'field_media.entity'), $this
      ->fromEntityReference('field_media_images')));

    // Link.
    $this
      ->resolveParagraphInterfaceFields('ParagraphLink');
    $this
      ->addFieldResolverIfNotExists('ParagraphLink', 'links', $this->builder
      ->fromPath('entity', 'field_link'));

    // Video.
    $this
      ->resolveParagraphInterfaceFields('ParagraphVideo');
    $this
      ->addFieldResolverIfNotExists('ParagraphVideo', 'video', $this->builder
      ->fromPath('entity', 'field_video.entity'));

    // Quote.
    $this
      ->resolveParagraphInterfaceFields('ParagraphQuote');
    $this
      ->addFieldResolverIfNotExists('ParagraphQuote', 'text', $this->builder
      ->fromPath('entity', 'field_text.processed'));
  }

  /**
   * Resolves page 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 resolveParagraphTypes($value, ResolveContext $context, ResolveInfo $info) : string {
    if ($value instanceof ParagraphInterface) {
      return 'Paragraph' . $this
        ->mapBundleToSchemaName($value
        ->bundle());
    }
    throw new \Exception('Invalid paragraph 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.
ThunderParagraphsSchemaExtension::registerResolvers public function Overrides ThunderSchemaExtensionPluginBase::registerResolvers
ThunderParagraphsSchemaExtension::resolveFields protected function Add paragraph field resolvers.
ThunderParagraphsSchemaExtension::resolveParagraphTypes protected function Resolves page 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.