You are here

public static function Term::baseFieldDefinitions in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/taxonomy/src/Entity/Term.php \Drupal\taxonomy\Entity\Term::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/taxonomy/src/Entity/Term.php, line 108
Contains \Drupal\taxonomy\Entity\Term.

Class

Term
Defines the taxonomy term entity.

Namespace

Drupal\taxonomy\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields['tid'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Term ID'))
    ->setDescription(t('The term ID.'))
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE);
  $fields['uuid'] = BaseFieldDefinition::create('uuid')
    ->setLabel(t('UUID'))
    ->setDescription(t('The term UUID.'))
    ->setReadOnly(TRUE);
  $fields['vid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Vocabulary'))
    ->setDescription(t('The vocabulary to which the term is assigned.'))
    ->setSetting('target_type', 'taxonomy_vocabulary');
  $fields['langcode'] = BaseFieldDefinition::create('language')
    ->setLabel(t('Language'))
    ->setDescription(t('The term language code.'))
    ->setTranslatable(TRUE)
    ->setDisplayOptions('view', array(
    'type' => 'hidden',
  ))
    ->setDisplayOptions('form', array(
    'type' => 'language_select',
    'weight' => 2,
  ));
  $fields['name'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Name'))
    ->setDescription(t('The term name.'))
    ->setTranslatable(TRUE)
    ->setRequired(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['description'] = BaseFieldDefinition::create('text_long')
    ->setLabel(t('Description'))
    ->setDescription(t('A description of the term.'))
    ->setTranslatable(TRUE)
    ->setDisplayOptions('view', array(
    'label' => 'hidden',
    'type' => 'text_default',
    'weight' => 0,
  ))
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayOptions('form', array(
    'type' => 'text_textfield',
    'weight' => 0,
  ))
    ->setDisplayConfigurable('form', TRUE);
  $fields['weight'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Weight'))
    ->setDescription(t('The weight of this term in relation to other terms.'))
    ->setDefaultValue(0);
  $fields['parent'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Term Parents'))
    ->setDescription(t('The parents of this term.'))
    ->setSetting('target_type', 'taxonomy_term')
    ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
    ->setCustomStorage(TRUE);
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time that the term was last edited.'))
    ->setTranslatable(TRUE);
  return $fields;
}