function hook_entity_base_field_info

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_base_field_info()
  2. 8.9.x core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_base_field_info()
  3. 10 core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_base_field_info()

Provides custom base field definitions for a content entity type.

Field (storage) definitions returned by this hook must run through the regular field storage life-cycle operations: they need to be properly installed, updated, and uninstalled. This would typically be done through the Entity Update API provided by the entity definition update manager.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type definition.

Return value

\Drupal\Core\Field\FieldDefinitionInterface[] An array of field definitions, keyed by field name.

See also

hook_entity_base_field_info_alter()

hook_entity_bundle_field_info()

hook_entity_bundle_field_info_alter()

\Drupal\Core\Field\FieldDefinitionInterface

\Drupal\Core\Entity\EntityFieldManagerInterface::getFieldDefinitions()

\Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface

https://www.drupal.org/node/3034742

Related topics

9 functions implement hook_entity_base_field_info()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

entity_reference_test_entity_base_field_info in core/modules/system/tests/modules/entity_reference_test/entity_reference_test.module
Implements hook_entity_base_field_info().
entity_schema_test_entity_base_field_info in core/modules/system/tests/modules/entity_schema_test/entity_schema_test.module
Implements hook_entity_base_field_info().
entity_test_extra_entity_base_field_info in core/modules/system/tests/modules/entity_test_extra/entity_test_extra.module
Implements hook_entity_base_field_info().
entity_test_update_entity_base_field_info in core/modules/system/tests/modules/entity_test_update/entity_test_update.module
Implements hook_entity_base_field_info().
jsonapi_test_reference_types_entity_base_field_info in core/modules/jsonapi/tests/modules/jsonapi_test_reference_types/jsonapi_test_reference_types.module
Implements hook_entity_base_field_info().

... See full list

File

core/lib/Drupal/Core/Entity/entity.api.php, line 1945

Code

function hook_entity_base_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) {
    if ($entity_type->id() == 'node') {
        $fields = [];
        $fields['my_module_text'] = BaseFieldDefinition::create('string')->setLabel(t('The text'))
            ->setDescription(t('A text property added by my_module.'))
            ->setComputed(TRUE)
            ->setClass('\\Drupal\\my_module\\EntityComputedText');
        return $fields;
    }
}

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