Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / lib / Drupal / Core / Field / FieldInputValueNormalizerTrait.php
1 <?php
2
3 namespace Drupal\Core\Field;
4
5 /**
6  * A trait used to assist in the normalization of raw input field values.
7  *
8  * @internal
9  *
10  * @see \Drupal\Core\Field\FieldConfigBase
11  * @see \Drupal\Core\Field\BaseFieldDefinition
12  */
13 trait FieldInputValueNormalizerTrait {
14
15   /**
16    * Ensure a field value is transformed into a format keyed by delta.
17    *
18    * @param mixed $value
19    *   The raw field value to normalize.
20    * @param string $main_property_name
21    *   The main field property name.
22    *
23    * @return array
24    *   A field value normalized into a format keyed by delta.
25    */
26   protected static function normalizeValue(&$value, $main_property_name) {
27     if (!isset($value) || $value === NULL) {
28       return [];
29     }
30     if (!is_array($value)) {
31       if ($main_property_name === NULL) {
32         throw new \InvalidArgumentException('A main property is required when normalizing scalar field values.');
33       }
34       return [[$main_property_name => $value]];
35     }
36     if (!empty($value) && !is_numeric(array_keys($value)[0])) {
37       return [0 => $value];
38     }
39     return $value;
40   }
41
42 }