Skip to content

Commit

Permalink
Add support for multiple paragraph references in a field, separated b…
Browse files Browse the repository at this point in the history
…y commas
  • Loading branch information
miiimooo committed Mar 29, 2021
1 parent 58fbef1 commit 059c1a7
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/Context/ParagraphsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,27 @@ public function beforeNodeCreateHook(BeforeNodeCreateScope $scope) {
|| $fields[$field_name]->getSettings()['target_type'] !== 'paragraph') {
continue;
}
$target_id = isset($this->paragraphNames[$value]) ? $this->paragraphNames[$value] : NULL;
if (!$target_id) {
sprintf('Referenced paragraph name "%s" not found.', $value);
return;
$target_ids = [];
$revision_ids = [];
$values = (strpos($value, ',') !== FALSE)
? array_map('trim', explode(',', $value))
: [$value];
foreach ($values as $value) {
$target_id = isset($this->paragraphNames[$value]) ? $this->paragraphNames[$value] : NULL;
if (!$target_id) {
throw new \Exception(sprintf('Referenced paragraph name "%s" not found.', $value));
}
$paragraph = \Drupal\paragraphs\Entity\Paragraph::load($target_id);
$target_ids[] = $target_id;
$revision_ids[] = $paragraph->getRevisionId();
}
if (empty($target_ids) || empty($revision_ids)) {
continue;
}
$paragraph = \Drupal\paragraphs\Entity\Paragraph::load($target_id);
$column_name = "$field_name:target_id";
$node->$column_name = $target_id;
$node->$column_name = implode(',', $target_ids);
$column_name = "$field_name:target_revision_id";
$node->$column_name = $paragraph->getRevisionId();
$node->$column_name = implode(',', $revision_ids);
}
}
}

0 comments on commit 059c1a7

Please sign in to comment.