EntityShareBlockFieldEnhancer.php in Entity Share 8.3
File
src/Plugin/jsonapi/FieldEnhancer/EntityShareBlockFieldEnhancer.php
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 EntityShareBlockFieldEnhancer 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['settings']['id'])) {
$parsed_id = [];
preg_match("/block_content:(.*)/", $data['settings']['id'], $parsed_id);
if (!empty($parsed_id)) {
$block_content_uuid = $parsed_id[1];
$block_contents = $this->entityTypeManager
->getStorage('block_content')
->loadByProperties([
'uuid' => $block_content_uuid,
]);
if (!empty($block_contents)) {
$block_content = array_shift($block_contents);
$route_name = sprintf('jsonapi.%s--%s.individual', 'block_content', $block_content
->bundle());
$url = Url::fromRoute($route_name, [
'entity' => $block_content_uuid,
])
->setOption('language', $this->languageManager
->getCurrentLanguage())
->setOption('absolute', TRUE);
$data['block_content_href'] = $url
->toString();
}
}
}
return $data;
}
protected function doTransform($value, Context $context) {
if (isset($value['block_content_href'])) {
unset($value['block_content_href']);
}
return $value;
}
public function getOutputJsonSchema() {
return [
'type' => 'object',
];
}
}