You are here

protected static function FieldInputValueNormalizerTrait::normalizeValue in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Field/FieldInputValueNormalizerTrait.php \Drupal\Core\Field\FieldInputValueNormalizerTrait::normalizeValue()

Ensure a field value is transformed into a format keyed by delta.

Parameters

mixed $value: The raw field value to normalize.

string $main_property_name: The main field property name.

Return value

array A field value normalized into a format keyed by delta.

10 calls to FieldInputValueNormalizerTrait::normalizeValue()
BaseFieldDefinition::getDefaultValue in core/lib/Drupal/Core/Field/BaseFieldDefinition.php
Returns the default value for the field in a newly created entity.
BaseFieldDefinition::getInitialValue in core/lib/Drupal/Core/Field/BaseFieldDefinition.php
Returns the initial value for the field.
BaseFieldDefinition::setInitialValue in core/lib/Drupal/Core/Field/BaseFieldDefinition.php
Sets an initial value for the field.
FieldConfigBase::getDefaultValue in core/lib/Drupal/Core/Field/FieldConfigBase.php
Returns the default value for the field in a newly created entity.
FieldConfigBase::setDefaultValue in core/lib/Drupal/Core/Field/FieldConfigBase.php
Sets a default value.

... See full list

File

core/lib/Drupal/Core/Field/FieldInputValueNormalizerTrait.php, line 26

Class

FieldInputValueNormalizerTrait
A trait used to assist in the normalization of raw input field values.

Namespace

Drupal\Core\Field

Code

protected static function normalizeValue(&$value, $main_property_name) {
  if (!isset($value) || $value === NULL) {
    return [];
  }
  if (!is_array($value)) {
    if ($main_property_name === NULL) {
      throw new \InvalidArgumentException('A main property is required when normalizing scalar field values.');
    }
    return [
      [
        $main_property_name => $value,
      ],
    ];
  }
  if (!empty($value) && !is_numeric(array_keys($value)[0])) {
    return [
      0 => $value,
    ];
  }
  return $value;
}