Skip to content

CustomField

A helper for changing Custom Field subfields that already have data.


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

Properties

customFieldTypeManager

Custom Field's subfield type plugin manager, if the module is installed.

protected ?\Drupal\Component\Plugin\PluginManagerInterface $customFieldTypeManager

customFieldWidgetManager

Custom Field's widget plugin manager, if the module is installed.

protected ?\Drupal\Component\Plugin\PluginManagerInterface $customFieldWidgetManager

customFieldFormatterManager

Custom Field's formatter plugin manager, if the module is installed.

protected ?\Drupal\Component\Plugin\PluginManagerInterface $customFieldFormatterManager

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

Changes the length of a Custom Field string subfield.

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

Once a field has data, the length cannot normally be changed. This changes the column, keeping the data. Shortening is allowed, but throws an exception if existing data is longer than the new length.

This will only work with the following conditions: - The field is a Custom Field field, not a base field, and not deleted. - The subfield is a string subfield, 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:

Parameter Type Description
$entityTypeId string
The entity type ID.
$fieldName string
The field name.
$length int
The new length.
$propertyName string
The subfield (column) name.

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

changeStringToLong

Change a custom_field subfield from type string to string_long.

public changeStringToLong(string $entityTypeId, string $fieldName, string $propertyName, string|null $defaultFormat = null): 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.
$propertyName string
The subfield/column name.
$defaultFormat string|null
(optional) The default text format to use.

changeType

Changes a Custom Field subfield's type in the configuration.

protected changeType(\Drupal\field\FieldStorageConfigInterface $storageDefinition, string $propertyName, string $fieldType): void

This doesn't change the tables or the stored schema, which callers must do first. It updates: - The field storage: the subfield's type, without a length setting if the new type's schema has no length. - Each bundle's field settings for the subfield: settings only the old type has are removed, and the new type's defaults are added. - Form and view displays that show the subfield: if the widget or formatter doesn't support the new type, it's replaced by the new type's default one. Settings only the old plugin has are removed, and the new plugin's defaults are added.

Parameters:

Parameter Type Description
$storageDefinition \Drupal\field\FieldStorageConfigInterface
The custom field storage definition.
$propertyName string
The subfield (column) name.
$fieldType string
The new subfield type, e.g. string_long.

Throws:

If the Custom Field module isn't installed. - LogicException


replacePlugin

Replaces a subfield's widget or formatter if it doesn't support a type.

protected replacePlugin(array $settings, string $pluginKey, \Drupal\Component\Plugin\PluginManagerInterface $manager, string $fieldType, string $defaultPluginId): array

Parameters:

Parameter Type Description
$settings array
The plugin ID, under $pluginKey, and the plugin's settings.
$pluginKey string
The key of the plugin ID in $settings: 'type' or 'format_type'.
$manager \Drupal\Component\Plugin\PluginManagerInterface
The widget or formatter plugin manager.
$fieldType string
The new subfield type.
$defaultPluginId string
The new subfield type's default widget or formatter.

Return Value:

The settings, unchanged if the plugin supports the new type.


replaceDefaultSettings

Replaces one plugin's default settings with another's.

protected static replaceDefaultSettings(array $settings, array $oldDefaults, array $newDefaults): array

Settings only the old plugin has are removed, settings both have keep their values, and the new plugin's other defaults are added. Settings that neither plugin has, such as a subfield's weight, are kept.

  • This method is static. Parameters:
Parameter Type Description
$settings array
The settings.
$oldDefaults array
The old plugin's default settings.
$newDefaults array
The new plugin's default settings.

Return Value:

The settings for the new plugin.