public static Comment::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 ContentEntityBase::baseFieldDefinitions
See also
\Drupal\Core\Entity\EntityManagerInterface::getFieldDefinitions()
\Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions()
File
- core/modules/comment/src/Entity/Comment.php, line 210
Class
- Comment
- Defines the comment entity class.
Namespace
Drupal\comment\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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | public static function baseFieldDefinitions(EntityTypeInterface $entity_type ) { /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */ $fields = parent::baseFieldDefinitions( $entity_type ); $fields [ 'cid' ]->setLabel(t( 'Comment ID' )) ->setDescription(t( 'The comment ID.' )); $fields [ 'uuid' ]->setDescription(t( 'The comment UUID.' )); $fields [ 'comment_type' ]->setLabel(t( 'Comment Type' )) ->setDescription(t( 'The comment type.' )); $fields [ 'langcode' ]->setDescription(t( 'The comment language code.' )); $fields [ 'pid' ] = BaseFieldDefinition::create( 'entity_reference' ) ->setLabel(t( 'Parent ID' )) ->setDescription(t( 'The parent comment ID if this is a reply to a comment.' )) ->setSetting( 'target_type' , 'comment' ); $fields [ 'entity_id' ] = BaseFieldDefinition::create( 'entity_reference' ) ->setLabel(t( 'Entity ID' )) ->setDescription(t( 'The ID of the entity of which this comment is a reply.' )) ->setRequired(TRUE); $fields [ 'subject' ] = BaseFieldDefinition::create( 'string' ) ->setLabel(t( 'Subject' )) ->setTranslatable(TRUE) ->setSetting( 'max_length' , 64) ->setDisplayOptions( 'form' , array ( 'type' => 'string_textfield' , // Default comment body field has weight 20. 'weight' => 10, )) ->setDisplayConfigurable( 'form' , TRUE); $fields [ 'uid' ] = BaseFieldDefinition::create( 'entity_reference' ) ->setLabel(t( 'User ID' )) ->setDescription(t( 'The user ID of the comment author.' )) ->setTranslatable(TRUE) ->setSetting( 'target_type' , 'user' ) ->setDefaultValue(0); $fields [ 'name' ] = BaseFieldDefinition::create( 'string' ) ->setLabel(t( 'Name' )) ->setDescription(t( "The comment author's name." )) ->setTranslatable(TRUE) ->setSetting( 'max_length' , 60) ->setDefaultValue( '' ); $fields [ 'mail' ] = BaseFieldDefinition::create( 'email' ) ->setLabel(t( 'Email' )) ->setDescription(t( "The comment author's email address." )) ->setTranslatable(TRUE); $fields [ 'homepage' ] = BaseFieldDefinition::create( 'uri' ) ->setLabel(t( 'Homepage' )) ->setDescription(t( "The comment author's home page address." )) ->setTranslatable(TRUE) // URIs are not length limited by RFC 2616, but we can only store 255 // characters in our comment DB schema. ->setSetting( 'max_length' , 255); $fields [ 'hostname' ] = BaseFieldDefinition::create( 'string' ) ->setLabel(t( 'Hostname' )) ->setDescription(t( "The comment author's hostname." )) ->setTranslatable(TRUE) ->setSetting( 'max_length' , 128); $fields [ 'created' ] = BaseFieldDefinition::create( 'created' ) ->setLabel(t( 'Created' )) ->setDescription(t( 'The time that the comment was created.' )) ->setTranslatable(TRUE); $fields [ 'changed' ] = BaseFieldDefinition::create( 'changed' ) ->setLabel(t( 'Changed' )) ->setDescription(t( 'The time that the comment was last edited.' )) ->setTranslatable(TRUE); $fields [ 'status' ] = BaseFieldDefinition::create( 'boolean' ) ->setLabel(t( 'Publishing status' )) ->setDescription(t( 'A boolean indicating whether the comment is published.' )) ->setTranslatable(TRUE) ->setDefaultValue(TRUE); $fields [ 'thread' ] = BaseFieldDefinition::create( 'string' ) ->setLabel(t( 'Thread place' )) ->setDescription(t( "The alphadecimal representation of the comment's place in a thread, consisting of a base 36 string prefixed by an integer indicating its length." )) ->setSetting( 'max_length' , 255); $fields [ 'entity_type' ] = BaseFieldDefinition::create( 'string' ) ->setLabel(t( 'Entity type' )) ->setDescription(t( 'The entity type to which this comment is attached.' )) ->setSetting( 'is_ascii' , TRUE) ->setSetting( 'max_length' , EntityTypeInterface::ID_MAX_LENGTH); $fields [ 'field_name' ] = BaseFieldDefinition::create( 'string' ) ->setLabel(t( 'Comment field name' )) ->setDescription(t( 'The field name through which this comment was added.' )) ->setSetting( 'is_ascii' , TRUE) ->setSetting( 'max_length' , FieldStorageConfig::NAME_MAX_LENGTH); return $fields ; } |
Please login to continue.