class AliasLinkEnhancer in Gatsby Live Preview & Incremental Builds 8
Same name and namespace in other branches
- 2.0.x modules/gatsby_extras/src/Plugin/jsonapi/FieldEnhancer/AliasLinkEnhancer.php \Drupal\gatsby_extras\Plugin\jsonapi\FieldEnhancer\AliasLinkEnhancer
Use UUID for internal link field value.
Plugin annotation
@ResourceFieldEnhancer(
id = "alias_link",
label = @Translation("Alias for link (link field only)"),
description = @Translation("Use alias for internal link field.")
)
Hierarchy
- class \Drupal\gatsby_extras\Plugin\jsonapi\FieldEnhancer\AliasLinkEnhancer extends \Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase implements ContainerFactoryPluginInterface
Expanded class hierarchy of AliasLinkEnhancer
File
- modules/
gatsby_extras/ src/ Plugin/ jsonapi/ FieldEnhancer/ AliasLinkEnhancer.php, line 20
Namespace
Drupal\gatsby_extras\Plugin\jsonapi\FieldEnhancerView source
class AliasLinkEnhancer 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_uuid'] = 'entity:' . $entity_type . '/' . $entity
->bundle() . '/' . $entity
->uuid();
$data['uri_alias'] = $entity
->toUrl()
->toString();
}
else {
$data = [
'uri' => '',
'uri_uuid' => '',
'uri_alias' => '',
'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)) {
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = array_shift($entities);
$value['uri_uuid'] = 'entity:' . $entity_type . '/' . $entity
->id();
$value['uri_alias'] = $entity
->toUrl()
->toString();
}
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 |
---|---|---|---|---|
AliasLinkEnhancer:: |
protected | property | The entity type manager. | |
AliasLinkEnhancer:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
AliasLinkEnhancer:: |
protected | function | ||
AliasLinkEnhancer:: |
protected | function | ||
AliasLinkEnhancer:: |
public | function | ||
AliasLinkEnhancer:: |
public | function |