public static function TokenCustom::baseFieldDefinitions in Custom Tokens 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/ TokenCustom.php, line 59
Class
- TokenCustom
- Defines the token_custom entity class.
Namespace
Drupal\token_custom\EntityCode
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['machine_name'] = BaseFieldDefinition::create('string')
->setLabel(t('Machine name ID'))
->setDescription(t('A unique machine-readable name for this token. It must only contain lowercase letters, numbers, and underscores.'))
->setSetting('max_length', 64)
->setDisplayOptions('form', [
'label' => 'hidden',
'type' => 'string_textfield',
'weight' => 1,
])
->setDisplayConfigurable('form', TRUE);
$fields['type'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Custom Token Type'))
->setDescription(t('Custom Token Type.'))
->setSetting('target_type', 'token_custom_type')
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => 2,
]);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('Administrative name.'))
->setRequired(TRUE)
->setTranslatable(FALSE)
->setRevisionable(FALSE)
->setDefaultValue('')
->setSetting('max_length', 255)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE);
$fields['description'] = BaseFieldDefinition::create('string')
->setLabel(t('Description'))
->setDescription(t('Description.'))
->setRequired(FALSE)
->setRevisionable(FALSE)
->setDefaultValue('')
->setSetting('max_length', 255)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 3,
])
->setDisplayConfigurable('form', TRUE)
->setTranslatable(TRUE);
$fields['content'] = BaseFieldDefinition::create('text_long')
->setLabel(t('Content'))
->setDescription(t('The content that will replace this token.'))
->setRequired(TRUE)
->setRevisionable(FALSE)
->setDefaultValue([
'value' => '',
'format' => 'plain_text',
])
->setDisplayOptions('form', [
'type' => 'text_textarea',
'weight' => 4,
])
->setDisplayConfigurable('form', TRUE)
->setTranslatable(TRUE);
$fields['is_new'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Is new'))
->setDescription(t('TRUE if token has been created and not edited before.'))
->setReadOnly(TRUE)
->setRevisionable(FALSE)
->setTranslatable(FALSE);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The language code for the entity.'))
->setDisplayOptions('form', [
'type' => 'language_select',
'weight' => 10,
])
->setDisplayConfigurable('form', TRUE);
return $fields;
}