You are here

abstract class ThunderSchemaExtensionPluginBase in Thunder 6.2.x

The base class for Thunder schema extension plugins.

Hierarchy

Expanded class hierarchy of ThunderSchemaExtensionPluginBase

File

modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSchemaExtensionPluginBase.php, line 18

Namespace

Drupal\thunder_gqls\Plugin\GraphQL\SchemaExtension
View source
abstract class ThunderSchemaExtensionPluginBase extends SdlSchemaExtensionPluginBase {
  use ResolverHelperTrait;

  /**
   * The data producer plugin manager.
   *
   * @var \Drupal\graphql\Plugin\DataProducerPluginManager
   */
  protected $dataProducerManager;

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $plugin = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    $plugin
      ->createResolverBuilder();
    $plugin
      ->setDataProducerManager($container
      ->get('plugin.manager.graphql.data_producer'));
    $plugin
      ->setEntityTypeManager($container
      ->get('entity_type.manager'));
    return $plugin;
  }

  /**
   * {@inheritdoc}
   */
  public function registerResolvers(ResolverRegistryInterface $registry) {
    $this->registry = $registry;
  }

  /**
   * Set the plugin manager.
   *
   * @param \Drupal\graphql\Plugin\DataProducerPluginManager $pluginManager
   *   The data producer plugin manager.
   */
  protected function setDataProducerManager(DataProducerPluginManager $pluginManager) {
    $this->dataProducerManager = $pluginManager;
  }

  /**
   * Set the entity type manager.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager service.
   */
  protected function setEntityTypeManager(EntityTypeManagerInterface $entityTypeManager) {
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * Takes the bundle name and returns the schema name.
   *
   * @param string $bundleName
   *   The bundle name.
   *
   * @return string
   *   Returns the mapped bundle name.
   */
  protected function mapBundleToSchemaName(string $bundleName) {
    return str_replace('_', '', ucwords($bundleName, '_'));
  }

  /**
   * Add fields common to all entities.
   *
   * @param string $type
   *   The type name.
   * @param string $entity_type_id
   *   The entity type ID.
   */
  protected function resolveBaseFields(string $type, string $entity_type_id) {
    $this
      ->addFieldResolverIfNotExists($type, 'uuid', $this->builder
      ->produce('entity_uuid')
      ->map('entity', $this->builder
      ->fromParent()));
    $this
      ->addFieldResolverIfNotExists($type, 'id', $this->builder
      ->produce('entity_id')
      ->map('entity', $this->builder
      ->fromParent()));
    $this
      ->addFieldResolverIfNotExists($type, 'entity', $this->builder
      ->produce('entity_type_id')
      ->map('entity', $this->builder
      ->fromParent()));
    $this
      ->addFieldResolverIfNotExists($type, 'name', $this->builder
      ->produce('entity_label')
      ->map('entity', $this->builder
      ->fromParent()));
    $this
      ->addFieldResolverIfNotExists($type, 'language', $this->builder
      ->fromPath('entity', 'langcode.value'));
    $entity_type = $this->entityTypeManager
      ->getDefinition($entity_type_id);
    if ($entity_type
      ->hasLinkTemplate('canonical')) {
      $this
        ->addFieldResolverIfNotExists($type, 'url', $this->builder
        ->compose($this->builder
        ->produce('entity_url')
        ->map('entity', $this->builder
        ->fromParent()), $this->builder
        ->produce('url_path')
        ->map('url', $this->builder
        ->fromParent())));
    }
    else {
      $this
        ->addFieldResolverIfNotExists($type, 'url', $this->builder
        ->fromValue(NULL));
    }
    if (method_exists($entity_type
      ->getClass(), 'getCreatedTime')) {
      $this
        ->addFieldResolverIfNotExists($type, 'created', $this->builder
        ->produce('entity_created')
        ->map('entity', $this->builder
        ->fromParent()));
    }
    if ($entity_type
      ->entityClassImplements(EntityChangedInterface::class)) {
      $this
        ->addFieldResolverIfNotExists($type, 'changed', $this->builder
        ->produce('entity_changed')
        ->map('entity', $this->builder
        ->fromParent()));
    }
    if ($entity_type
      ->entityClassImplements(EntityPublishedInterface::class)) {
      $this
        ->addFieldResolverIfNotExists($type, 'published', $this->builder
        ->produce('entity_published')
        ->map('entity', $this->builder
        ->fromParent()));
    }
    if ($entity_type
      ->entityClassImplements(EntityOwnerInterface::class)) {
      $this
        ->addFieldResolverIfNotExists($type, 'author', $this->builder
        ->produce('entity_owner')
        ->map('entity', $this->builder
        ->fromParent()));
    }
    $this
      ->addFieldResolverIfNotExists($type, 'entityLinks', $this->builder
      ->produce('entity_links')
      ->map('entity', $this->builder
      ->fromParent()));
  }

  /**
   * Add fields common to all media types.
   *
   * @param string $type
   *   The type name.
   */
  protected function resolveMediaInterfaceFields(string $type) {
    $this
      ->resolveBaseFields($type, 'media');
    $this
      ->addFieldResolverIfNotExists($type, 'thumbnail', $this->builder
      ->produce('thunder_image')
      ->map('entity', $this->builder
      ->fromPath('entity', 'thumbnail.entity'))
      ->map('field', $this->builder
      ->fromPath('entity', 'thumbnail')));
    if ($this->dataProducerManager
      ->hasDefinition('media_expire_fallback_entity')) {
      $this
        ->addFieldResolverIfNotExists($type, 'fallbackMedia', $this->builder
        ->produce('media_expire_fallback_entity')
        ->map('entity', $this->builder
        ->fromParent()));
    }
  }

  /**
   * Add fields common to all media types.
   *
   * @param string $type
   *   The type name.
   */
  protected function resolveParagraphInterfaceFields(string $type) {
    $this
      ->addFieldResolverIfNotExists($type, 'summary', $this->builder
      ->produce('paragraph_summary')
      ->map('paragraph', $this->builder
      ->fromParent()));
  }

  /**
   * Add fields common to all page types.
   *
   * @param string $type
   *   The type name.
   * @param string $entity_type_id
   *   The entity type ID.
   */
  protected function resolvePageInterfaceFields(string $type, string $entity_type_id) {
    $this
      ->resolveBaseFields($type, $entity_type_id);
  }

  /**
   * Add content query field resolvers.
   *
   * @param string $page_type
   *   The page type name.
   * @param string $entity_type_id
   *   The entity type ID.
   */
  protected function resolvePageInterfaceQueryFields(string $page_type, string $entity_type_id) {
    $this
      ->addFieldResolverIfNotExists('Query', $page_type, $this->builder
      ->produce('entity_load_by_uuid')
      ->map('type', $this->builder
      ->fromValue($entity_type_id))
      ->map('uuid', $this->builder
      ->fromArgument('uuid')));
  }

}

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.
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::registerResolvers public function 7
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.