class EntityShareUuidLinkEnhancer in Entity Share 8.3
Use UUID for internal link field value and add URL for import.
Plugin annotation
@ResourceFieldEnhancer(
id = "entity_share_uuid_link",
label = @Translation("UUID for link (link field only) (Entity Share)"),
description = @Translation("Use UUID for internal link field and provide import URL.")
)
Hierarchy
- class \Drupal\entity_share\Plugin\jsonapi\FieldEnhancer\EntityShareUuidLinkEnhancer extends \Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer\UuidLinkEnhancer
Expanded class hierarchy of EntityShareUuidLinkEnhancer
File
- src/
Plugin/ jsonapi/ FieldEnhancer/ EntityShareUuidLinkEnhancer.php, line 21
Namespace
Drupal\entity_share\Plugin\jsonapi\FieldEnhancerView source
class EntityShareUuidLinkEnhancer extends UuidLinkEnhancer {
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : self {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->languageManager = $container
->get('language_manager');
return $instance;
}
/**
* {@inheritdoc}
*/
protected function doUndoTransform($data, Context $context) {
if (isset($data['uri'])) {
// Check if it is a link to an entity.
preg_match("/entity:(.*)\\/(.*)/", $data['uri'], $parsed_uri);
if (!empty($parsed_uri)) {
$entity_type = $parsed_uri[1];
$entity_id = $parsed_uri[2];
$entity = $this->entityTypeManager
->getStorage($entity_type)
->load($entity_id);
if (!is_null($entity)) {
$data['uri'] = 'entity:' . $entity_type . '/' . $entity
->bundle() . '/' . $entity
->uuid();
// Add URL for import.
$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);
$data['content_entity_href'] = $content_url
->toString();
} catch (\Exception $exception) {
// Do nothing.
}
}
else {
$data = [
'uri' => '',
'title' => '',
'options' => [],
];
}
}
}
return $data;
}
/**
* {@inheritdoc}
*/
protected function doTransform($value, Context $context) {
if (isset($value['content_entity_href'])) {
unset($value['content_entity_href']);
}
return parent::doTransform($value, $context);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityShareUuidLinkEnhancer:: |
protected | property | The language manager. | |
EntityShareUuidLinkEnhancer:: |
public static | function | ||
EntityShareUuidLinkEnhancer:: |
protected | function | ||
EntityShareUuidLinkEnhancer:: |
protected | function |