Skip to content

Commit

Permalink
Add support for references to paragraphs in paragraph entities
Browse files Browse the repository at this point in the history
  • Loading branch information
miiimooo committed Mar 29, 2021
1 parent 059c1a7 commit 725c78e
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/Context/ParagraphsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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'])
Expand All @@ -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);
}
}
}

0 comments on commit 725c78e

Please sign in to comment.