Skip to content

Drupal\helper\Field\StringField

A helper for working with string fields.

Implements:

Drupal\Core\DependencyInjection\ContainerInjectionInterface

Methods

Name Description
__construct StringField constructor.
changeFromText Change a text field into a string field.
changeLength Change a string field's length.
changeToText Change a string field into a text field.
create {@inheritdoc}
setLogger Sets a logger.

StringField::__construct

Description

public __construct (\Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager, \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface $entityDefinitionUpdateManager, \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager, \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entityDisplayRepository, \Drupal\Core\Field\FieldTypePluginManagerInterface $fieldTypePluginManager, \Drupal\Core\Config\ConfigFactoryInterface $configFactory, \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $keyValueFactory, \Drupal\Core\Database\Connection $database, \Psr\Log\LoggerInterface $logger)

StringField constructor.

Parameters

  • (\Drupal\Core\Entity\EntityTypeManagerInterface) $entityTypeManager :
    The entity type manager.
  • (\Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface) $entityDefinitionUpdateManager :
    The entity definition update manager.
  • (\Drupal\Core\Entity\EntityFieldManagerInterface) $entityFieldManager :
    The entity field manager.
  • (\Drupal\Core\Entity\EntityDisplayRepositoryInterface) $entityDisplayRepository :
    The entity display repository.
  • (\Drupal\Core\Field\FieldTypePluginManagerInterface) $fieldTypePluginManager :
    The field type plugin manager.
  • (\Drupal\Core\Config\ConfigFactoryInterface) $configFactory :
    The config factory.
  • (\Drupal\Core\KeyValueStore\KeyValueFactoryInterface) $keyValueFactory :
    The key-value factory.
  • (\Drupal\Core\Database\Connection) $database :
    The database.
  • (\Psr\Log\LoggerInterface) $logger :
    The logger.

Return Values

void


StringField::changeFromText

Description

public changeFromText (string $entityTypeId, string $fieldName)

Change a text field into a string field.

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

  • (string) $entityTypeId :
    The entity type ID.
  • (string) $fieldName :
    The field name.

Return Values

void


StringField::changeLength

Description

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

Change a string field's length.

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 or a custom field with a length
setting.
- 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

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

Return Values

void


StringField::changeToText

Description

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

Change a string field into a text field.

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

  • (string) $entityTypeId :
    The entity type ID.
  • (string) $fieldName :
    The field name.
  • (string) $defaultFormat :
    The default text format to use.

Return Values

void


StringField::create

Description

public static create (void)

{@inheritdoc}

Parameters

This function has no parameters.

Return Values

void


StringField::setLogger

Description

public setLogger (void)

Sets a logger.

Parameters

This function has no parameters.

Return Values

void