Skip to content

StringField

A helper for working with string fields.


  • Full name: \Drupal\helper\Field\StringField
  • Parent class: FieldStorageHelperBase

Properties

classResolver

The class resolver, used to create the Custom Field helper.

protected \Drupal\Core\DependencyInjection\ClassResolverInterface $classResolver

Methods

create

{@inheritdoc}

public static create(\Symfony\Component\DependencyInjection\ContainerInterface $container): mixed
  • This method is static. Parameters:
Parameter Type Description
$container \Symfony\Component\DependencyInjection\ContainerInterface

changeLength

Change a string field's length.

public changeLength(string $entityTypeId, string $fieldName, int $length, string|null $propertyName = null): void

Once a field has data, the length cannot be changed. Sometimes, we find a need to expand the length of the field to support data longer than had been expected.

This does support shortening the length of the field, but an exception will be thrown if the table contains data longer than the new length.

This will only work with the following conditions: - The field is not base field. - The field is not deleted. - The field has a max_length setting. Custom Field subfields are passed to CustomField::changeLength(). - The entity type must be using SQL storage.

Note, this does not wrap the SQL change in a transaction. This is because \Drupal\Core\Database\Schema::executeDdlStatement() does not support being in an active transaction.

Parameters:

Parameter Type Description
$entityTypeId string
The entity type ID.
$fieldName string
The field name.
$length int
The new field length.
$propertyName string|null
The field property name. The main property name is used by default. If
the field is a custom field type, this must be defined.

setLengthSetting

{@inheritdoc}

protected setLengthSetting(\Drupal\field\FieldStorageConfigInterface $storageDefinition, string $propertyName, int $length): void

Parameters:

Parameter Type Description
$storageDefinition \Drupal\field\FieldStorageConfigInterface
$propertyName string
$length int

changeToText

Change a string field into a text field.

public changeToText(string $entityTypeId, string $fieldName, string $defaultFormat): void

This will only work with the following conditions: - The field is not base field. - The field is not deleted. - The field does not have is_ascii set to TRUE. - The field does not have case_sensitive set to TRUE. - The entity type must be using SQL storage.

Note, this does not wrap the SQL change in a transaction. This is because \Drupal\Core\Database\Schema::executeDdlStatement() does not support being in an active transaction.

Parameters:

Parameter Type Description
$entityTypeId string
The entity type ID.
$fieldName string
The field name.
$defaultFormat string
The default text format to use.

getFormatColumnSchema

Gets the schema core uses for a text field's format column.

protected static getFormatColumnSchema(string $fieldType): array
  • This method is static. Parameters:
Parameter Type Description
$fieldType string
The field type: text, text_long or text_with_summary.

Return Value:

The column schema, as core stores it for a dedicated field table.


changeFromText

Change a text field into a string field.

public changeFromText(string $entityTypeId, string $fieldName): void

This will only work with the following conditions: - The field is not base field. - The field is not deleted. - The entity type must be using SQL storage.

Note, this does not wrap the SQL change in a transaction. This is because \Drupal\Core\Database\Schema::executeDdlStatement() does not support being in an active transaction.

Parameters:

Parameter Type Description
$entityTypeId string
The entity type ID.
$fieldName string
The field name.

changeToLong

Change a string or text field into a string_long or text_long field.

public changeToLong(string $entityTypeId, string $fieldName): void

This will only work with the following conditions: - The field is not base field. - The field is not deleted. - The entity type must be using SQL storage.

This does not support Custom Field subfields: use CustomField::changeStringToLong() instead.

Note, this does not wrap the SQL change in a transaction. This is because \Drupal\Core\Database\Schema::executeDdlStatement() does not support being in an active transaction.

Parameters:

Parameter Type Description
$entityTypeId string
The entity type ID.
$fieldName string
The field name.

changeFieldType

Change a field type.

protected changeFieldType(\Drupal\field\FieldStorageConfigInterface $fieldStorageDefinition, string $fieldType, callable|null $alterFieldConfig = null): void

Note: this does not change the tables or the stored schema, which callers must do first. This just handles updating the following configuration: - Field storage. - Configurable fields. - Entity form displays and entity view displays: widgets and formatters that don't support the new field type are replaced by its defaults.

Parameters:

Parameter Type Description
$fieldStorageDefinition \Drupal\field\FieldStorageConfigInterface
The field storage definition.
$fieldType string
The new field type.
$alterFieldConfig callable|null
(optional) Callback to alter field configuration before saving.

Throws:

  • PluginNotFoundException
  • EntityStorageException