-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathParagraphsTrait.php
195 lines (165 loc) · 6.38 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?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;
use Drupal\paragraphs\ParagraphInterface;
/**
* Trait ParagraphsTrait.
*
* Paragraphs-related steps.
*
* @package DrevOps\BehatSteps
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
trait ParagraphsTrait {
/**
* Array of created paragraph entities.
*
* @var array<int,\Drupal\paragraphs\ParagraphInterface>
*/
protected static $paragraphEntities = [];
/**
* 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::$paragraphEntities as $paragraph) {
$paragraph->delete();
}
static::$paragraphEntities = [];
}
/**
* 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.
$parent_entity = $this->paragraphsFindEntity($entity_type, $bundle, $entity_field_name, $entity_field_identifer);
if (!$parent_entity) {
throw new \RuntimeException(sprintf('Parent entity "%s" with field "%s" of value "%s" not found', $bundle, $entity_field_name, $entity_field_identifer));
}
// 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);
$this->paragraphsAttachFromStubToEntity($parent_entity, $field_name, $paragraph_type, $stub);
}
/**
* Create a paragraphs item from a stub and attach it to an entity.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $parent_entity
* Node to attach paragraph to.
* @param string $parent_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\ParagraphInterface
* Created paragraphs item.
*/
protected function paragraphsAttachFromStubToEntity(ContentEntityInterface $parent_entity, string $parent_entity_field_name, string $paragraph_bundle, \StdClass $stub, bool $save_entity = TRUE): ParagraphInterface {
$stub->type = $paragraph_bundle;
$stub = (array) $stub;
$paragraph = Paragraph::create($stub);
$paragraph->setParentEntity($parent_entity, $parent_entity_field_name)->save();
$new_value = $parent_entity->get($parent_entity_field_name)->getValue();
$new_value[] = [
'target_id' => $paragraph->id(),
'target_revision_id' => $paragraph->getRevisionId(),
];
$parent_entity->set($parent_entity_field_name, $new_value);
if ($save_entity) {
$parent_entity->save();
}
static::$paragraphEntities[] = $paragraph;
return $paragraph;
}
/**
* Find entity.
*
* @param string $entity_type
* Entity type.
* @param string $bundle
* Bundle name.
* @param string $field_name
* Field name.
* @param string $field_value
* Field value.
*
* @return \Drupal\Core\Entity\ContentEntityInterface|null
* Found entity or NULL if not found.
*/
protected function paragraphsFindEntity(string $entity_type, string $bundle, string $field_name, string $field_value): ?ContentEntityInterface {
$query = \Drupal::entityQuery($entity_type)
->accessCheck(FALSE)
->condition($entity_type === 'taxonomy_term' ? 'vid' : 'type', $bundle)
->condition($field_name, $field_value);
$entity_ids = $query->execute();
if (empty($entity_ids)) {
return NULL;
}
$entity_id = array_pop($entity_ids);
$entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($entity_id);
return $entity instanceof ContentEntityInterface ? $entity : NULL;
}
/**
* Expand parsed fields into expected field values based on field type.
*
* @param string $entity_type
* Entity type.
* @param \StdClass $stub
* Stub object.
*/
protected function paragraphsExpandEntityFields(string $entity_type, \StdClass $stub): void {
$core = $this->getDriver()->getCore();
$class = new \ReflectionClass($core::class);
$method = $class->getMethod('expandEntityFields');
$method->setAccessible(TRUE);
$method->invokeArgs($core, func_get_args());
}
/**
* Validate that an entity has a field.
*
* @param string $entity_type
* Entity type.
* @param string $bundle
* Bundle name.
* @param string $field_name
* Field name.
*
* @throws \RuntimeException
* If the field does not exist on the entity.
*/
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));
}
}
}