You are here

public static function Log::baseFieldDefinitions in Log entity 2.x

Same name and namespace in other branches
  1. 8 src/Entity/Log.php \Drupal\log\Entity\Log::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\EntityFieldManagerInterface::getFieldDefinitions()

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

File

src/Entity/Log.php, line 171

Class

Log
Defines the Log entity.

Namespace

Drupal\log\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['name'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Name'))
    ->setDescription(t('The name of the log. Leave this blank to automatically generate a name.'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDefaultValue('')
    ->setSetting('max_length', 255)
    ->setSetting('text_processing', 0)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => -5,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => -5,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['timestamp'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(t('Timestamp'))
    ->setDescription(t('Timestamp of the event being logged.'))
    ->setDefaultValueCallback(static::class . '::getRequestTime')
    ->setRevisionable(TRUE)
    ->setDisplayOptions('view', [
    'label' => 'above',
    'type' => 'timestamp',
    'weight' => 10,
  ])
    ->setDisplayOptions('form', [
    'type' => 'datetime_timestamp',
    'weight' => 10,
  ])
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayConfigurable('form', TRUE);
  $fields['status'] = BaseFieldDefinition::create('state')
    ->setLabel(t('Status'))
    ->setDescription(t('Indicates the status of the log.'))
    ->setRevisionable(TRUE)
    ->setRequired(TRUE)
    ->setSetting('max_length', 255)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'state_transition_form',
    'weight' => 10,
  ])
    ->setDisplayOptions('form', [
    'type' => 'options_select',
    'weight' => 11,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE)
    ->setSetting('workflow_callback', [
    '\\Drupal\\log\\Entity\\Log',
    'getWorkflowId',
  ]);
  $fields['uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Authored by'))
    ->setDescription(t('The user ID of author of the log.'))
    ->setRevisionable(TRUE)
    ->setSetting('target_type', 'user')
    ->setSetting('handler', 'default')
    ->setDefaultValueCallback('Drupal\\log\\Entity\\Log::getCurrentUserId')
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'author',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'weight' => 12,
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'autocomplete_type' => 'tags',
      'placeholder' => '',
    ],
  ])
    ->setDisplayConfigurable('view', TRUE);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Authored on'))
    ->setDescription(t('The time that the log was created.'))
    ->setRevisionable(TRUE)
    ->setDefaultValueCallback(static::class . '::getRequestTime')
    ->setDisplayOptions('form', [
    'type' => 'datetime_timestamp',
    'weight' => 13,
  ])
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayConfigurable('form', TRUE);
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time the log was last edited.'))
    ->setRevisionable(TRUE);
  return $fields;
}