ContentEntityBase::baseFieldDefinitions

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:

$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

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;
}
doc_Drupal
2016-10-29 08:56:53
Comments
Leave a Comment

Please login to continue.