You are here

public static function Item::baseFieldDefinitions in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/aggregator/src/Entity/Item.php \Drupal\aggregator\Entity\Item::baseFieldDefinitions()

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:

$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/modules/aggregator/src/Entity/Item.php, line 54
Contains \Drupal\aggregator\Entity\Item.

Class

Item
Defines the aggregator item entity class.

Namespace

Drupal\aggregator\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields['iid'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Aggregator item ID'))
    ->setDescription(t('The ID of the feed item.'))
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE);
  $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['langcode'] = BaseFieldDefinition::create('language')
    ->setLabel(t('Language code'))
    ->setDescription(t('The feed item language code.'));
  $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
  //   https://www.drupal.org/node/2149851.
  $fields['guid'] = BaseFieldDefinition::create('string_long')
    ->setLabel(t('GUID'))
    ->setDescription(t('Unique identifier for the feed item.'));
  return $fields;
}