You are here

public static function EditorNote::baseFieldDefinitions in Editor Notes 8

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 ContentEntityBase::baseFieldDefinitions

See also

\Drupal\Core\Entity\EntityFieldManagerInterface::getFieldDefinitions()

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

File

src/Entity/EditorNote.php, line 51

Class

EditorNote
Defines the EditorNote entity.

Namespace

Drupal\editor_note\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields['uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('UID'))
    ->setDescription(t('The {users}.uid who authored the note.'))
    ->setSetting('target_type', 'user')
    ->setDefaultValueCallback('Drupal\\node\\Entity\\Node::getCurrentUserId');
  $fields['entity_id'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Entity ID'))
    ->setDescription(t('The entity id note is attached to.'))
    ->setReadOnly(TRUE);
  $fields['entity_type'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Entity Type'))
    ->setDescription(t('The entity type note is attached to.'))
    ->setSetting('max_length', 128)
    ->setRequired(TRUE)
    ->setReadOnly(TRUE);
  $fields['bundle'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Bundle'))
    ->setDescription(t('The entity bundle note is attached to.'))
    ->setSetting('max_length', 128)
    ->setRequired(TRUE)
    ->setReadOnly(TRUE);
  $fields['revision_id'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Revision ID'))
    ->setDescription(t('The entity revision id note is attached to, or NULL if the entity type is not versioned.'))
    ->setSetting('unsigned', TRUE)
    ->setReadOnly(TRUE);
  $fields['note'] = BaseFieldDefinition::create('text_long')
    ->setLabel(t('Editor note'))
    ->setDescription('Content of the note.')
    ->setTranslatable(TRUE);
  $fields['field_machine_name'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Field machine name'))
    ->setSetting('max_length', 128)
    ->setRequired(TRUE)
    ->setReadOnly(TRUE);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Authored on'))
    ->setDescription(t('The time that the note was created, as a Unix timestamp.'));
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time that the note was last edited, as a Unix timestamp.'));
  return $fields;
}