EntityLinks.php in Thunder 6.2.x
File
modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/EntityLinks.php
View source
<?php
namespace Drupal\thunder_gqls\Plugin\GraphQL\DataProducer;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\RendererInterface;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityLinks extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
protected $renderer;
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
return new static($configuration, $pluginId, $pluginDefinition, $container
->get('renderer'));
}
public function __construct(array $configuration, string $pluginId, $pluginDefinition, RendererInterface $renderer) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->renderer = $renderer;
}
public function resolve(EntityInterface $entity) {
$context = new RenderContext();
$result = $this->renderer
->executeInRenderContext($context, function () use ($entity) {
$links = $entity
->getEntityType()
->getLinkTemplates();
array_walk($links, function (&$url, $rel) use ($entity) {
try {
$url = $entity
->toUrl($rel)
->toString();
} catch (\Exception $exception) {
$url = '';
}
});
$transformed_keys = array_map([
$this,
'toCamelCase',
], array_keys($links));
return array_combine($transformed_keys, $links);
});
return $result ?? NULL;
}
public static function toCamelCase($input) {
return lcfirst(str_replace(' ', '', ucwords(str_replace('-', ' ', $input))));
}
}
Classes
Name |
Description |
EntityLinks |
Plugin to resolve all the links for an entity. |