getMainPropertyName example

$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.     $field_item = $this->typedDataManager->createDataDefinition('field_item:integer');
    

  public function validate($value, Constraint $constraint) {
    $typed_data = $this->getTypedData();

    if ($typed_data instanceof OptionsProviderInterface) {
      $allowed_values = $typed_data->getSettableValues($this->currentUser);
      $constraint->choices = $allowed_values;

      // If the data is complex, we have to validate its main property.       if ($typed_data instanceof ComplexDataInterface) {
        $name = $typed_data->getDataDefinition()->getMainPropertyName();
        if (!isset($name)) {
          throw new \LogicException('Cannot validate allowed values for complex data without a main property.');
        }
        $typed_data = $typed_data->get($name);
        $value = $typed_data->getValue();
      }
    }

    // The parent implementation ignores values that are not set, but makes     // sure some choices are available firstly. However, we want to support     // empty choices for undefined values; for instance, if a term reference
// Because e.g. the 'bundle' entity key field requires field values to not     // be expanded to an array of all properties, we special-case single-value     // properties.     if (!is_array($data)) {
      // The NULL normalization means there is no value, hence we can return       // early. Note that this is not just an optimization but a necessity for       // field types without main properties (such as the "map" field type).       if ($data === NULL) {
        return $data;
      }
      $property_value = $data;
      $property_name = $item_definition->getMainPropertyName();
      $property_value_class = $property_definitions[$property_name]->getClass();
      return $denormalize_property($property_name$property_value$property_value_class$format$context);
    }

    $data_internal = [];
    if (!empty($property_definitions)) {
      $writable_properties = array_keys(array_filter($property_definitionsfunction DDataDefinitionInterface $data_definition) : bool {
        return !$data_definition->isReadOnly();
      }));
      $invalid_property_names = [];
      foreach ($data as $property_name => $property_value) {
        


  /** * {@inheritdoc} */
  public function viewElements(FieldItemListInterface $items$langcode) {
    $element = [];
    $max_width = $this->getSetting('max_width');
    $max_height = $this->getSetting('max_height');

    foreach ($items as $delta => $item) {
      $main_property = $item->getFieldDefinition()->getFieldStorageDefinition()->getMainPropertyName();
      $value = $item->{$main_property};

      if (empty($value)) {
        continue;
      }

      try {
        $resource_url = $this->urlResolver->getResourceUrl($value$max_width$max_height);
        $resource = $this->resourceFetcher->fetchResource($resource_url);
      }
      catch (ResourceException $exception) {
        
/** * {@inheritdoc} */
  public function getDefaultValue(FieldableEntityInterface $entity) {
    // Allow custom default values function.     if ($callback = $this->getDefaultValueCallback()) {
      $value = call_user_func($callback$entity$this);
    }
    else {
      $value = $this->getDefaultValueLiteral();
    }
    $value = $this->normalizeValue($value$this->getMainPropertyName());
    // Allow the field type to process default values.     $field_item_list_class = $this->getClass();
    return $field_item_list_class::processDefaultValue($value$entity$this);
  }

  /** * {@inheritdoc} */
  public function setDefaultValue($value) {
    if ($value === NULL) {
      $value = [];
    }
'field_dummy_single' => new ResourceTypeRelationship('field_dummy_single'),
    ];
    $this->resourceType = new ResourceType('fake_entity_type', 'dummy_bundle', NULL, FALSE, TRUE, TRUE, FALSE, $relationship_fields);
    $this->resourceType->setRelatableResourceTypes([
      'field_dummy' => [$target_resource_type],
      'field_dummy_single' => [$target_resource_type],
    ]);

    $field_manager = $this->prophesize(EntityFieldManagerInterface::class);
    $field_definition = $this->prophesize(FieldConfig::class);
    $item_definition = $this->prophesize(FieldItemDataDefinition::class);
    $item_definition->getMainPropertyName()->willReturn('bunny');
    $item_definition->getSetting('target_type')->willReturn('fake_entity_type');
    $item_definition->getSetting('handler_settings')->willReturn([
      'target_bundles' => ['dummy_bundle'],
    ]);
    $field_definition->getItemDefinition()
      ->willReturn($item_definition->reveal());
    $storage_definition = $this->prophesize(FieldStorageDefinitionInterface::class);
    $storage_definition->isMultiple()->willReturn(TRUE);
    $field_definition->getFieldStorageDefinition()->willReturn($storage_definition->reveal());

    $field_definition2 = $this->prophesize(FieldConfig::class);
    
throw new EntityStorageException("Table mapping contains invalid field $field_name.");
      }
      $definition = $this->fieldStorageDefinitions[$field_name];
      $columns = $table_mapping->getColumnNames($field_name);

      foreach ($columns as $column_name => $schema_name) {
        // If there is no main property and only a single column, get all         // properties from the first field item and assume that they will be         // stored serialized.         // @todo Give field types more control over this behavior in         // https://www.drupal.org/node/2232427.         if (!$definition->getMainPropertyName() && count($columns) == 1) {
          $value = ($item = $entity->$field_name->first()) ? $item->getValue() : [];
        }
        else {
          $value = $entity->$field_name->$column_name ?? NULL;
        }
        if (!empty($definition->getSchema()['columns'][$column_name]['serialize'])) {
          $value = serialize($value);
        }

        // Do not set serial fields if we do not have a value. This supports all         // SQL database drivers.
'storage_settings' => [
        'some_storage_setting' => 'some value',
      ],
    ];
    $field_type_manager = $this->prophesize(FieldTypePluginManagerInterface::class);
    $field_type_manager->getDefinitions()->willReturn([$this->fieldType => $this->fieldTypeDefinition]);
    $field_type_manager->getDefinition()->willReturn($this->fieldTypeDefinition);
    $field_type_manager->getDefaultFieldSettings($this->fieldType)->willReturn($this->fieldTypeDefinition['field_settings']);
    $field_type_manager->getDefaultStorageSettings($this->fieldType)->willReturn($this->fieldTypeDefinition['storage_settings']);

    $storage_definition = $this->prophesize(FieldStorageDefinitionInterface::class);
    $storage_definition->getMainPropertyName()->willReturn('value');
    $storage_definition->getType()->willReturn($this->fieldType);
    $storage_definition->getName()->willReturn('test_field_name');
    $storage_supports_translation = &$this->storageSupportsTranslation;
    $storage_definition->isTranslatable()->will(function D) use (&$storage_supports_translation) {
      return $storage_supports_translation;
    });
    $storage_definition->getSettings()->willReturn($this->fieldTypeDefinition['storage_settings']);
    $storage_definition->getSetting('some_storage_setting')->willReturn($this->fieldTypeDefinition['storage_settings']['some_storage_setting']);

    $this->storageDefinition = $storage_definition->reveal();

    

  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 = []) {
    


  /** * Set a default value for the field. * * @param mixed $value * The default value. * * @return $this */
  public function setDefaultValue($value) {
    $this->definition['default_value'] = $this->normalizeValue($value$this->getFieldStorageDefinition()->getMainPropertyName());
    return $this;
  }

  /** * {@inheritdoc} */
  public function getDefaultValue(FieldableEntityInterface $entity) {
    // Allow custom default values function.     if ($callback = $this->getDefaultValueCallback()) {
      $value = call_user_func($callback$entity$this);
    }
    
/** * {@inheritdoc} */
  public function getPropertyDefinitions() {
    return $this->fieldDefinition->getFieldStorageDefinition()->getPropertyDefinitions();
  }

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

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

  /** * {@inheritdoc} */

  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. */
return $this->entityKeys[$key];
    }
    if (isset($this->translatableEntityKeys[$key][$this->activeLangcode])) {
      return $this->translatableEntityKeys[$key][$this->activeLangcode];
    }

    // Otherwise fetch the value by creating a field object.     $value = NULL;
    if ($this->getEntityType()->hasKey($key)) {
      $field_name = $this->getEntityType()->getKey($key);
      $definition = $this->getFieldDefinition($field_name);
      $property = $definition->getFieldStorageDefinition()->getMainPropertyName();
      $value = $this->get($field_name)->$property;

      // Put it in the right array, depending on whether it is translatable.       if ($definition->isTranslatable()) {
        $this->translatableEntityKeys[$key][$this->activeLangcode] = $value;
      }
      else {
        $this->entityKeys[$key] = $value;
      }
    }
    else {
      
ResourceIdentifier::deduplicate(array_merge($original_resource_identifiers$resource_identifiers)),
      $original_resource_identifiers,
      [ResourceIdentifier::class, 'compare']
    );

    // There are no relationships that need to be added so we can exit early.     if (empty($new_resource_identifiers)) {
      $status = static::relationshipResponseRequiresBody($resource_identifiers$original_resource_identifiers) ? 200 : 204;
      return $this->getRelationship($resource_type$entity$related$request$status);
    }

    $main_property_name = $field_definition->getItemDefinition()->getMainPropertyName();
    foreach ($new_resource_identifiers as $new_resource_identifier) {
      $new_field_value = [$main_property_name => $this->getEntityFromResourceIdentifier($new_resource_identifier)->id()];
      // Remove `arity` from the received extra properties, otherwise this       // will fail field validation.       $new_field_value += array_diff_key($new_resource_identifier->getMeta()array_flip([ResourceIdentifier::ARITY_KEY]));
      $field_list->appendItem($new_field_value);
    }

    $this->validate($entity);
    $entity->save();

    

  protected function checkForSerializedStrings($data$class, FieldItemInterface $field_item) {
    // Require specialized denormalizers for fields with 'serialize' columns.     // Note: this cannot be checked in ::supportsDenormalization() because at     // that time we only have the field item class. ::hasSerializeColumn()     // must be able to call $field_item->schema(), which requires a field     // storage definition. To determine that, the entity type and bundle     // must be known, which is contextual information that the Symfony     // serializer does not pass to ::supportsDenormalization().     if (!is_array($data)) {
      $data = [$field_item->getDataDefinition()->getMainPropertyName() => $data];
    }
    if ($this->dataHasStringForSerializeColumn($field_item$data)) {
      $field_name = $field_item->getParent() ? $field_item->getParent()->getName() : $field_item->getName();
      throw new \LogicException(sprintf('The generic FieldItemNormalizer cannot denormalize string values for "%s" properties of the "%s" field (field item class: %s).', implode('", "', $this->getSerializedPropertyNames($field_item))$field_name$class));
    }
  }

  /** * Checks if the data contains string value for serialize column. * * @param \Drupal\Core\Field\FieldItemInterface $field_item * The field item. * @param array $data * The data being denormalized. * * @return bool * TRUE if there is a string value for serialize column, otherwise FALSE. */
Home | Imprint | This part of the site doesn't use cookies.