You are here

class DataReferenceDefinitionNormalizer in Schemata 8

Same name in this branch
  1. 8 schemata_json_schema/src/Normalizer/json/DataReferenceDefinitionNormalizer.php \Drupal\schemata_json_schema\Normalizer\json\DataReferenceDefinitionNormalizer
  2. 8 schemata_json_schema/src/Normalizer/hal/DataReferenceDefinitionNormalizer.php \Drupal\schemata_json_schema\Normalizer\hal\DataReferenceDefinitionNormalizer

Normalizer for Entity References.

DataReferenceDefinitions are embedded inside ComplexDataDefinitions, and represent a type property. The key for this is usually "entity", and it is found alongside a "target_id" value which refers to the specific entity instance for the reference. The target_id is not normalized by this class, instead it comes through the DataDefinitionNormalizer as a scalar value.

Hierarchy

Expanded class hierarchy of DataReferenceDefinitionNormalizer

1 file declares its use of DataReferenceDefinitionNormalizer
DataReferenceDefinitionNormalizer.php in schemata_json_schema/src/Normalizer/hal/DataReferenceDefinitionNormalizer.php
1 string reference to 'DataReferenceDefinitionNormalizer'
schemata_json_schema.services.yml in schemata_json_schema/schemata_json_schema.services.yml
schemata_json_schema/schemata_json_schema.services.yml
1 service uses DataReferenceDefinitionNormalizer
serializer.normalizer.data_reference_definition.schema_json.json in schemata_json_schema/schemata_json_schema.services.yml
Drupal\schemata_json_schema\Normalizer\json\DataReferenceDefinitionNormalizer

File

schemata_json_schema/src/Normalizer/json/DataReferenceDefinitionNormalizer.php, line 19

Namespace

Drupal\schemata_json_schema\Normalizer\json
View source
class DataReferenceDefinitionNormalizer extends DataDefinitionNormalizer {

  /**
   * The interface or class that this Normalizer supports.
   *
   * @var string
   */
  protected $supportedInterfaceOrClass = DataReferenceDefinitionInterface::class;

  /**
   * EntityTypeManager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityTypeManager;

  /**
   * Constructs an DataReferenceDefinitionNormalizer object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The Entity Type Manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function normalize($entity, $format = NULL, array $context = []) {

    /* @var $entity \Drupal\Core\TypedData\DataReferenceDefinitionInterface */
    try {
      $is_valid = $this
        ->validateEntity($entity);
    } catch (PluginNotFoundException $exception) {
      $is_valid = FALSE;
    }

    // DataDefinitionNormalizer::normalize() results in extraneous structures
    // added to the schema for this field element (e.g., entity)
    return $is_valid ? $this
      ->extractPropertyData($entity, $context) : [];
  }

  /**
   * Ensure the entity type is one we support for schema reference.
   *
   * If somehow the entity does not exist, or is not a ContentEntity, skip it.
   *
   * @param mixed $entity
   *   The object to be normalized.
   *
   * @return bool
   *   TRUE if valid for use.
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   *   If the plugin could not be found.
   */
  protected function validateEntity($entity) {

    // Only entity references have a schema.
    // This leads to incompatibility with alternate reference modules such as
    // Dynamic Entity Reference.
    if ($entity
      ->getDataType() != 'entity_reference') {
      return FALSE;
    }
    $entity_type_plugin = $this->entityTypeManager
      ->getDefinition($entity
      ->getConstraint('EntityType'), FALSE);
    return !empty($entity_type_plugin) && $entity_type_plugin
      ->entityClassImplements(ContentEntityInterface::class);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
DataDefinitionNormalizer::extractPropertyData protected function Extracts property details from a data definition.
DataReferenceDefinitionNormalizer::$entityTypeManager protected property EntityTypeManager.
DataReferenceDefinitionNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides DataDefinitionNormalizer::$supportedInterfaceOrClass
DataReferenceDefinitionNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. Overrides DataDefinitionNormalizer::normalize 1
DataReferenceDefinitionNormalizer::validateEntity protected function Ensure the entity type is one we support for schema reference.
DataReferenceDefinitionNormalizer::__construct public function Constructs an DataReferenceDefinitionNormalizer object. 1
JsonNormalizerBase::$describedFormat protected property The formats that the Normalizer can handle. 7
JsonNormalizerBase::$format protected property The formats that the Normalizer can handle. Overrides NormalizerBase::$format 7
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. Overrides NormalizerBase::checkFormat
NormalizerBase::denormalize public function Denormalizes data back into an object of the given class.
NormalizerBase::normalizeProperties protected function Normalize an array of data definitions.
NormalizerBase::requiredProperty protected function Determine if the given property is a required element of the schema.
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides NormalizerBase::supportsDenormalization
NormalizerBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. 1