View source
<?php
declare (strict_types=1);
namespace Drupal\entity_share\Plugin\jsonapi\FieldEnhancer;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase;
use Shaper\Util\Context;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityShareEmbeddedEntitiesEnhancer extends ResourceFieldEnhancerBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $languageManager;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->languageManager = $language_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('language_manager'));
}
protected function doUndoTransform($data, Context $context) {
if (!isset($data['format']) || !isset($data['value'])) {
return $data;
}
$data['value'] = preg_replace_callback('#(<.*data-entity-type="(.*)".*data-entity-uuid="(.*)".*)(/?)>#U', [
self::class,
'addEntityJsonapiUrl',
], $data['value']);
return $data;
}
protected function doTransform($value, Context $context) {
if (!isset($value['format']) || !isset($value['value'])) {
return $value;
}
$value['value'] = preg_replace('# data-entity-jsonapi-url="(.*)"#U', '', $value['value']);
$value['value'] = preg_replace_callback('#(<img.*data-entity-type="(.*)".*data-entity-uuid="(.*)".*)(/?)>#U', [
self::class,
'updateImgSrc',
], $value['value']);
return $value;
}
public function getOutputJsonSchema() {
return [
'type' => 'object',
];
}
protected function addEntityJsonapiUrl(array $matches) {
$entity_type = $matches[2];
$entity_uuid = $matches[3];
$entities = $this->entityTypeManager
->getStorage($entity_type)
->loadByProperties([
'uuid' => $entity_uuid,
]);
if (!empty($entities)) {
$entity = array_shift($entities);
$route_name = sprintf('jsonapi.%s--%s.individual', $entity_type, $entity
->bundle());
try {
$content_url = Url::fromRoute($route_name, [
'entity' => $entity
->uuid(),
])
->setOption('language', $this->languageManager
->getCurrentLanguage())
->setOption('absolute', TRUE);
$closing_slash = $matches[4];
return $matches[1] . ' data-entity-jsonapi-url="' . $content_url
->toString() . '"' . $closing_slash . '>';
} catch (\Exception $exception) {
}
}
return $matches[0];
}
protected function updateImgSrc(array $matches) {
$entity_type = $matches[2];
$entity_uuid = $matches[3];
if ($entity_type != 'file') {
return $matches[0];
}
$entities = $this->entityTypeManager
->getStorage($entity_type)
->loadByProperties([
'uuid' => $entity_uuid,
]);
if (!empty($entities)) {
$entity = array_shift($entities);
$new_src = $entity
->createFileUrl();
return preg_replace('#src="(.*)"#U', 'src="' . $new_src . '"', $matches[0]);
}
return $matches[0];
}
}