You are here

class UuidLinkEnhancer in JSON:API Extras 8.3

Same name and namespace in other branches
  1. 8 src/Plugin/jsonapi/FieldEnhancer/UuidLinkEnhancer.php \Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer\UuidLinkEnhancer
  2. 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

Expanded class hierarchy of UuidLinkEnhancer

File

src/Plugin/jsonapi/FieldEnhancer/UuidLinkEnhancer.php, line 20

Namespace

Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer
View 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

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
ResourceFieldEnhancerBase::$configuration protected property Holds the plugin configuration. Overrides PluginBase::$configuration
ResourceFieldEnhancerBase::calculateDependencies public function
ResourceFieldEnhancerBase::defaultConfiguration public function 4
ResourceFieldEnhancerBase::getConfiguration public function
ResourceFieldEnhancerBase::getInputValidator public function
ResourceFieldEnhancerBase::getInternalValidator public function
ResourceFieldEnhancerBase::getOutputValidator public function
ResourceFieldEnhancerBase::getSettingsForm public function Get a form element to render the settings. Overrides ResourceFieldEnhancerInterface::getSettingsForm 4
ResourceFieldEnhancerBase::setConfiguration public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UuidLinkEnhancer::$entityTypeManager protected property The entity type manager.
UuidLinkEnhancer::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
UuidLinkEnhancer::doTransform protected function
UuidLinkEnhancer::doUndoTransform protected function
UuidLinkEnhancer::getOutputJsonSchema public function Get the JSON Schema for the new output. Overrides ResourceFieldEnhancerInterface::getOutputJsonSchema
UuidLinkEnhancer::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct