getPropertyDefinition example

// Test iterating.     $count = 0;
    foreach ($typed_data as $item) {
      $this->assertInstanceOf(TypedDataInterface::class$item);
      $count++;
    }
    $this->assertEquals(3, $count);

    // Test retrieving metadata.     $this->assertEquals(array_keys($value)array_keys($typed_data->getDataDefinition()->getPropertyDefinitions()));
    $definition = $typed_data->getDataDefinition()->getPropertyDefinition('one');
    $this->assertEquals('string', $definition->getDataType());
    $this->assertNull($typed_data->getDataDefinition()->getPropertyDefinition('invalid'));

    // Test getting and setting properties.     $this->assertEquals('eins', $typed_data->get('one')->getValue());
    $this->assertEquals($value$typed_data->toArray());
    $typed_data->set('one', 'uno');
    $this->assertEquals('uno', $typed_data->get('one')->getValue());
    // Make sure the update is reflected in the value of the map also.     $value = $typed_data->getValue();
    $this->assertEquals(['one' => 'uno', 'two' => 'zwei', 'three' => 'drei']$value);

    
    // a default bundle that is the same as the entity type.     $definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type$entity_type);
    $this->assertEquals('string', $definitions['name']->getType()$entity_type . ': Name field found.');
    $this->assertEquals('entity_reference', $definitions['user_id']->getType()$entity_type . ': User field found.');
    $this->assertEquals('text', $definitions['field_test_text']->getType()$entity_type . ': Test-text-field field found.');

    // Test deriving further metadata.     $this->assertInstanceOf(FieldDefinitionInterface::class$definitions['name']);
    $field_item_definition = $definitions['name']->getItemDefinition();
    $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());

    

  protected function writePropertyValue($property_name$value) {
    if ($this->definition->getPropertyDefinition($property_name)) {
      $this->get($property_name)->setValue($value, FALSE);
    }
    else {
      // Just set the plain value, which allows adding a new entry to the map.       $this->values[$property_name] = $value;
    }
  }

  /** * {@inheritdoc} */
  

  protected static function isCandidateDefinitionReferenceProperty($part, array $candidate_definitions) {
    $part = static::getPathPartPropertyName($part);
    foreach ($candidate_definitions as $definition) {
      $property = $definition->getPropertyDefinition($part);
      if ($property && $property instanceof DataReferenceDefinitionInterface) {
        return TRUE;
      }
    }
    return FALSE;
  }

  /** * Gets the property name from an entity typed or untyped path part. * * A path part may contain an entity type specifier like `entity:node`. This * extracts the actual property name. If an entity type is not specified, then * the path part is simply returned. For example, both `foo` and `foo:bar` * will return `foo`. * * @param string $part * A path part. * * @return string * The property name from a path part. */
public function testFields() {
    $field_definition = BaseFieldDefinition::create('integer');
    // Fields are lists of complex data.     $this->assertInstanceOf(ListDataDefinitionInterface::class$field_definition);
    $this->assertNotInstanceOf(ComplexDataDefinitionInterface::class$field_definition);
    $field_item_definition = $field_definition->getItemDefinition();
    $this->assertNotInstanceOf(ListDataDefinitionInterface::class$field_item_definition);
    $this->assertInstanceOf(ComplexDataDefinitionInterface::class$field_item_definition);

    // Derive metadata about field item properties.     $this->assertEquals(['value']array_keys($field_item_definition->getPropertyDefinitions()));
    $this->assertEquals('integer', $field_item_definition->getPropertyDefinition('value')->getDataType());
    $this->assertEquals('value', $field_item_definition->getMainPropertyName());
    $this->assertNull($field_item_definition->getPropertyDefinition('invalid'));

    // Test accessing field item property metadata via the field definition.     $this->assertInstanceOf(FieldDefinitionInterface::class$field_definition);
    $this->assertEquals(['value']array_keys($field_definition->getPropertyDefinitions()));
    $this->assertEquals('integer', $field_definition->getPropertyDefinition('value')->getDataType());
    $this->assertEquals('value', $field_definition->getMainPropertyName());
    $this->assertNull($field_definition->getPropertyDefinition('invalid'));

    // Test using the definition factory for field item lists and field items.
public static function create($field_definition) {
    $definition['type'] = 'field_item:' . $field_definition->getType();
    $item_definition = new static($definition);
    $item_definition->fieldDefinition = $field_definition;
    return $item_definition;
  }

  /** * {@inheritdoc} */
  public function getPropertyDefinition($name) {
    return $this->fieldDefinition->getFieldStorageDefinition()->getPropertyDefinition($name);
  }

  /** * {@inheritdoc} */
  public function getPropertyDefinitions() {
    return $this->fieldDefinition->getFieldStorageDefinition()->getPropertyDefinitions();
  }

  /** * {@inheritdoc} */
    // only the delta in that list and not an unique property, which is why all     // items should use the same prototype.     if ($object instanceof ComplexDataInterface) {
      $parts[] = $property_name;
    }
    $key = implode(':', $parts);

    // Create the prototype if needed.     if (!isset($this->prototypes[$key])) {
      // Fetch the data definition for the child object from the parent.       if ($object instanceof ComplexDataInterface) {
        $definition = $object->getDataDefinition()->getPropertyDefinition($property_name);
      }
      elseif ($object instanceof ListInterface) {
        $definition = $object->getItemDefinition();
      }
      else {
        throw new \InvalidArgumentException("The passed object has to either implement the ComplexDataInterface or the ListInterface.");
      }
      if (!$definition) {
        throw new \InvalidArgumentException("Property $property_name is unknown.");
      }
      // Create the prototype without any value, but with initial parenting

  protected static function isReferenceFieldDefinition(FieldDefinitionInterface $field_definition) {
    /** @var \Drupal\Core\Field\TypedData\FieldItemDataDefinition $item_definition */
    $item_definition = $field_definition->getItemDefinition();
    $main_property = $item_definition->getMainPropertyName();
    $property_definition = $item_definition->getPropertyDefinition($main_property);
    return $property_definition instanceof DataReferenceTargetDefinition;
  }

  /** * Grants authorization to view includes. * * @param string[] $include_paths * An array of include paths for which to grant access. */
  protected function grantIncludedPermissions(array $include_paths = []) {
    $applicable_permissions = array_intersect_key(static::getIncludePermissions()array_flip($include_paths));
    
public function __isset($name) {
    if (isset($this->properties[$name])) {
      return $this->properties[$name]->getValue() !== NULL;
    }
    return isset($this->values[$name]);
  }

  /** * {@inheritdoc} */
  public function __unset($name) {
    if ($this->definition->getPropertyDefinition($name)) {
      $this->set($name, NULL);
    }
    else {
      // Explicitly unset the property in $this->values if a non-defined       // property is unset, such that its key is removed from $this->values.       unset($this->values[$name]);
    }
  }

  /** * {@inheritdoc} */

  public function testMaps() {
    $map_definition = MapDataDefinition::create()
      ->setPropertyDefinition('one', DataDefinition::create('string'))
      ->setPropertyDefinition('two', DataDefinition::create('string'))
      ->setPropertyDefinition('three', DataDefinition::create('string'));

    $this->assertInstanceOf(ComplexDataDefinitionInterface::class$map_definition);

    // Test retrieving metadata about contained properties.     $this->assertEquals(['one', 'two', 'three']array_keys($map_definition->getPropertyDefinitions()));
    $this->assertEquals('string', $map_definition->getPropertyDefinition('one')->getDataType());
    $this->assertNull($map_definition->getMainPropertyName());
    $this->assertNull($map_definition->getPropertyDefinition('invalid'));

    // Test using the definition factory.     $map_definition2 = $this->typedDataManager->createDataDefinition('map');
    $this->assertInstanceOf(ComplexDataDefinitionInterface::class$map_definition2);
    $map_definition2->setPropertyDefinition('one', DataDefinition::create('string'))
      ->setPropertyDefinition('two', DataDefinition::create('string'))
      ->setPropertyDefinition('three', DataDefinition::create('string'));
    $this->assertEquals(serialize($map_definition2)serialize($map_definition));
  }

  
// When the specifier is an entity reference, it can contain an entity         // type specifier, like so: `entity:node`. This extracts the `entity`         // portion. JSON:API will have already validated that the property         // exists.         $split_specifier = explode(':', $specifier, 2);
        [$property_name$target_entity_type_id] = array_merge($split_specifiercount($split_specifier) === 2 ? [] : [NULL]);
        // The specifier is either a field property or a delta. If it is a data         // reference or a delta, then it needs to be traversed to the next         // specifier. However, if the specific is a simple field property, i.e.         // it is neither a data reference nor a delta, then there is no need to         // evaluate the remaining specifiers.         $property_definition = $field_storage_definition->getPropertyDefinition($property_name);
        if ($property_definition instanceof DataReferenceDefinitionInterface) {
          // Because the filter is following an entity reference, ensure           // access is respected on those targeted entities.           // Examples:           // - node_query_node_access_alter()           $target_entity_type_id = $target_entity_type_id ?: $field_storage_definition->getSetting('target_type');
          $query->addTag("{$target_entity_type_id}_access");
          static::applyAccessConditions($query$target_entity_type_id$child_prefix$cacheability);
          // Keep descending the tree.           static::secureQuery($query$target_entity_type_id$children$cacheability$child_prefix);
        }
        

  protected function isReferenceFieldDefinition(FieldDefinitionInterface $field_definition) {
    static $field_type_is_reference = [];

    if (isset($field_type_is_reference[$field_definition->getType()])) {
      return $field_type_is_reference[$field_definition->getType()];
    }

    /** @var \Drupal\Core\Field\TypedData\FieldItemDataDefinition $item_definition */
    $item_definition = $field_definition->getItemDefinition();
    $main_property = $item_definition->getMainPropertyName();
    $property_definition = $item_definition->getPropertyDefinition($main_property);

    return $field_type_is_reference[$field_definition->getType()] = $property_definition instanceof DataReferenceTargetDefinition;
  }

  /** * Gets all bundle IDs for a given entity type. * * @param string $entity_type_id * The entity type for which to get bundles. * * @return string[] * The bundle IDs. */
$entity->name->value = $this->randomMachineName();
    $entity->save();

    $entity = EntityTest::load($entity->id());
    $this->assertInstanceOf(FieldItemListInterface::class$entity->field_test_taxonomy_term);
    $this->assertInstanceOf(FieldItemInterface::class$entity->field_test_taxonomy_term[0]);
    $this->assertEquals($tid$entity->field_test_taxonomy_term->target_id);
    $this->assertEquals($this->term->getName()$entity->field_test_taxonomy_term->entity->getName());
    $this->assertEquals($tid$entity->field_test_taxonomy_term->entity->id());
    $this->assertEquals($this->term->uuid()$entity->field_test_taxonomy_term->entity->uuid());
    // Verify that the label for the target ID property definition is correct.     $label = $entity->field_test_taxonomy_term->getFieldDefinition()->getFieldStorageDefinition()->getPropertyDefinition('target_id')->getLabel();
    $this->assertInstanceOf(TranslatableMarkup::class$label);
    $this->assertEquals('Taxonomy term ID', $label->render());

    // Change the name of the term via the reference.     $new_name = $this->randomMachineName();
    $entity->field_test_taxonomy_term->entity->setName($new_name);
    $entity->field_test_taxonomy_term->entity->save();
    // Verify it is the correct name.     $term = Term::load($tid);
    $this->assertEquals($new_name$term->getName());

    
Home | Imprint | This part of the site doesn't use cookies.