-
-
Save chicodasilva/65be2837dc59828ad55534be6eea8aa7 to your computer and use it in GitHub Desktop.
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 | |
| /** | |
| * This plugin creates a new paragraph entity based on the source. | |
| * | |
| * @MigrateProcessPlugin( | |
| * id = "mds_paragraph" | |
| * ) | |
| */ | |
| class ParagraphMigrateProcessor extends ParagraphProcessBase { | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { | |
| $paragraph = $this->getParagraph($row, $destination_property, 0); | |
| $paragraph->set('field_text', ['value' => $value, 'format' => $format]); | |
| $paragraph->save(); | |
| return $paragraph; | |
| } | |
| } |
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 | |
| /** | |
| * Base class for paragraph process plugins. | |
| */ | |
| class ParagraphProcessBase extends ProcessPluginBase { | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { | |
| $paragraphs = []; | |
| foreach ($value[0] as $delta => $item) { | |
| $paragraph = $this->getParagraph($row, $destination_property, $delta); | |
| // Loop over all properties. | |
| foreach ($this->configuration['source'] as $destination => $source) { | |
| // Fetch the right delta, skip if it doesn't exist. | |
| $source_items = $row->getSourceProperty($source); | |
| if (!isset($source_items[$delta])) { | |
| continue; | |
| } | |
| $source_item = $source_items[$delta]; | |
| $destination_value = NULL; | |
| if (isset($source_item['nid'])) { | |
| $destination_value = $source_item['nid']; | |
| } | |
| elseif (isset($source_item['value'])) { | |
| $destination_value = $source_item['value']; | |
| } | |
| if ($destination_value) { | |
| $paragraph->$destination->appendItem($destination_value); | |
| } | |
| } | |
| $paragraph->save(); | |
| $paragraphs[]['entity'] = $paragraph; | |
| } | |
| return $paragraphs; | |
| } | |
| /** | |
| * Creates a new or returns an existing paragraph for the target node. | |
| * | |
| * @param \Drupal\migrate\Row $row | |
| * The migration row. | |
| * @param string $field_name | |
| * The field name. | |
| * @param int $delta | |
| * The field delta. | |
| * | |
| * @return \Drupal\paragraphs\Entity\Paragraph | |
| * The paragraph. | |
| */ | |
| protected function getParagraph(Row $row, $field_name, $delta) { | |
| // Attempt to fetch an existing paragraph if the target node already | |
| // exists, then get the translation. Otherwise create a new. | |
| if ($row->getDestinationProperty('nid') && $node = Node::load($row->getDestinationProperty('nid'))) { | |
| if (isset($node->$field_name[$delta]) && $node->$field_name[$delta]->entity) { | |
| $paragraph = $node->$field_name[$delta]->entity; | |
| if ($row->getDestinationProperty('langcode') && $row->getDestinationProperty('langcode') != LanguageInterface::LANGCODE_NOT_SPECIFIED) { | |
| if (!$paragraph->hasTranslation($row->getDestinationProperty('langcode'))) { | |
| $paragraph->addTranslation($row->getDestinationProperty('langcode')); | |
| } | |
| $paragraph = $paragraph->getTranslation($row->getDestinationProperty('langcode')); | |
| } | |
| return $paragraph; | |
| } | |
| } | |
| // Fallback, create a new paragraph. | |
| $paragraph = Paragraph::create(['type' => $this->configuration['type']]); | |
| if ($row->getDestinationProperty('langcode')) { | |
| $paragraph->langcode = $row->getDestinationProperty('langcode'); | |
| } | |
| return $paragraph; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment