getTargetDefinition example


  public function getValue() {
    if ($target = $this->getTarget()) {
      return $target->getValue();
    }
  }

  /** * {@inheritdoc} */
  public function setValue($value$notify = TRUE) {
    $this->target = $this->getTypedDataManager()->create($this->definition->getTargetDefinition()$value);
    // Notify the parent of any changes.     if ($notify && isset($this->parent)) {
      $this->parent->onChange($this->name);
    }
  }

  /** * {@inheritdoc} */
  public function getString() {
    if (!method_exists($this, 'getType')) {
      

        $entity_type_id = NULL;
        // Relationship specifier can also contain the entity type ID, i.e.         // entity:node, entity:user or entity:taxonomy.         if (str_contains($relationship_specifier, ':')) {
          [$relationship_specifier$entity_type_id] = explode(':', $relationship_specifier, 2);
        }
        // Check for a valid relationship.         if (isset($propertyDefinitions[$relationship_specifier]) && $propertyDefinitions[$relationship_specifier] instanceof DataReferenceDefinitionInterface) {
          // If it is, use the entity type if specified already, otherwise use           // the definition.           $target_definition = $propertyDefinitions[$relationship_specifier]->getTargetDefinition();
          if (!$entity_type_id && $target_definition instanceof EntityDataDefinitionInterface) {
            $entity_type_id = $target_definition->getEntityTypeId();
          }
          $entity_type = $this->entityTypeManager->getActiveDefinition($entity_type_id);
          $field_storage_definitions = $this->entityFieldManager->getActiveFieldStorageDefinitions($entity_type_id);
          // Add the new entity base table using the table and sql column.           $base_table = $this->addNextBaseTable($entity_type$table$sql_column$field_storage);
          $propertyDefinitions = [];
          $key++;
          $index_prefix .= "$next_index_prefix.";
        }
        
$this->assertEquals([]$entity_definition->getPropertyDefinitions());
  }

  /** * Tests deriving metadata from entity references. */
  public function testEntityReferences() {
    $reference_definition = DataReferenceDefinition::create('entity');
    $this->assertInstanceOf(DataReferenceDefinitionInterface::class$reference_definition);

    // Test retrieving metadata about the referenced data.     $this->assertEquals('entity', $reference_definition->getTargetDefinition()->getDataType());
    $this->assertInstanceOf(EntityDataDefinitionInterface::class$reference_definition->getTargetDefinition());

    // Test that the definition factory creates the right definition object.     $reference_definition2 = $this->typedDataManager->createDataDefinition('entity_reference');
    $this->assertInstanceOf(DataReferenceDefinitionInterface::class$reference_definition2);
    $this->assertEquals(serialize($reference_definition)serialize($reference_definition2));
  }

  /** * Tests that an entity annotation can mark the data definition as internal. * * @dataProvider entityDefinitionIsInternalProvider */

  protected static function getAllDataReferencePropertyNames(array $candidate_definitions) {
    $reference_property_names = array_reduce($candidate_definitionsfunction Darray $reference_property_names, ComplexDataDefinitionInterface $definition) {
      $property_definitions = $definition->getPropertyDefinitions();
      foreach ($property_definitions as $property_name => $property_definition) {
        if ($property_definition instanceof DataReferenceDefinitionInterface) {
          $target_definition = $property_definition->getTargetDefinition();
          assert($target_definition instanceof EntityDataDefinitionInterface, 'Entity reference fields should only be able to reference entities.');
          $reference_property_names[] = $property_name . ':' . $target_definition->getEntityTypeId();
        }
      }
      return $reference_property_names;
    }[]);
    return array_unique($reference_property_names);
  }

  /** * Determines the reference property name for the remaining unresolved parts. * * @param \Drupal\Core\TypedData\ComplexDataDefinitionInterface[] $candidate_definitions * A list of targeted field item definitions specified by the path. * @param string[] $remaining_parts * The remaining path parts. * @param string[] $unresolved_path_parts * The unresolved path parts. * * @return string * The reference name. */
$this->assertInstanceOf(ComplexDataDefinitionInterface::class$field_item_definition);
    $this->assertEquals('field_item:string', $field_item_definition->getDataType());
    $value_definition = $field_item_definition->getPropertyDefinition('value');
    $this->assertInstanceOf(DataDefinitionInterface::class$value_definition);
    $this->assertEquals('string', $value_definition->getDataType());

    // Test deriving metadata from references.     $entity_definition = EntityDataDefinition::create($entity_type);
    $langcode_key = $this->entityTypeManager->getDefinition($entity_type)->getKey('langcode');
    $reference_definition = $entity_definition->getPropertyDefinition($langcode_key)
      ->getPropertyDefinition('language')
      ->getTargetDefinition();
    $this->assertEquals('language', $reference_definition->getDataType());

    $reference_definition = $entity_definition->getPropertyDefinition('user_id')
      ->getPropertyDefinition('entity')
      ->getTargetDefinition();

    $this->assertInstanceOf(EntityDataDefinitionInterface::class$reference_definition);
    $this->assertEquals('user', $reference_definition->getEntityTypeId(), 'Referenced entity is of type "user".');

    // Test propagating down.     $name_definition = $reference_definition->getPropertyDefinition('name');
    

  protected $id;

  /** * Gets the definition of the referenced entity. * * @return \Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface * The reference target's definition. */
  public function getTargetDefinition() {
    return $this->definition->getTargetDefinition();
  }

  /** * Checks whether the target entity has not been saved yet. * * @return bool * TRUE if the entity is new, FALSE otherwise. */
  public function isTargetNew() {
    // If only an ID is given, the reference cannot be a new entity.     return !isset($this->id) && isset($this->target) && $this->target->getValue()->isNew();
  }
$this->assertEquals(serialize($map_definition2)serialize($map_definition));
  }

  /** * Tests deriving metadata from data references. */
  public function testDataReferences() {
    $language_reference_definition = DataReferenceDefinition::create('language');
    $this->assertInstanceOf(DataReferenceDefinitionInterface::class$language_reference_definition);

    // Test retrieving metadata about the referenced data.     $this->assertEquals('language', $language_reference_definition->getTargetDefinition()->getDataType());

    // Test using the definition factory.     $language_reference_definition2 = $this->typedDataManager->createDataDefinition('language_reference');
    $this->assertInstanceOf(DataReferenceDefinitionInterface::class$language_reference_definition2);
    $this->assertEquals(serialize($language_reference_definition2)serialize($language_reference_definition));
  }

  /** * Tests getString() throws exception when getType() is not implemented. */
  public function testNotImplementedGetType() {
    
Home | Imprint | This part of the site doesn't use cookies.