public static ContentEntityBase::baseFieldDefinitions(EntityTypeInterface $entity_type)
Provides base field definitions for an entity type.
Implementations typically use the class \Drupal\Core\Field\BaseFieldDefinition for creating the field definitions; for example a 'name' field could be defined as the following:
1 2 | $fields [ 'name' ] = BaseFieldDefinition::create( 'string' ) ->setLabel(t( 'Name' )); |
By definition, base fields are fields that exist for every bundle. To provide definitions for fields that should only exist on some bundles, use \Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions().
The definitions returned by this function can be overridden for all bundles by hook_entity_base_field_info_alter() or overridden on a per-bundle basis via 'base_field_override' configuration entities.
Parameters
\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type definition. Useful when a single class is used for multiple, possibly dynamic entity types.
Return value
\Drupal\Core\Field\FieldDefinitionInterface[] An array of base field definitions for the entity type, keyed by field name.
Overrides FieldableEntityInterface::baseFieldDefinitions
See also
\Drupal\Core\Entity\EntityManagerInterface::getFieldDefinitions()
\Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions()
File
- core/lib/Drupal/Core/Entity/ContentEntityBase.php, line 1122
Class
- ContentEntityBase
- Implements Entity Field API specific enhancements to the Entity class.
Namespace
Drupal\Core\Entity
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | public static function baseFieldDefinitions(EntityTypeInterface $entity_type ) { $fields = []; if ( $entity_type ->hasKey( 'id' )) { $fields [ $entity_type ->getKey( 'id' )] = BaseFieldDefinition::create( 'integer' ) ->setLabel( new TranslatableMarkup( 'ID' )) ->setReadOnly(TRUE) ->setSetting( 'unsigned' , TRUE); } if ( $entity_type ->hasKey( 'uuid' )) { $fields [ $entity_type ->getKey( 'uuid' )] = BaseFieldDefinition::create( 'uuid' ) ->setLabel( new TranslatableMarkup( 'UUID' )) ->setReadOnly(TRUE); } if ( $entity_type ->hasKey( 'revision' )) { $fields [ $entity_type ->getKey( 'revision' )] = BaseFieldDefinition::create( 'integer' ) ->setLabel( new TranslatableMarkup( 'Revision ID' )) ->setReadOnly(TRUE) ->setSetting( 'unsigned' , TRUE); } if ( $entity_type ->hasKey( 'langcode' )) { $fields [ $entity_type ->getKey( 'langcode' )] = BaseFieldDefinition::create( 'language' ) ->setLabel( new TranslatableMarkup( 'Language' )) ->setDisplayOptions( 'view' , [ 'type' => 'hidden' , ]) ->setDisplayOptions( 'form' , [ 'type' => 'language_select' , 'weight' => 2, ]); if ( $entity_type ->isRevisionable()) { $fields [ $entity_type ->getKey( 'langcode' )]->setRevisionable(TRUE); } if ( $entity_type ->isTranslatable()) { $fields [ $entity_type ->getKey( 'langcode' )]->setTranslatable(TRUE); } } if ( $entity_type ->hasKey( 'bundle' )) { if ( $bundle_entity_type_id = $entity_type ->getBundleEntityType()) { $fields [ $entity_type ->getKey( 'bundle' )] = BaseFieldDefinition::create( 'entity_reference' ) ->setLabel( $entity_type ->getBundleLabel()) ->setSetting( 'target_type' , $bundle_entity_type_id ) ->setRequired(TRUE) ->setReadOnly(TRUE); } else { $fields [ $entity_type ->getKey( 'bundle' )] = BaseFieldDefinition::create( 'string' ) ->setLabel( $entity_type ->getBundleLabel()) ->setRequired(TRUE) ->setReadOnly(TRUE); } } return $fields ; } |
Please login to continue.