Item::baseFieldDefinitions

public static Item::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/aggregator/src/Entity/Item.php, line 49

Class

Item
Defines the aggregator item entity class.

Namespace

Drupal\aggregator\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
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
  $fields = parent::baseFieldDefinitions($entity_type);
 
  $fields['iid']->setLabel(t('Aggregator item ID'))
    ->setDescription(t('The ID of the feed item.'));
 
  $fields['langcode']->setLabel(t('Language code'))
    ->setDescription(t('The feed item language code.'));
 
  $fields['fid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Source feed'))
    ->setRequired(TRUE)
    ->setDescription(t('The aggregator feed entity associated with this item.'))
    ->setSetting('target_type', 'aggregator_feed')
    ->setDisplayOptions('view', array(
      'label' => 'hidden',
      'type' => 'entity_reference_label',
      'weight' => 0,
    ))
    ->setDisplayConfigurable('form', TRUE);
 
  $fields['title'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Title'))
    ->setDescription(t('The title of the feed item.'));
 
  $fields['link'] = BaseFieldDefinition::create('uri')
    ->setLabel(t('Link'))
    ->setDescription(t('The link of the feed item.'))
    ->setDisplayOptions('view', array(
      'type' => 'hidden',
    ))
    ->setDisplayConfigurable('view', TRUE);
 
  $fields['author'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Author'))
    ->setDescription(t('The author of the feed item.'))
    ->setDisplayOptions('view', array(
      'label' => 'hidden',
      'weight' => 3,
    ))
    ->setDisplayConfigurable('view', TRUE);
 
  $fields['description'] = BaseFieldDefinition::create('string_long')
    ->setLabel(t('Description'))
    ->setDescription(t('The body of the feed item.'));
 
  $fields['timestamp'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Posted on'))
    ->setDescription(t('Posted date of the feed item, as a Unix timestamp.'))
    ->setDisplayOptions('view', array(
      'label' => 'hidden',
      'type' => 'timestamp_ago',
      'weight' => 1,
    ))
    ->setDisplayConfigurable('view', TRUE);
 
  // @todo Convert to a real UUID field in
  $fields['guid'] = BaseFieldDefinition::create('string_long')
    ->setLabel(t('GUID'))
    ->setDescription(t('Unique identifier for the feed item.'));
 
  return $fields;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.