Node::baseFieldDefinitions

public static Node::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/node/src/Entity/Node.php, line 368

Class

Node
Defines the node entity class.

Namespace

Drupal\node\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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);
 
  $fields['title'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Title'))
    ->setRequired(TRUE)
    ->setTranslatable(TRUE)
    ->setRevisionable(TRUE)
    ->setSetting('max_length', 255)
    ->setDisplayOptions('view', array(
      'label' => 'hidden',
      'type' => 'string',
      'weight' => -5,
    ))
    ->setDisplayOptions('form', array(
      'type' => 'string_textfield',
      'weight' => -5,
    ))
    ->setDisplayConfigurable('form', TRUE);
 
  $fields['uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Authored by'))
    ->setDescription(t('The username of the content author.'))
    ->setRevisionable(TRUE)
    ->setSetting('target_type', 'user')
    ->setDefaultValueCallback('Drupal\node\Entity\Node::getCurrentUserId')
    ->setTranslatable(TRUE)
    ->setDisplayOptions('view', array(
      'label' => 'hidden',
      'type' => 'author',
      'weight' => 0,
    ))
    ->setDisplayOptions('form', array(
      'type' => 'entity_reference_autocomplete',
      'weight' => 5,
      'settings' => array(
        'match_operator' => 'CONTAINS',
        'size' => '60',
        'placeholder' => '',
      ),
    ))
    ->setDisplayConfigurable('form', TRUE);
 
  $fields['status'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Publishing status'))
    ->setDescription(t('A boolean indicating whether the node is published.'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDefaultValue(TRUE);
 
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Authored on'))
    ->setDescription(t('The time that the node was created.'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDisplayOptions('view', array(
      'label' => 'hidden',
      'type' => 'timestamp',
      'weight' => 0,
    ))
    ->setDisplayOptions('form', array(
      'type' => 'datetime_timestamp',
      'weight' => 10,
    ))
    ->setDisplayConfigurable('form', TRUE);
 
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time that the node was last edited.'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE);
 
  $fields['promote'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Promoted to front page'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDefaultValue(TRUE)
    ->setDisplayOptions('form', array(
      'type' => 'boolean_checkbox',
      'settings' => array(
        'display_label' => TRUE,
      ),
      'weight' => 15,
    ))
    ->setDisplayConfigurable('form', TRUE);
 
  $fields['sticky'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Sticky at top of lists'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDefaultValue(FALSE)
    ->setDisplayOptions('form', array(
      'type' => 'boolean_checkbox',
      'settings' => array(
        'display_label' => TRUE,
      ),
      'weight' => 16,
    ))
    ->setDisplayConfigurable('form', TRUE);
 
  $fields['revision_timestamp'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Revision timestamp'))
    ->setDescription(t('The time that the current revision was created.'))
    ->setQueryable(FALSE)
    ->setRevisionable(TRUE);
 
  $fields['revision_uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Revision user ID'))
    ->setDescription(t('The user ID of the author of the current revision.'))
    ->setSetting('target_type', 'user')
    ->setQueryable(FALSE)
    ->setRevisionable(TRUE);
 
  $fields['revision_log'] = BaseFieldDefinition::create('string_long')
    ->setLabel(t('Revision log message'))
    ->setDescription(t('Briefly describe the changes you have made.'))
    ->setRevisionable(TRUE)
    ->setDefaultValue('')
    ->setDisplayOptions('form', array(
      'type' => 'string_textarea',
      'weight' => 25,
      'settings' => array(
        'rows' => 4,
      ),
    ));
 
  $fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Revision translation affected'))
    ->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))
    ->setReadOnly(TRUE)
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE);
 
  return $fields;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.