function FieldInputValueNormalizerTrait::normalizeValue

Same name in other branches
  1. 9 core/lib/Drupal/Core/Field/FieldInputValueNormalizerTrait.php \Drupal\Core\Field\FieldInputValueNormalizerTrait::normalizeValue()
  2. 10 core/lib/Drupal/Core/Field/FieldInputValueNormalizerTrait.php \Drupal\Core\Field\FieldInputValueNormalizerTrait::normalizeValue()
  3. 11.x 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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.