class UuidLinkEnhancer in JSON:API Extras 8.3
Same name and namespace in other branches
- 8 src/Plugin/jsonapi/FieldEnhancer/UuidLinkEnhancer.php \Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer\UuidLinkEnhancer
- 8.2 src/Plugin/jsonapi/FieldEnhancer/UuidLinkEnhancer.php \Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer\UuidLinkEnhancer
Use UUID for internal link field value.
Plugin annotation
@ResourceFieldEnhancer(
id = "uuid_link",
label = @Translation("UUID for link (link field only)"),
description = @Translation("Use UUID for internal link field.")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase implements ResourceFieldEnhancerInterface uses \Shaper\DataAdaptor\DataAdaptorValidatorTrait, \Shaper\DataAdaptor\DataAdaptorTransformerTrait
- class \Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer\UuidLinkEnhancer implements ContainerFactoryPluginInterface
- class \Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase implements ResourceFieldEnhancerInterface uses \Shaper\DataAdaptor\DataAdaptorValidatorTrait, \Shaper\DataAdaptor\DataAdaptorTransformerTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of UuidLinkEnhancer
File
- src/
Plugin/ jsonapi/ FieldEnhancer/ UuidLinkEnhancer.php, line 20
Namespace
Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancerView source
class UuidLinkEnhancer extends ResourceFieldEnhancerBase implements ContainerFactoryPluginInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* {@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();
}
else {
$data = [
'uri' => '',
'title' => '',
'options' => [],
];
}
}
}
return $data;
}
/**
* {@inheritdoc}
*/
protected function doTransform($value, Context $context) {
if (isset($value['uri'])) {
// Check if it is a link to an entity.
preg_match("/entity:(.*)\\/(.*)\\/(.*)/", $value['uri'], $parsed_uri);
if (!empty($parsed_uri)) {
$entity_type = $parsed_uri[1];
$entity_uuid = $parsed_uri[3];
$entities = $this->entityTypeManager
->getStorage($entity_type)
->loadByProperties([
'uuid' => $entity_uuid,
]);
if (!empty($entities)) {
$entity = array_shift($entities);
$value['uri'] = 'entity:' . $entity_type . '/' . $entity
->id();
}
else {
// If the entity has not been imported yet we unset the field value.
$value = [];
}
}
}
return $value;
}
/**
* {@inheritdoc}
*/
public function getOutputJsonSchema() {
return [
'type' => 'object',
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
ResourceFieldEnhancerBase:: |
protected | property |
Holds the plugin configuration. Overrides PluginBase:: |
|
ResourceFieldEnhancerBase:: |
public | function | ||
ResourceFieldEnhancerBase:: |
public | function | 4 | |
ResourceFieldEnhancerBase:: |
public | function | ||
ResourceFieldEnhancerBase:: |
public | function | ||
ResourceFieldEnhancerBase:: |
public | function | ||
ResourceFieldEnhancerBase:: |
public | function | ||
ResourceFieldEnhancerBase:: |
public | function |
Get a form element to render the settings. Overrides ResourceFieldEnhancerInterface:: |
4 |
ResourceFieldEnhancerBase:: |
public | function | ||
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UuidLinkEnhancer:: |
protected | property | The entity type manager. | |
UuidLinkEnhancer:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
UuidLinkEnhancer:: |
protected | function | ||
UuidLinkEnhancer:: |
protected | function | ||
UuidLinkEnhancer:: |
public | function |
Get the JSON Schema for the new output. Overrides ResourceFieldEnhancerInterface:: |
|
UuidLinkEnhancer:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |