You are here

public static function MenuLinkContent::baseFieldDefinitions in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/menu_link_content/src/Entity/MenuLinkContent.php \Drupal\menu_link_content\Entity\MenuLinkContent::baseFieldDefinitions()
  2. 10 core/modules/menu_link_content/src/Entity/MenuLinkContent.php \Drupal\menu_link_content\Entity\MenuLinkContent::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 EditorialContentEntityBase::baseFieldDefinitions

See also

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

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

File

core/modules/menu_link_content/src/Entity/MenuLinkContent.php, line 267

Class

MenuLinkContent
Defines the menu link content entity class.

Namespace

Drupal\menu_link_content\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {

  /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
  $fields = parent::baseFieldDefinitions($entity_type);

  // Add the publishing status field.
  $fields += static::publishedBaseFieldDefinitions($entity_type);
  $fields['id']
    ->setLabel(t('Entity ID'))
    ->setDescription(t('The entity ID for this menu link content entity.'));
  $fields['uuid']
    ->setDescription(t('The content menu link UUID.'));
  $fields['langcode']
    ->setDescription(t('The menu link language code.'));
  $fields['bundle']
    ->setDescription(t('The content menu link bundle.'))
    ->setSetting('max_length', EntityTypeInterface::BUNDLE_MAX_LENGTH)
    ->setSetting('is_ascii', TRUE);
  $fields['title'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Menu link title'))
    ->setDescription(t('The text to be used for this link in the menu.'))
    ->setRequired(TRUE)
    ->setTranslatable(TRUE)
    ->setRevisionable(TRUE)
    ->setSetting('max_length', 255)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => -5,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => -5,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['description'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Description'))
    ->setDescription(t('Shown when hovering over the menu link.'))
    ->setTranslatable(TRUE)
    ->setRevisionable(TRUE)
    ->setSetting('max_length', 255)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => 0,
  ]);
  $fields['menu_name'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Menu name'))
    ->setDescription(t('The menu name. All links with the same menu name (such as "tools") are part of the same menu.'))
    ->setDefaultValue('tools')
    ->setSetting('is_ascii', TRUE);
  $fields['link'] = BaseFieldDefinition::create('link')
    ->setLabel(t('Link'))
    ->setDescription(t('The location this menu link points to.'))
    ->setRevisionable(TRUE)
    ->setRequired(TRUE)
    ->setSettings([
    'link_type' => LinkItemInterface::LINK_GENERIC,
    'title' => DRUPAL_DISABLED,
  ])
    ->setDisplayOptions('form', [
    'type' => 'link_default',
    'weight' => -2,
  ]);
  $fields['external'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('External'))
    ->setDescription(t('A flag to indicate if the link points to a full URL starting with a protocol, like http:// (1 = external, 0 = internal).'))
    ->setDefaultValue(FALSE)
    ->setRevisionable(TRUE);
  $fields['rediscover'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Indicates whether the menu link should be rediscovered'))
    ->setDefaultValue(FALSE);
  $fields['weight'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Weight'))
    ->setDescription(t('Link weight among links in the same menu at the same depth. In the menu, the links with high weight will sink and links with a low weight will be positioned nearer the top.'))
    ->setDefaultValue(0)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'number_integer',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'type' => 'number',
    'weight' => 20,
  ]);
  $fields['expanded'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Show as expanded'))
    ->setDescription(t('If selected and this menu link has children, the menu will always appear expanded. This option may be overridden for the entire menu tree when placing a menu block.'))
    ->setDefaultValue(FALSE)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'boolean',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'settings' => [
      'display_label' => TRUE,
    ],
    'weight' => 0,
  ]);

  // Override some properties of the published field added by
  // \Drupal\Core\Entity\EntityPublishedTrait::publishedBaseFieldDefinitions().
  $fields['enabled']
    ->setLabel(t('Enabled'));
  $fields['enabled']
    ->setDescription(t('A flag for whether the link should be enabled in menus or hidden.'));
  $fields['enabled']
    ->setTranslatable(FALSE);
  $fields['enabled']
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'boolean',
    'weight' => 0,
  ]);
  $fields['enabled']
    ->setDisplayOptions('form', [
    'settings' => [
      'display_label' => TRUE,
    ],
    'weight' => -1,
  ]);
  $fields['parent'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Parent plugin ID'))
    ->setDescription(t('The ID of the parent menu link plugin, or empty string when at the top level of the hierarchy.'));
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time that the menu link was last edited.'))
    ->setTranslatable(TRUE)
    ->setRevisionable(TRUE);

  // @todo Keep this field hidden until we have a revision UI for menu links.
  //   @see https://www.drupal.org/project/drupal/issues/2350939
  $fields['revision_log_message']
    ->setDisplayOptions('form', [
    'region' => 'hidden',
  ]);
  return $fields;
}