You are here

public static function Redirect::baseFieldDefinitions in Redirect 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/Redirect.php, line 263

Class

Redirect
The redirect entity class.

Namespace

Drupal\redirect\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields['rid'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Redirect ID'))
    ->setDescription(t('The redirect ID.'))
    ->setReadOnly(TRUE);
  $fields['uuid'] = BaseFieldDefinition::create('uuid')
    ->setLabel(t('UUID'))
    ->setDescription(t('The record UUID.'))
    ->setReadOnly(TRUE);
  $fields['hash'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Hash'))
    ->setSetting('max_length', 64)
    ->setDescription(t('The redirect hash.'));
  $fields['type'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Type'))
    ->setDescription(t('The redirect type.'));
  $fields['uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('User ID'))
    ->setDescription(t('The user ID of the node author.'))
    ->setDefaultValueCallback('\\Drupal\\redirect\\Entity\\Redirect::getCurrentUserId')
    ->setSettings([
    'target_type' => 'user',
  ]);
  $fields['redirect_source'] = BaseFieldDefinition::create('redirect_source')
    ->setLabel(t('From'))
    ->setDescription(t("Enter an internal Drupal path or path alias to redirect (e.g. %example1 or %example2). Fragment anchors (e.g. %anchor) are <strong>not</strong> allowed.", [
    '%example1' => 'node/123',
    '%example2' => 'taxonomy/term/123',
    '%anchor' => '#anchor',
  ]))
    ->setRequired(TRUE)
    ->setTranslatable(FALSE)
    ->setDisplayOptions('form', [
    'type' => 'redirect_link',
    'weight' => -5,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['redirect_redirect'] = BaseFieldDefinition::create('link')
    ->setLabel(t('To'))
    ->setRequired(TRUE)
    ->setTranslatable(FALSE)
    ->setSettings([
    'link_type' => LinkItemInterface::LINK_GENERIC,
    'title' => DRUPAL_DISABLED,
  ])
    ->setDisplayOptions('form', [
    'type' => 'link',
    'weight' => -4,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['language'] = BaseFieldDefinition::create('language')
    ->setLabel(t('Language'))
    ->setDescription(t('The redirect language.'))
    ->setDisplayOptions('form', [
    'type' => 'language_select',
    'weight' => 2,
  ]);
  $fields['status_code'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Status code'))
    ->setDescription(t('The redirect status code.'))
    ->setDefaultValue(0);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created'))
    ->setDescription(t('The date when the redirect was created.'));
  return $fields;
}