class EntityReferenceUuidItem

Defines the 'entity_reference_uuid' entity field type.

Supported settings (below the definition's 'settings' key) are:

  • target_type: The entity type to reference. Required.

@property string $target_uuid

Attributes

#[FieldType(id: 'jsonapi_test_field_type_entity_reference_uuid', label: new TranslatableMarkup('Entity reference UUID'), description: new TranslatableMarkup('An entity field containing an entity reference by UUID.'), category: 'reference', default_widget: 'entity_reference_autocomplete', default_formatter: 'entity_reference_label', list_class: EntityReferenceFieldItemList::class)]

Hierarchy

Expanded class hierarchy of EntityReferenceUuidItem

File

core/modules/jsonapi/tests/modules/jsonapi_test_field_type/src/Plugin/Field/FieldType/EntityReferenceUuidItem.php, line 26

Namespace

Drupal\jsonapi_test_field_type\Plugin\Field\FieldType
View source
class EntityReferenceUuidItem extends EntityReferenceItem {
  
  /**
   * {@inheritdoc}
   */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $settings = $field_definition->getSettings();
    $target_type_info = \Drupal::entityTypeManager()->getDefinition($settings['target_type']);
    $properties = parent::propertyDefinitions($field_definition);
    $target_uuid_definition = DataReferenceTargetDefinition::create('string')->setLabel(new TranslatableMarkup('@label UUID', [
      '@label' => $target_type_info->getLabel(),
    ]));
    $target_uuid_definition->setRequired(TRUE);
    $properties['target_uuid'] = $target_uuid_definition;
    $properties['entity'] = DataReferenceDefinition::create('entity')->setLabel($target_type_info->getLabel())
      ->setDescription(new TranslatableMarkup('The referenced entity by UUID'))
      ->setComputed(TRUE)
      ->setReadOnly(FALSE)
      ->setTargetDefinition(EntityDataDefinition::create($settings['target_type']))
      ->addConstraint('EntityType', $settings['target_type']);
    return $properties;
  }
  
  /**
   * {@inheritdoc}
   */
  public static function mainPropertyName() {
    return 'target_uuid';
  }
  
  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    $columns = [
      'target_uuid' => [
        'description' => 'The UUID of the target entity.',
        'type' => 'varchar_ascii',
        'length' => 128,
      ],
    ];
    return [
      'columns' => $columns,
      'indexes' => [
        'target_uuid' => [
          'target_uuid',
        ],
      ],
    ];
  }
  
  /**
   * {@inheritdoc}
   */
  public function setValue($values, $notify = TRUE) : void {
    if (isset($values) && !is_array($values)) {
      // If either a scalar or an object was passed as the value for the item,
      // assign it to the 'entity' or 'target_uuid' depending on values type.
      if (is_object($values)) {
        $this->set('entity', $values, $notify);
      }
      else {
        $this->set('target_uuid', $values, $notify);
      }
    }
    else {
      parent::setValue($values, FALSE);
      // Support setting the field item with only one property, but make sure
      // values stay in sync if only property is passed.
      // NULL is a valid value, so we use array_key_exists().
      if (is_array($values) && array_key_exists('target_uuid', $values) && !isset($values['entity'])) {
        $this->onChange('target_uuid', FALSE);
      }
      elseif (is_array($values) && !array_key_exists('target_uuid', $values) && isset($values['entity'])) {
        $this->onChange('entity', FALSE);
      }
      elseif (is_array($values) && array_key_exists('target_uuid', $values) && isset($values['entity'])) {
        // If both properties are passed, verify the passed values match. The
        // only exception we allow is when we have a new entity: in this case
        // its actual id and target_uuid will be different, due to the new
        // entity marker.
        $entity_uuid = $this->get('entity')
          ->get('uuid');
        // If the entity has been saved and we're trying to set both the
        // target_uuid and the entity values with a non-null target UUID, then
        // the value for target_uuid should match the UUID of the entity value.
        if (!$this->entity
          ->isNew() && $values['target_uuid'] !== NULL && $entity_uuid !== $values['target_uuid']) {
          throw new \InvalidArgumentException('The target UUID and entity passed to the entity reference item do not match.');
        }
      }
      // Notify the parent if necessary.
      if ($notify && $this->parent) {
        $this->parent
          ->onChange($this->getName());
      }
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public function onChange($property_name, $notify = TRUE) : void {
    // Make sure that the target UUID and the target property stay in sync.
    if ($property_name === 'entity') {
      $property = $this->get('entity');
      if ($target_uuid = $property->isTargetNew() ? NULL : $property->getValue()
        ->uuid()) {
        $this->writePropertyValue('target_uuid', $target_uuid);
      }
    }
    elseif ($property_name === 'target_uuid') {
      $property = $this->get('entity');
      $entity_type = $property->getDataDefinition()
        ->getConstraint('EntityType');
      $entities = \Drupal::entityTypeManager()->getStorage($entity_type)
        ->loadByProperties([
        'uuid' => $this->get('target_uuid')
          ->getValue(),
      ]);
      if ($entity = array_shift($entities)) {
        assert($entity instanceof EntityInterface);
        $this->writePropertyValue('target_uuid', $entity->uuid());
        $this->writePropertyValue('entity', $entity);
      }
    }
    parent::onChange($property_name, $notify);
  }
  
  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
    // Avoid loading the entity by first checking the 'target_uuid'.
    if ($this->target_uuid !== NULL) {
      return FALSE;
    }
    if ($this->entity && $this->entity instanceof EntityInterface) {
      return FALSE;
    }
    return TRUE;
  }
  
  /**
   * {@inheritdoc}
   */
  public function preSave() : void {
    if ($this->hasNewEntity()) {
      // Save the entity if it has not already been saved by some other code.
      if ($this->entity
        ->isNew()) {
        $this->entity
          ->save();
      }
      // Make sure the parent knows we are updating this property so it can
      // react properly.
      $this->target_uuid = $this->entity
        ->uuid();
    }
    if (!$this->isEmpty() && $this->target_uuid === NULL) {
      $this->target_uuid = $this->entity
        ->uuid();
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public static function generateSampleValue(FieldDefinitionInterface $field_definition) : array {
    $manager = \Drupal::service('plugin.manager.entity_reference_selection');
    // Instead of calling $manager->getSelectionHandler($field_definition)
    // replicate the behavior to be able to override the sorting settings.
    $options = [
      'target_type' => $field_definition->getFieldStorageDefinition()
        ->getSetting('target_type'),
      'handler' => $field_definition->getSetting('handler'),
      'handler_settings' => $field_definition->getSetting('handler_settings') ?: [],
      'entity' => NULL,
    ];
    $entity_type = \Drupal::entityTypeManager()->getDefinition($options['target_type']);
    $options['handler_settings']['sort'] = [
      'field' => $entity_type->getKey('uuid'),
      'direction' => 'DESC',
    ];
    $selection_handler = $manager->getInstance($options);
    // Select a random number of references between the last 50 referenceable
    // entities created.
    if ($referenceable = $selection_handler->getReferenceableEntities(NULL, 'CONTAINS', 50)) {
      $group = array_rand($referenceable);
      return [
        'target_uuid' => array_rand($referenceable[$group]),
      ];
    }
    return [];
  }
  
  /**
   * Determines whether the item holds an unsaved entity.
   *
   * This is notably used for "autocreate" widgets, and more generally to
   * support referencing freshly created entities (they will get saved
   * automatically as the hosting entity gets saved).
   *
   * @return bool
   *   TRUE if the item holds an unsaved entity.
   */
  public function hasNewEntity() {
    return !$this->isEmpty() && $this->target_uuid === NULL && $this->entity
      ->isNew();
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title 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 2
DependencySerializationTrait::__wakeup public function 2
EntityReferenceItem::calculateDependencies public static function Calculates dependencies for field items. Overrides FieldItemBase::calculateDependencies
EntityReferenceItem::calculateStorageDependencies public static function Calculates dependencies for field items on the storage level. Overrides FieldItemBase::calculateStorageDependencies
EntityReferenceItem::defaultFieldSettings public static function Defines the field-level settings for this plugin. Overrides FieldItemBase::defaultFieldSettings 1
EntityReferenceItem::defaultStorageSettings public static function Defines the storage-level settings for this plugin. Overrides FieldItemBase::defaultStorageSettings 1
EntityReferenceItem::fieldSettingsAjaxProcess public static function Render API callback: Processes the field settings form.
EntityReferenceItem::fieldSettingsAjaxProcessElement public static function Adds the field settings to AJAX form elements.
EntityReferenceItem::fieldSettingsForm public function Returns a form for the field-level settings. Overrides FieldItemBase::fieldSettingsForm 1
EntityReferenceItem::fieldSettingsFormValidate public static function Form element validation handler; Invokes selection plugin's validation.
EntityReferenceItem::fieldSettingsSummary public static function Returns a short summary of the field's field-level settings. Overrides FieldItemBase::fieldSettingsSummary
EntityReferenceItem::formProcessMergeParent public static function Render API callback that moves entity reference elements up a level.
EntityReferenceItem::getConstraints public function Gets a list of validation constraints. Overrides TypedData::getConstraints
EntityReferenceItem::getPossibleOptions public function Returns an array of possible values with labels for display. Overrides OptionsProviderInterface::getPossibleOptions
EntityReferenceItem::getPossibleValues public function Returns an array of possible values. Overrides OptionsProviderInterface::getPossibleValues
EntityReferenceItem::getPreconfiguredOptions public static function Returns preconfigured field options for a field type. Overrides PreconfiguredFieldUiOptionsInterface::getPreconfiguredOptions 1
EntityReferenceItem::getRandomBundle protected static function Gets a bundle for a given entity type and selection options.
EntityReferenceItem::getReferenceableBundles public static function Returns the referenceable entity types and bundles. Overrides EntityReferenceItemInterface::getReferenceableBundles
EntityReferenceItem::getSettableOptions public function Returns an array of settable values with labels for display. Overrides OptionsProviderInterface::getSettableOptions
EntityReferenceItem::getSettableValues public function Returns an array of settable values. Overrides OptionsProviderInterface::getSettableValues
EntityReferenceItem::getValue public function Gets the data value. Overrides Map::getValue
EntityReferenceItem::onDependencyRemoval public static function Informs the plugin that a dependency of the field will be deleted. Overrides FieldItemBase::onDependencyRemoval
EntityReferenceItem::settingsAjaxSubmit public static function Submit handler for the non-JS case.
EntityReferenceItem::storageSettingsForm public function Returns a form for the storage-level settings. Overrides FieldItemBase::storageSettingsForm 1
EntityReferenceItem::storageSettingsSummary public static function Returns a short summary of the field's storage-level settings. Overrides FieldItemBase::storageSettingsSummary 1
EntityReferenceItemBase::__get public function Magic method: Gets a property value. Overrides FieldItemBase::__get
EntityReferenceUuidItem::generateSampleValue public static function Generates placeholder field values. Overrides EntityReferenceItem::generateSampleValue
EntityReferenceUuidItem::hasNewEntity public function Determines whether the item holds an unsaved entity. Overrides EntityReferenceItem::hasNewEntity
EntityReferenceUuidItem::isEmpty public function Determines whether the data structure is empty. Overrides EntityReferenceItem::isEmpty
EntityReferenceUuidItem::mainPropertyName public static function Returns the name of the main property, if any. Overrides EntityReferenceItem::mainPropertyName
EntityReferenceUuidItem::onChange public function React to changes to a child property or item. Overrides EntityReferenceItem::onChange
EntityReferenceUuidItem::preSave public function Defines custom presave behavior for field values. Overrides EntityReferenceItem::preSave
EntityReferenceUuidItem::propertyDefinitions public static function Defines field item properties. Overrides EntityReferenceItem::propertyDefinitions
EntityReferenceUuidItem::schema public static function Returns the schema for the field. Overrides EntityReferenceItem::schema
EntityReferenceUuidItem::setValue public function Overrides \Drupal\Core\TypedData\TypedData::setValue(). Overrides EntityReferenceItem::setValue
FieldItemBase::delete public function Defines custom delete behavior for field values. Overrides FieldItemInterface::delete 2
FieldItemBase::deleteRevision public function Defines custom revision delete behavior for field values. Overrides FieldItemInterface::deleteRevision
FieldItemBase::fieldSettingsFromConfigData public static function Returns a settings array in the field type's canonical representation. Overrides FieldItemInterface::fieldSettingsFromConfigData 1
FieldItemBase::fieldSettingsToConfigData public static function Returns a settings array that can be stored as a configuration value. Overrides FieldItemInterface::fieldSettingsToConfigData 1
FieldItemBase::getEntity public function Gets the entity that field belongs to. Overrides FieldItemInterface::getEntity
FieldItemBase::getFieldDefinition public function Gets the field definition. Overrides FieldItemInterface::getFieldDefinition
FieldItemBase::getLangcode public function Gets the langcode of the field values held in the object. Overrides FieldItemInterface::getLangcode
FieldItemBase::getSetting protected function Returns the value of a field setting.
FieldItemBase::getSettings protected function Returns the array of field settings.
FieldItemBase::postSave public function Defines custom post-save behavior for field values. Overrides FieldItemInterface::postSave 2
FieldItemBase::storageSettingsFromConfigData public static function Returns a settings array in the field type's canonical representation. Overrides FieldItemInterface::storageSettingsFromConfigData 2
FieldItemBase::storageSettingsToConfigData public static function Returns a settings array that can be stored as a configuration value. Overrides FieldItemInterface::storageSettingsToConfigData 2
FieldItemBase::view public function Returns a renderable array for a single field item. Overrides FieldItemInterface::view
FieldItemBase::writePropertyValue protected function Different to the parent Map class, we avoid creating property objects as
far as possible in order to optimize performance. Thus we just update
$this->values if no property object has been created yet.
Overrides Map::writePropertyValue
FieldItemBase::__construct public function Constructs a TypedData object given its definition and context. Overrides TypedData::__construct 3
FieldItemBase::__isset public function Magic method: Determines whether a property is set. Overrides FieldItemInterface::__isset
FieldItemBase::__set public function Magic method: Sets a property value. Overrides FieldItemInterface::__set 1
FieldItemBase::__unset public function Magic method: Unsets a property. Overrides FieldItemInterface::__unset
Map::$definition protected property The data definition. Overrides TypedData::$definition
Map::$properties protected property The array of properties.
Map::$values protected property An array of values for the contained properties.
Map::applyDefaultValue public function Applies the default value. Overrides TypedData::applyDefaultValue 4
Map::get public function Gets a property object. Overrides ComplexDataInterface::get
Map::getIterator public function Retrieves the iterator for the object.
Map::getProperties public function Gets an array of property objects. Overrides ComplexDataInterface::getProperties
Map::getString public function Returns a string representation of the data. Overrides TypedData::getString
Map::set public function Sets a property value. Overrides ComplexDataInterface::set
Map::toArray public function Returns an array of all property values. Overrides ComplexDataInterface::toArray 1
Map::__clone public function Magic method: Implements a deep clone.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
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. 1
TypedData::$name protected property The property name.
TypedData::$parent protected property The parent typed data object.
TypedData::createInstance public static function Constructs a TypedData object given its definition and context. Overrides TypedDataInterface::createInstance
TypedData::getDataDefinition public function Gets the data definition. Overrides TypedDataInterface::getDataDefinition
TypedData::getName public function Returns the name of a property or item. Overrides TypedDataInterface::getName
TypedData::getParent public function Returns the parent data structure; i.e. either complex data or a list. Overrides TypedDataInterface::getParent
TypedData::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
TypedData::getPluginId public function Gets the plugin ID of the plugin instance. Overrides PluginInspectionInterface::getPluginId
TypedData::getPropertyPath public function Returns the property path of the data. Overrides TypedDataInterface::getPropertyPath
TypedData::getRoot public function Returns the root of the typed data tree. Overrides TypedDataInterface::getRoot
TypedData::setContext public function Sets the context of a property or item via a context aware parent. Overrides TypedDataInterface::setContext
TypedData::validate public function Validates the currently set data value. Overrides TypedDataInterface::validate
TypedDataTrait::$typedDataManager protected property The typed data manager used for creating the data types.
TypedDataTrait::getTypedDataManager public function Gets the typed data manager. 2
TypedDataTrait::setTypedDataManager public function Sets the typed data manager. 2

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.