You are here

public static function File::baseFieldDefinitions in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/file/src/Entity/File.php \Drupal\file\Entity\File::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/file/src/Entity/File.php, line 219
Contains \Drupal\file\Entity\File.

Class

File
Defines the file entity class.

Namespace

Drupal\file\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields['fid'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('File ID'))
    ->setDescription(t('The file ID.'))
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE);
  $fields['uuid'] = BaseFieldDefinition::create('uuid')
    ->setLabel(t('UUID'))
    ->setDescription(t('The file UUID.'))
    ->setReadOnly(TRUE);
  $fields['langcode'] = BaseFieldDefinition::create('language')
    ->setLabel(t('Language code'))
    ->setDescription(t('The file language code.'));
  $fields['uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('User ID'))
    ->setDescription(t('The user ID of the file.'))
    ->setSetting('target_type', 'user');
  $fields['filename'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Filename'))
    ->setDescription(t('Name of the file with no path components.'));
  $fields['uri'] = BaseFieldDefinition::create('uri')
    ->setLabel(t('URI'))
    ->setDescription(t('The URI to access the file (either local or remote).'))
    ->setSetting('max_length', 255)
    ->setSetting('case_sensitive', TRUE)
    ->addConstraint('FileUriUnique');
  $fields['filemime'] = BaseFieldDefinition::create('string')
    ->setLabel(t('File MIME type'))
    ->setSetting('is_ascii', TRUE)
    ->setDescription(t("The file's MIME type."));
  $fields['filesize'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('File size'))
    ->setDescription(t('The size of the file in bytes.'))
    ->setSetting('unsigned', TRUE)
    ->setSetting('size', 'big');
  $fields['status'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Status'))
    ->setDescription(t('The status of the file, temporary (FALSE) and permanent (TRUE).'))
    ->setDefaultValue(FALSE);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created'))
    ->setDescription(t('The timestamp that the file was created.'));
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The timestamp that the file was last changed.'));
  return $fields;
}