Last active
October 22, 2021 05:36
-
-
Save DanLaufer/9742dc177d31b4ccc44c66cb6573ccf9 to your computer and use it in GitHub Desktop.
Drupal 8 - Add paragraph reference to nodes without one
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Drupal\paragraphs\Entity\Paragraph; | |
$query = \Drupal::entityQuery('node'); | |
// get all blogs with the flexible content field | |
$node_ids = $query | |
->condition('status', 1) | |
->condition('type','insight') | |
->exists('field_flexible_content') | |
->execute(); | |
// get a list of nodes with the in-content newsletter form | |
// already added to flexible content | |
$node_ids_with_in_content_form_newsletter = []; | |
foreach($node_ids as $node_id) { | |
$node = \Drupal\node\Entity\Node::load($node_id); | |
$flexible_paragraphs = $node->get('field_flexible_content')->referencedEntities(); | |
foreach($flexible_paragraphs as $parag) { | |
if($parag->getParagraphType()->id == 'in_content_form') { | |
$form_paragraphs = $parag->get('field_form_picker')->referencedEntities(); | |
foreach($form_paragraphs as $parag2) { | |
if($parag2->getParagraphType()->id == 'in_content_form_newsletter') { | |
array_push($node_ids_with_in_content_form_newsletter, $node_id); | |
break; | |
} | |
} | |
} | |
} | |
} | |
// remove nodes with in-content newsletter form from list of blog nodes | |
foreach($node_ids_with_in_content_form_newsletter as $node_id) { | |
foreach (array_keys($node_ids) as $key) { | |
if ($node_ids[$key] === $node_id) { | |
unset($node_ids[$key]); | |
} | |
} | |
} | |
// print_r("Content with newsletter form already:"); | |
// print_r($node_ids_with_in_content_form_newsletter); | |
// | |
// print_r("Content still needing it:"); | |
// print_r($node_ids); | |
// the remaining list ($node_ids) are the blog nodes without the | |
// in-content newsletter form. Add a new in-content newsletter form | |
// to each of them. | |
foreach($node_ids as $node_id) { | |
$node = \Drupal\node\Entity\Node::load($node_id); | |
// create and save in-content newsletter form paragraph | |
$subparagraph = Paragraph::create(['type' => 'in_content_form_newsletter']); | |
$subparagraph->isNew(); | |
$subparagraph->save(); | |
// create in-content form paragraph | |
$paragraph = Paragraph::create(['type' => 'in_content_form']); | |
$paragraph->isNew(); | |
// add new in-content newsletter form paragraph to new in-content | |
// form paragraph's form-picker field and save the in-content | |
// form paragraph. | |
$currentparags = $paragraph->get('field_form_picker')->getValue(); | |
$currentparags[] = array( | |
'target_id' => $subparagraph->id(), | |
'target_revision_id' => $subparagraph->getRevisionId(), | |
); | |
$paragraph->set('field_form_picker', $currentparags); | |
$paragraph->save(); | |
// get the current flexible content paragraphs and add the new | |
// in-content form paragraph to the node's flexible content. | |
$current = $node->get('field_flexible_content')->getValue(); | |
$current[] = array( | |
'target_id' => $paragraph->id(), | |
'target_revision_id' => $paragraph->getRevisionId(), | |
); | |
$node->set('field_flexible_content', $current); | |
// save the node | |
$node->save(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment