Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metatag dataProducer error #3

Open
usmanublox opened this issue Sep 26, 2023 · 0 comments
Open

Metatag dataProducer error #3

usmanublox opened this issue Sep 26, 2023 · 0 comments

Comments

@usmanublox
Copy link

usmanublox commented Sep 26, 2023

I am trying to create metatag dataProducer for directive and getting the below error while executing query in the graphql explorer.

LogicException: The controller result claims to be providing relevant cache metadata, but leaked metadata was detected. Please ensure you are not rendering content too early. Returned object class: Drupal\Core\Cache\CacheableJsonResponse. in Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (line 154 of /app/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php).

Code:

<?php

namespace Drupal\custom_graphql\Plugin\GraphQL\DataProducer;

use Drupal\metatag\MetatagToken;
use Drupal\metatag\MetatagManagerInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\graphql\GraphQL\Execution\FieldContext;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;


/**
 * To retrieve entity metatags.
 *
 * @DataProducer(
 *   id = "metatags",
 *   name = @Translation("Metatags"),
 *   description = @Translation("To retrieve entity metatags."),
 *   produces = @ContextDefinition("any",
 *     label = @Translation("Any"),
 *     multiple = FALSE
 *   ),
 *   consumes = {
 *     "entity" = @ContextDefinition("entity",
 *       label = @Translation("Parent entity"),
 *       required = TRUE
 *     )
 *   }
 * )
 */
class Metatags extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
  use DependencySerializationTrait;

  /**
   * The metatag manager service.
   *
   * @var \Drupal\metatag\MetatagManagerInterface
   */
  protected $metatagManager;

  /**
   * Metatag token.
   *
   * @var \Drupal\metatag\MetatagToken
   */
  protected $metatagToken;

  /**
   * Module Handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * {@inheritdoc}
   *
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('metatag.manager'),
      $container->get('metatag.token'),
      $container->get('module_handler')
    );
  }

  /**
   * Metatags constructor.
   *
   * @param array $configuration
   *   The plugin configuration array.
   * @param string $pluginId
   *   The plugin id.
   * @param mixed $pluginDefinition
   *   The plugin definition array.
   * @param \Drupal\metatag\MetatagManagerInterface $metatagManager
   *   Metatag manager service.
   * @param \Drupal\metatag\MetatagToken $metatagToken
   *   Metatag token service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   Module Handler.
   */
  public function __construct(
    array $configuration,
    string $pluginId,
    $pluginDefinition,
    MetatagManagerInterface $metatagManager,
    MetatagToken $metatagToken,
    ModuleHandlerInterface $moduleHandler
  ) {
    parent::__construct($configuration, $pluginId, $pluginDefinition);
    $this->metatagManager = $metatagManager;
    $this->metatagToken = $metatagToken;
    $this->moduleHandler = $moduleHandler;
  }

  /**
   * Resolve metatags in the given field name.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $value
   * @param \Drupal\graphql\GraphQL\Execution\FieldContext $context
   *
   * @return array
   *   A promise that will return metatags properties or empty array if there
   *   aren't any.
   */
  public function resolve(ContentEntityInterface $entity, FieldContext $context) {
    $tags = $this->metatagManager->tagsFromEntityWithDefaults($entity);
    $tags = $this->replaceTokens($tags, $entity);

    $metatagContext = [
      'entity' => &$entity,
      'graphql_context' => $context,
    ];

    $this->moduleHandler->alter('metatags', $tags, $metatagContext);
    $elements = $this->metatagManager->generateRawElements($tags, $entity);

    return $elements;
  }

  /**
   * Replace tokens.
   *
   * @param array $tags
   *   Metatags tags.
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   Content entity.
   *
   * @return array
   *   Metatags tags replaced.
   */
  protected function replaceTokens(array $tags, ContentEntityInterface $entity): array {
    $data = [$entity->getEntityTypeId() => $entity];
    $options = [
      'langcode' => $entity->language()->getId(),
      'entity' => $entity,
    ];

    foreach ($tags as $index => $tag) {
      $tags[$index] = $this->metatagToken->replace($tag, $data, $options);
    }

    return $tags;
  }
}

This error comes only on first attemp (after clear cache) to execute the query in the explorer and if I execute the query second time then get the expected result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant