-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
ParagraphsTrait.php
179 lines (151 loc) · 5.94 KB
/
ParagraphsTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
declare(strict_types=1);
namespace DrevOps\BehatSteps;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Gherkin\Node\TableNode;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\paragraphs\Entity\Paragraph;
/**
* Trait ParagraphsTrait.
*
* Paragraphs-related steps.
*
* @package DrevOps\BehatSteps
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
trait ParagraphsTrait {
/**
* Array of created paragraph entities.
*
* @var array
*/
protected static $paragraphs = [];
/**
* Clean all paragraphs instances after scenario run.
*
* @AfterScenario
*/
public function paragraphsCleanAll(AfterScenarioScope $scope): void {
// Allow to skip this by adding a tag.
if ($scope->getScenario()->hasTag('behat-steps-skip:' . __FUNCTION__)) {
return;
}
foreach (static::$paragraphs as $paragraph) {
try {
$paragraph->delete();
}
catch (\Exception) {
// Ignore the exception and move on.
continue;
}
}
static::$paragraphs = [];
}
/**
* Creates paragraphs of the given type with fields for existing entity.
*
* Paragraph fields are specified in the same way as for nodeCreate():
* | field_paragraph_title | My paragraph title |
* | field_paragraph_longtext:value | My paragraph message |
* | field_paragraph_longtext:format | full_html |
* | ... | ... |
*
* @When :field_name in :bundle :entity_type with :entity_field_name of :entity_field_identifer has :paragraph_type paragraph:
*/
public function paragraphsAddToEntityWithFields(string $field_name, string $bundle, string $entity_type, string $entity_field_name, string $entity_field_identifer, string $paragraph_type, TableNode $fields): void {
$this->paragraphsValidateEntityFieldName($entity_type, $bundle, $field_name);
// Find previously created entity by entity_type, bundle and identifying
// field value.
$entity = $this->paragraphsFindEntity([
'field_value' => $entity_field_identifer,
'field_name' => $entity_field_name,
'bundle' => $bundle,
'entity_type' => $entity_type,
]);
// Get fields from scenario, parse them and expand values according to
// field tables.
$stub = (object) $fields->getRowsHash();
$stub->type = $paragraph_type;
$this->parseEntityFields('paragraph', $stub);
$this->paragraphsExpandEntityFields('paragraph', $stub);
// Attach paragraph from stub to node.
$this->paragraphsAttachFromStubToEntity($entity, $field_name, $paragraph_type, $stub);
}
/**
* Create a paragraphs item from a stub and attach it to an entity.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* Node to attach paragraph to.
* @param string $entity_field_name
* Field name on the entity that refers paragraphs item.
* @param string $paragraph_bundle
* Paragraphs item bundle name.
* @param \StdClass $stub
* Standard object with filled-in fields. Fields are merged with created
* paragraphs item object.
* @param bool $save_entity
* Flag to save node after attaching a paragraphs item. Defaults to TRUE.
*
* @return \Drupal\paragraphs\Entity\Paragraph
* Created paragraphs item.
*/
protected function paragraphsAttachFromStubToEntity(ContentEntityInterface $entity, string $entity_field_name, string $paragraph_bundle, \StdClass $stub, bool $save_entity = TRUE): Paragraph {
$stub->type = $paragraph_bundle;
$stub = (array) $stub;
$paragraph = Paragraph::create($stub);
$paragraph->setParentEntity($entity, $entity_field_name)->save();
$existing_value = $entity->get($entity_field_name);
$new_value = $existing_value->getValue();
$new_value[] = [
'target_id' => $paragraph->id(),
'target_revision_id' => $paragraph->getRevisionId(),
];
$entity->set($entity_field_name, $new_value);
if ($save_entity) {
$entity->save();
}
static::$paragraphs[] = $paragraph;
return $paragraph;
}
/**
* Find entity using provided conditions.
*/
protected function paragraphsFindEntity(array $conditions = []): ContentEntityInterface|null {
$type = ($conditions['entity_type'] === 'taxonomy_term') ? 'vid' : 'type';
$query = \Drupal::entityQuery($conditions['entity_type'])
->accessCheck(FALSE)
->condition($type, $conditions['bundle'])
->condition($conditions['field_name'], $conditions['field_value']);
$entity_ids = $query->execute();
if (empty($entity_ids)) {
throw new \Exception(sprintf('Unable to find entity that matches conditions: "%s"', print_r($conditions, TRUE)));
}
$entity_id = array_pop($entity_ids);
$entity = \Drupal::entityTypeManager()->getStorage($conditions['entity_type'])->load($entity_id);
if (!$entity instanceof ContentEntityInterface) {
throw new \Exception(sprintf('Unable to load entity "%s" with id "%s"', $conditions['entity_type'], $entity_id));
}
return $entity;
}
/**
* Expand parsed fields into expected field values based on field type.
*/
protected function paragraphsExpandEntityFields(string $entity_type, \StdClass $stub) {
$core = $this->getDriver()->getCore();
$class = new \ReflectionClass($core::class);
$method = $class->getMethod('expandEntityFields');
$method->setAccessible(TRUE);
return $method->invokeArgs($core, func_get_args());
}
/**
* Get a field name that references the paragraphs item.
*/
protected function paragraphsValidateEntityFieldName(string $entity_type, string $bundle, string $field_name): void {
/** @var \Drupal\Core\Field\FieldDefinitionInterface[] $field_info */
$field_info = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type, $bundle);
if (!array_key_exists($field_name, $field_info)) {
throw new \RuntimeException(sprintf('"%s" "%s" does not have a field "%s"', $bundle, $entity_type, $field_name));
}
}
}