Created
June 9, 2020 15:12
-
-
Save lpeabody/c83877a5e6f0f1c532545f8e8be41065 to your computer and use it in GitHub Desktop.
Move paragraphs from multiple limited-1 fields to a single components field with unlimited cardinality.
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
<?php | |
/** @var \Drupal\node\Entity\Node[] $landing_pages */ | |
$landing_pages = \Drupal::entityTypeManager() | |
->getStorage('node') | |
->loadByProperties([ | |
'type' => 'landing_page' | |
]); | |
$old_paragraph_fields = [ | |
'field_tile_section', | |
'field_leadership', | |
'field_media_slider', | |
'field_testimonial', | |
]; | |
/** @var \Drupal\paragraphs\Entity\Paragraph[] $paragraphs_to_move */ | |
foreach ($landing_pages as $landing_page) { | |
$paragraphs_to_move = []; | |
foreach ($old_paragraph_fields as $field) { | |
if (!$landing_page->hasField($field) || $landing_page->{$field}->isEmpty()) { | |
continue; | |
} | |
/** @var \Drupal\paragraphs\Entity\Paragraph[] $paragraphs */ | |
$paragraphs = $landing_page->{$field}->referencedEntities(); | |
foreach ($paragraphs as $paragraph) { | |
$paragraphs_to_move[] = $paragraph; | |
} | |
$landing_page->{$field} = NULL; | |
} | |
foreach ($paragraphs_to_move as $paragraph) { | |
$paragraph | |
->setParentEntity($landing_page, 'field_landing_components') | |
->save(); | |
$landing_page->field_landing_components[] = $paragraph; | |
} | |
$landing_page->save(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment