function EntityReferenceItem::propertyDefinitions

Same name in other branches
  1. 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::propertyDefinitions()
  2. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::propertyDefinitions()
  3. 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::propertyDefinitions()
1 call to EntityReferenceItem::propertyDefinitions()
FileItem::propertyDefinitions in core/modules/file/src/Plugin/Field/FieldType/FileItem.php
1 method overrides EntityReferenceItem::propertyDefinitions()
FileItem::propertyDefinitions in core/modules/file/src/Plugin/Field/FieldType/FileItem.php

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php, line 68

Class

EntityReferenceItem
Defines the 'entity_reference' entity field type.

Namespace

Drupal\Core\Field\Plugin\Field\FieldType

Code

public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $settings = $field_definition->getSettings();
    $target_type_info = \Drupal::entityTypeManager()->getDefinition($settings['target_type']);
    // If the target entity type doesn't have an ID key, we cannot determine
    // the target_id data type.
    if (!$target_type_info->hasKey('id')) {
        throw new FieldException('Entity type "' . $target_type_info->id() . '" has no ID key and cannot be targeted by entity reference field "' . $field_definition->getName() . '"');
    }
    $target_id_data_type = 'string';
    if ($target_type_info->entityClassImplements(FieldableEntityInterface::class)) {
        $id_definition = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions($settings['target_type'])[$target_type_info->getKey('id')];
        if ($id_definition->getType() === 'integer') {
            $target_id_data_type = 'integer';
        }
    }
    if ($target_id_data_type === 'integer') {
        $target_id_definition = DataReferenceTargetDefinition::create('integer')->setLabel(new TranslatableMarkup('@label ID', [
            '@label' => $target_type_info->getLabel(),
        ]))
            ->setSetting('unsigned', TRUE);
    }
    else {
        $target_id_definition = DataReferenceTargetDefinition::create('string')->setLabel(new TranslatableMarkup('@label ID', [
            '@label' => $target_type_info->getLabel(),
        ]));
    }
    $target_id_definition->setRequired(TRUE);
    $properties['target_id'] = $target_id_definition;
    $properties['entity'] = DataReferenceDefinition::create('entity')->setLabel($target_type_info->getLabel())
        ->setDescription(new TranslatableMarkup('The referenced entity'))
        ->setComputed(TRUE)
        ->setReadOnly(FALSE)
        ->setTargetDefinition(EntityDataDefinition::create($settings['target_type']))
        ->addConstraint('EntityType', $settings['target_type']);
    return $properties;
}

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