From 725c78ee43b7f4cd56351f1272891eb206e2fbb9 Mon Sep 17 00:00:00 2001 From: Michael Moritz Date: Mon, 29 Mar 2021 16:03:53 +0200 Subject: [PATCH] Add support for references to paragraphs in paragraph entities --- src/Context/ParagraphsContext.php | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Context/ParagraphsContext.php b/src/Context/ParagraphsContext.php index 658d030..abdeeed 100644 --- a/src/Context/ParagraphsContext.php +++ b/src/Context/ParagraphsContext.php @@ -77,6 +77,7 @@ public function createParagraph($type, $name, TableNode $fields) { foreach ($fields->getRowsHash() as $field => $value) { $entity->{$field} = $value; } + $this->preprocessEntityReferenceFieldsForParagraphs('paragraph', $entity); $saved = $this->create($entity); $this->paragraphNames[$name] = $saved->id; } @@ -85,11 +86,22 @@ public function createParagraph($type, $name, TableNode $fields) { */ public function beforeNodeCreateHook(BeforeNodeCreateScope $scope) { // This is missing in the Drupal driver + $this->preprocessEntityReferenceFieldsForParagraphs('node', $scope->getEntity()); + } + + /** + * Detect entity references to paragraphs and resovle them before creating an entity + * + * @param string $entity_type_id + * @param \StdClass $entity + * + * @throws \Exception + */ + protected function preprocessEntityReferenceFieldsForParagraphs(string $entity_type_id, \StdClass $entity) { /** @var \Drupal\field\Entity\FieldStorageConfig[] $fields */ $fields = \Drupal::service('entity_field.manager') - ->getFieldStorageDefinitions('node'); - $node = $scope->getEntity(); - foreach ((array)$node as $field_name => $value) { + ->getFieldStorageDefinitions($entity_type_id); + foreach ((array)$entity as $field_name => $value) { if (is_array($value) || !isset($fields[$field_name]) || !$fields[$field_name]->getSettings() || !isset($fields[$field_name]->getSettings()['target_type']) @@ -114,9 +126,9 @@ public function beforeNodeCreateHook(BeforeNodeCreateScope $scope) { continue; } $column_name = "$field_name:target_id"; - $node->$column_name = implode(',', $target_ids); + $entity->$column_name = implode(',', $target_ids); $column_name = "$field_name:target_revision_id"; - $node->$column_name = implode(',', $revision_ids); + $entity->$column_name = implode(',', $revision_ids); } } }