Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 18
n/a
0 / 0
n/a
0 / 0
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ContextFieldItemList
0.00% covered (danger)
0.00%
0 / 18
n/a
0 / 0
n/a
0 / 0
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 setValue
0.00% covered (danger)
0.00%
0 / 12
n/a
0 / 0
n/a
0 / 0
0.00% covered (danger)
0.00%
0 / 1
30
 getValue
0.00% covered (danger)
0.00%
0 / 6
n/a
0 / 0
n/a
0 / 0
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\display_builder;
6
7use Drupal\Core\Field\FieldItemList;
8use Drupal\Core\Plugin\Context\Context;
9use Drupal\Core\Plugin\Context\ContextDefinition;
10use Drupal\Core\Plugin\Context\ContextInterface;
11
12/**
13 * Represents an entity field; that is, a list of field item objects.
14 */
15class ContextFieldItemList extends FieldItemList {
16
17  /**
18   * {@inheritdoc}
19   */
20  public function setValue(mixed $values, mixed $notify = TRUE): void {
21    if (\is_array($values) && !\array_is_list($values)) {
22      $contexts = $values;
23      $values = [];
24
25      foreach ($contexts as $context_id => $context) {
26        if (!($context instanceof ContextInterface)) {
27          continue;
28        }
29
30        $values[] = [
31          'id' => $context_id,
32          'type' => $context->getContextDefinition()->getDataType(),
33          'value' => $context->getContextData(),
34        ];
35      }
36    }
37
38    parent::setValue($values, $notify);
39  }
40
41  /**
42   * {@inheritdoc}
43   */
44  public function getValue() {
45    $values = [];
46
47    foreach ($this->list as $item) {
48      $value = $item->getValue();
49      $definition = ContextDefinition::create($value['type']);
50      $values[$value['id']] = new Context($definition, $value['value']);
51    }
52
53    return $values;
54  }
55
56}