You are here

public static function Quantity::baseFieldDefinitions in farmOS 2.x

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\EntityFieldManagerInterface::getFieldDefinitions()

\Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions()

File

modules/core/quantity/src/Entity/Quantity.php, line 130

Class

Quantity
Defines the Quantity entity.

Namespace

Drupal\quantity\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields += static::ownerBaseFieldDefinitions($entity_type);
  $fields += static::revisionLogBaseFieldDefinitions($entity_type);
  $fields['measure'] = BaseFieldDefinition::create('list_string')
    ->setLabel(t('Measure'))
    ->setDescription(t('The measure of the quantity.'))
    ->setRevisionable(TRUE)
    ->setDefaultValueCallback('quantity_measure_default_value')
    ->setSettings([
    'allowed_values_function' => 'quantity_measure_options',
  ])
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'list_default',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'type' => 'options_select',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('form', FALSE)
    ->setDisplayConfigurable('view', FALSE);
  $fields['value'] = BaseFieldDefinition::create('fraction')
    ->setLabel(t('Value'))
    ->setDescription(t('Value of the quantity.'))
    ->setRevisionable(TRUE)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'fraction_decimal',
    'weight' => 5,
    'settings' => [
      'precision' => 0,
      'auto_precision' => TRUE,
      'separator' => '/',
      'prefix_suffix' => FALSE,
    ],
  ])
    ->setDisplayOptions('form', [
    'type' => 'fraction_decimal',
    'weight' => 5,
    'settings' => [
      'precision' => 0,
      'auto_precision' => TRUE,
    ],
  ])
    ->setDisplayConfigurable('form', FALSE)
    ->setDisplayConfigurable('view', FALSE);
  $fields['units'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Units'))
    ->setDescription(t('Units of the quantity.'))
    ->setSetting('target_type', 'taxonomy_term')
    ->setSetting('handler_settings', [
    'target_bundles' => [
      'unit' => 'unit',
    ],
    'auto_create' => TRUE,
    'auto_create_bundle' => 'unit',
  ])
    ->setSetting('handler', 'default')
    ->setRevisionable(TRUE)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'entity_reference_label',
    'settings' => [
      'link' => FALSE,
    ],
    'weight' => 10,
  ])
    ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'weight' => 10,
  ])
    ->setDisplayConfigurable('form', FALSE)
    ->setDisplayConfigurable('view', FALSE);
  $fields['label'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Label'))
    ->setDescription(t('Label of the quantity.'))
    ->setRevisionable(TRUE)
    ->setDefaultValue('')
    ->setSetting('max_length', 255)
    ->setSetting('text_processing', 0)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => 15,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => 15,
  ])
    ->setDisplayConfigurable('form', FALSE)
    ->setDisplayConfigurable('view', FALSE);
  $fields['uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Authored by'))
    ->setDescription(t('The user ID of author of the quantity.'))
    ->setRevisionable(TRUE)
    ->setSetting('target_type', 'user')
    ->setSetting('handler', 'default')
    ->setDefaultValueCallback('Drupal\\quantity\\Entity\\Quantity::getCurrentUserId')
    ->setDisplayOptions('view', [
    'region' => 'hidden',
  ])
    ->setDisplayOptions('form', [
    'region' => 'hidden',
  ])
    ->setDisplayConfigurable('form', FALSE)
    ->setDisplayConfigurable('view', FALSE);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Authored on'))
    ->setDescription(t('The time that the quantity was created.'))
    ->setRevisionable(TRUE)
    ->setDefaultValueCallback(static::class . '::getRequestTime')
    ->setDisplayOptions('view', [
    'region' => 'hidden',
  ])
    ->setDisplayOptions('form', [
    'region' => 'hidden',
  ])
    ->setDisplayConfigurable('form', FALSE)
    ->setDisplayConfigurable('view', FALSE);
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time the quantity was last edited.'))
    ->setRevisionable(TRUE);
  return $fields;
}