trait FieldInputValueNormalizerTrait

Same name in other branches
  1. 9 core/lib/Drupal/Core/Field/FieldInputValueNormalizerTrait.php \Drupal\Core\Field\FieldInputValueNormalizerTrait
  2. 8.9.x core/lib/Drupal/Core/Field/FieldInputValueNormalizerTrait.php \Drupal\Core\Field\FieldInputValueNormalizerTrait
  3. 11.x core/lib/Drupal/Core/Field/FieldInputValueNormalizerTrait.php \Drupal\Core\Field\FieldInputValueNormalizerTrait

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

@internal

Hierarchy

See also

\Drupal\Core\Field\FieldConfigBase

\Drupal\Core\Field\BaseFieldDefinition

1 file declares its use of FieldInputValueNormalizerTrait
FieldInputValueNormalizerTraitTest.php in core/tests/Drupal/Tests/Core/Field/FieldInputValueNormalizerTraitTest.php

File

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

Namespace

Drupal\Core\Field
View source
trait FieldInputValueNormalizerTrait {
    
    /**
     * Ensure a field value is transformed into a format keyed by delta.
     *
     * @param mixed $value
     *   The raw field value to normalize.
     * @param string $main_property_name
     *   The main field property name.
     *
     * @return array
     *   A field value normalized into a format keyed by delta.
     */
    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;
    }

}

Members

Title Sort descending Modifiers Object type Summary
FieldInputValueNormalizerTrait::normalizeValue protected static function Ensure a field value is transformed into a format keyed by delta.

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