From 4cdb8ec181a83cccb7d38e34ee10b42e277c3531 Mon Sep 17 00:00:00 2001 From: tuutti Date: Tue, 9 Feb 2021 15:49:40 +0200 Subject: [PATCH] Moved TranslatableEntityBase from TPR module to api base (#10) --- .../destination/TranslatableEntityBase.php | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 src/Plugin/migrate/destination/TranslatableEntityBase.php diff --git a/src/Plugin/migrate/destination/TranslatableEntityBase.php b/src/Plugin/migrate/destination/TranslatableEntityBase.php new file mode 100644 index 00000000..b69d5b84 --- /dev/null +++ b/src/Plugin/migrate/destination/TranslatableEntityBase.php @@ -0,0 +1,147 @@ +languageManager = $container->get('language_manager'); + + return $instance; + } + + /** + * {@inheritdoc} + */ + public function updateEntity(EntityInterface $entity, Row $row) { + $entity = parent::updateEntity($entity, $row); + // Always delete on rollback, even if it's "default" translation. + $this->setRollbackAction($row->getIdMap(), MigrateIdMapInterface::ROLLBACK_DELETE); + + return $entity; + } + + /** + * {@inheritdoc} + */ + protected function getEntity(Row $row, array $old_destination_id_values) { + $default_language = $this->languageManager->getDefaultLanguage(); + $row = $this->populateFieldTranslations($default_language, $row); + /** @var \Drupal\helfi_tpr\Entity\Unit $entity */ + $entity = parent::getEntity($row, $old_destination_id_values); + + $languages = $this->languageManager->getLanguages(); + unset($languages[$default_language->getId()]); + + foreach ($this->languageManager->getLanguages() as $langcode => $language) { + $languageRow = $this->populateFieldTranslations($language, $row); + + if ($entity->hasTranslation($langcode)) { + // Update existing translation. + $translation = $entity->getTranslation($langcode); + $this->updateEntity($translation, $languageRow); + } + else { + // Stubs might need some required fields filled in. + if ($languageRow->isStub()) { + $this->processStubRow($languageRow); + } + $translation = $entity->addTranslation($langcode, $languageRow->getDestination()); + $translation->enforceIsNew(); + } + } + return $entity; + } + + /** + * Gets the translatable source fields. + * + * Defined as remote field name => local field name: + * + * @code + * [ + * 'name => 'field_name', + * 'www' => 'field_url', + * ] + * @endcode + * Language code will be appended to remote field automatically. For + * example the field `name` will become name_fi, name_en etc. + * + * @return string[] + * An array of source fields. + */ + abstract protected function getTranslatableFields() : array; + + /** + * Populates the row object values. + * + * @param \Drupal\Core\Language\LanguageInterface $language + * The language. + * @param \Drupal\migrate\Row $row + * The row. + * + * @return \Drupal\migrate\Row + * The row. + */ + protected function populateFieldTranslations(LanguageInterface $language, Row $row) : Row { + $langcode = $language->getId(); + + if (!$row->get('langcode')) { + $row->setDestinationProperty('langcode', $langcode); + } + + foreach ($this->getTranslatableFields() as $remote => $local) { + $field = sprintf('%s_%s', $remote, $langcode); + + // Attempt to read source property in current language and fallback to + // finnish. + $value = $row->hasSourceProperty($field) ? $row->getSourceProperty($field) : $row->getSourceProperty(sprintf('%s_fi', $remote)); + + if (!$value) { + continue; + } + $row->setDestinationProperty($local, $value); + } + + return $row; + } + + /** + * {@inheritdoc} + */ + public function getIds() { + return ['id' => ['type' => 'string']]; + } + +}