You are here

public static function LibraryItem::baseFieldDefinitions in Paragraphs 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 EditorialContentEntityBase::baseFieldDefinitions

See also

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

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

File

modules/paragraphs_library/src/Entity/LibraryItem.php, line 81

Class

LibraryItem
Defines the LibraryItem entity.

Namespace

Drupal\paragraphs_library\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields['label'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Label'))
    ->setTranslatable(TRUE)
    ->setRevisionable(TRUE)
    ->setSettings([
    'max_length' => 255,
  ])
    ->setDisplayOptions('view', [
    'type' => 'string',
    'weight' => 0,
    'label' => 'hidden',
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => -5,
  ])
    ->setRequired(TRUE)
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['paragraphs'] = BaseFieldDefinition::create('entity_reference_revisions')
    ->setLabel(t('Paragraphs'))
    ->setRevisionable(TRUE)
    ->setSetting('target_type', 'paragraph')
    ->setSetting('handler', 'default')
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'entity_reference_revisions_entity_view',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'type' => 'paragraphs',
    'weight' => 0,
  ])
    ->setRequired(TRUE)
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Authored on'))
    ->setDescription(t('The time that the library item was created.'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDisplayOptions('form', array(
    'region' => 'hidden',
    'weight' => 0,
  ))
    ->setDisplayConfigurable('form', TRUE);
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time that the library item was last edited.'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE);
  $fields['uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Authored by'))
    ->setDescription(t('The user ID of the library item author.'))
    ->setRevisionable(TRUE)
    ->setSetting('target_type', 'user')
    ->setSetting('handler', 'default')
    ->setDefaultValueCallback('Drupal\\paragraphs_library\\Entity\\LibraryItem::getCurrentUserId')
    ->setTranslatable(TRUE)
    ->setDisplayOptions('form', array(
    'region' => 'hidden',
    'weight' => 0,
  ))
    ->setDisplayConfigurable('form', TRUE);
  $fields['status']
    ->setDisplayOptions('form', [
    'type' => 'boolean_checkbox',
    'settings' => [
      'display_label' => TRUE,
    ],
    'weight' => 3,
  ])
    ->setDisplayConfigurable('form', TRUE);
  return $fields;
}