ItemGenerator   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Test Coverage

Coverage 97.14%

Importance

Changes 0
Metric Value
dl 0
loc 165
ccs 68
cts 70
cp 0.9714
rs 10
c 0
b 0
f 0
wmc 20

11 Methods

Rating   Name   Duplication   Size   Complexity  
A id() 0 2 1
A topPath() 0 3 2
B savePath() 0 29 5
A postprocessParagraph() 0 2 1
A complete() 0 15 2
A pushPath() 0 4 1
A processParagraph() 0 8 2
A processField() 0 15 3
A postprocessField() 0 2 1
A popPath() 0 5 1
A initialize() 0 3 1
1
<?php
2
3
namespace Drupal\paragraphs_editor\WidgetBinder\Generators;
4
5
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
6
use Drupal\Core\Render\RenderContext;
7
use Drupal\paragraphs\ParagraphInterface;
8
use Drupal\paragraphs_editor\Utility\TypeUtility;
9
use Drupal\paragraphs_editor\WidgetBinder\GeneratorBase;
10
use Drupal\paragraphs_editor\WidgetBinder\WidgetBinderData;
11
use Drupal\paragraphs_editor\WidgetBinder\WidgetBinderDataCompilerState;
12
13
/**
14
 * Generates edit buffer item models for the widget binder library.
15
 *
16
 * The edit buffer item model contains the markup and information about editable
17
 * child fields.
18
 */
19
class ItemGenerator extends GeneratorBase {
20
21
  /**
22
   * {@inheritdoc}
23
   */
24 1
  public function id() {
25 1
    return 'item';
26
  }
27
28
  /**
29
   * {@inheritdoc}
30
   */
31 4
  public function initialize(WidgetBinderData $data, WidgetBinderDataCompilerState $state, ParagraphInterface $root_paragraph) {
32 4
    $state->set('field_map', []);
33 4
    $state->set('field_map_stack', []);
34 4
  }
35
36
  /**
37
   * {@inheritdoc}
38
   */
39 4
  public function processParagraph(WidgetBinderData $data, WidgetBinderDataCompilerState $state, ParagraphInterface $paragraph) {
40 4
    $top = $this->topPath($state);
41 4
    if (!empty($top)) {
42 4
      $this->pushPath($state, [
43 4
        'type' => 'widget',
44 4
        'uuid' => $paragraph->uuid(),
45
      ]);
46 4
      $this->savePath($state);
47
    }
48 4
  }
49
50
  /**
51
   * {@inheritdoc}
52
   */
53 4
  public function postprocessParagraph(WidgetBinderData $data, WidgetBinderDataCompilerState $state, ParagraphInterface $paragraph) {
54 4
    $this->popPath($state);
55 4
  }
56
57
  /**
58
   * {@inheritdoc}
59
   */
60 4
  public function processField(WidgetBinderData $data, WidgetBinderDataCompilerState $state, EntityReferenceFieldItemListInterface $items, $is_editor_field) {
61 4
    $field_definition = TypeUtility::ensureFieldConfig($items->getFieldDefinition());
62
    $path = [
63 4
      'type' => 'field',
64 4
      'name' => $field_definition->getName(),
65
    ];
66
67 4
    $context_id = $data->getContextId($items->getEntity()->uuid(), $field_definition->id());
68 4
    if ($context_id) {
69
      $path['context'] = $context_id;
70
    }
71
72 4
    $this->pushPath($state, $path);
73 4
    if ($is_editor_field) {
74 3
      $this->savePath($state);
75
    }
76 4
  }
77
78
  /**
79
   * {@inheritdoc}
80
   */
81 3
  public function postprocessField(WidgetBinderData $data, WidgetBinderDataCompilerState $state, EntityReferenceFieldItemListInterface $items, $is_editor_field) {
82 3
    $this->popPath($state);
83 3
  }
84
85
  /**
86
   * {@inheritdoc}
87
   */
88 4
  public function complete(WidgetBinderData $data, WidgetBinderDataCompilerState $state, RenderContext $render_context, $markup) {
89 4
    $context = $state->getItemContext();
90 4
    $paragraph = $state->getItem()->getEntity();
91
92
    $item_model = [
93 4
      'contextId' => $context->getContextString(),
94 4
      'markup' => $markup,
95 4
      'type' => $paragraph->bundle(),
96 4
      'fields' => $state->get('field_map'),
97
    ];
98 4
    if ($context->getAdditionalContext('command') == 'insert') {
99
      $item_model['insert'] = 'true';
100
    }
101
102 4
    $data->addModel('editBufferItem', $paragraph->uuid(), $item_model);
103 4
  }
104
105
  /**
106
   * Pushes a path node to the top of the map stack.
107
   *
108
   * @param \Drupal\paragraphs_editor\WidgetBinder\WidgetBinderDataCompilerState $state
109
   *   The current compiler state.
110
   * @param array $data
111
   *   The path node to push.
112
   */
113 4
  protected function pushPath(WidgetBinderDataCompilerState $state, array $data) {
114 4
    $stack = $state->get('field_map_stack');
115 4
    $stack[] = $data;
116 4
    $state->set('field_map_stack', $stack);
117 4
  }
118
119
  /**
120
   * Gets the path node at the top of the stack.
121
   *
122
   * @param \Drupal\paragraphs_editor\WidgetBinder\WidgetBinderDataCompilerState $state
123
   *   The current compiler state.
124
   *
125
   * @return array
126
   *   The top path node or NULL if no node exists.
127
   */
128 4
  protected function topPath(WidgetBinderDataCompilerState $state) {
129 4
    $stack = $state->get('field_map_stack');
130 4
    return $stack ? end($stack) : NULL;
131
  }
132
133
  /**
134
   * Pops a path node off the top of the map stack.
135
   *
136
   * @param \Drupal\paragraphs_editor\WidgetBinder\WidgetBinderDataCompilerState $state
137
   *   The current compiler state.
138
   *
139
   * @return array
140
   *   The popped path node or NULL if no node exists.
141
   */
142 4
  protected function popPath(WidgetBinderDataCompilerState $state) {
143 4
    $stack = $state->get('field_map_stack');
144 4
    $data = array_pop($stack);
145 4
    $state->set('field_map_stack', $stack);
146 4
    return $data;
147
  }
148
149
  /**
150
   * Saves a path to the compiled field map in the compiler state.
151
   *
152
   * @param \Drupal\paragraphs_editor\WidgetBinder\WidgetBinderDataCompilerState $state
153
   *   The current compiler state.
154
   */
155 4
  protected function savePath(WidgetBinderDataCompilerState $state) {
156 4
    $node_path = $state->get('field_map_stack');
157 4
    $field_map = $state->get('field_map');
158
159 4
    $map = &$field_map;
160 4
    foreach ($node_path as $node) {
161
162 4
      if ($node['type'] == 'field') {
163 4
        $node_id = $node['name'];
164
      }
165
      else {
166 4
        $node_id = $node['uuid'];
167
      }
168
169 4
      if (!isset($map[$node_id])) {
170 4
        $map[$node_id] = $node;
171
      }
172
      else {
173 3
        $map[$node_id] += $node;
174
      }
175
176 4
      if (!isset($map[$node_id]['children'])) {
177 4
        $map[$node_id]['children'] = [];
178
      }
179
180 4
      $map = &$map[$node_id]['children'];
181
    }
182
183 4
    $state->set('field_map', $field_map);
184 4
  }
185
186
}
187