/** * Set the entity of this source. */ public function setEntity(ContentEntityInterface $entity) { $this->entity = $entity; if ($this->entity->hasTranslation($this->getLanguage())) { $this->entity = $this->entity->getTranslation($this->getLanguage()); } }
/** * Transforms an entity into an array of strings. * * Parses an entity's fields and for every field it builds an array of string * to be compared. Basically this function transforms an entity into an array * of strings. * * @param ContentEntityInterface $entity * An entity containing fields. * * @return array * Array of strings resulted by parsing the entity. */ public function parseEntity(ContentEntityInterface $entity) { $result = array(); $langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(); // Load entity of current language, otherwise fields are always compared by // their default language. if ($entity->hasTranslation($langcode)) { $entity = $entity->getTranslation($langcode); } $entity_type_id = $entity->getEntityTypeId(); // Load all entity base fields. $entity_base_fields = $this->entityManager->getBaseFieldDefinitions($entity_type_id); // Loop through entity fields and transform every FieldItemList object // into an array of strings according to field type specific settings. foreach ($entity as $field_items) { $field_type = $field_items->getFieldDefinition()->getType(); $plugin_config = $this->pluginsConfig->get('field_types.' . $field_type); $plugin = NULL; if ($plugin_config && $plugin_config['type'] != 'hidden') { $plugin = $this->diffBuilderManager->createInstance($plugin_config['type'], $plugin_config['settings']); } if ($plugin) { // Configurable field. It is the responsibility of the class extending // this class to hide some configurable fields from comparison. This // class compares all configurable fields. if (!array_key_exists($field_items->getName(), $entity_base_fields)) { $build = $plugin->build($field_items); if (!empty($build)) { $result[$field_items->getName()] = $build; } } else { // Check if this field needs to be compared. $config_key = 'entity.' . $entity_type_id . '.' . $field_items->getName(); $enabled = $this->config->get($config_key); if ($enabled) { $build = $plugin->build($field_items); if (!empty($build)) { $result[$field_items->getName()] = $build; } } } } } return $result; }
/** * Builds the entity translation for the provided translation data. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The entity for which the translation should be returned. * @param array $data * The translation data for the fields. * @param string $target_langcode * The target language. * * @return \Drupal\Core\Entity\ContentEntityInterface $translation * Translation data. */ protected function makePreview(ContentEntityInterface $entity, array $data, $target_langcode) { // If the translation for this language does not exist yet, initialize it. if (!$entity->hasTranslation($target_langcode)) { $entity->addTranslation($target_langcode, $entity->toArray()); } $embeded_fields = $this->config('tmgmt_content.settings')->get('embedded_fields'); $translation = $entity->getTranslation($target_langcode); foreach ($data as $name => $field_data) { foreach (Element::children($field_data) as $delta) { $field_item = $field_data[$delta]; foreach (Element::children($field_item) as $property) { $property_data = $field_item[$property]; // If there is translation data for the field property, save it. if (isset($property_data['#translation']['#text']) && $property_data['#translate']) { $translation->get($name)->offsetGet($delta)->set($property, $property_data['#translation']['#text']); } elseif (isset($embeded_fields[$entity->getEntityTypeId()][$name])) { $this->makePreview($translation->get($name)->offsetGet($delta)->{$property}, $property_data, $target_langcode); } } } } return $translation; }
/** * Saves translation data in an entity translation. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The entity for which the translation should be saved. * @param array $data * The translation data for the fields. * @param string $target_langcode * The target language. */ protected function doSaveTranslations(ContentEntityInterface $entity, array $data, $target_langcode) { // If the translation for this language does not exist yet, initialize it. if (!$entity->hasTranslation($target_langcode)) { $entity->addTranslation($target_langcode, $entity->toArray()); } $embeded_fields = \Drupal::config('tmgmt_content.settings')->get('embedded_fields'); $translation = $entity->getTranslation($target_langcode); $manager = \Drupal::service('content_translation.manager'); $manager->getTranslationMetadata($translation)->setSource($entity->language()->getId()); foreach ($data as $name => $field_data) { foreach (Element::children($field_data) as $delta) { $field_item = $field_data[$delta]; foreach (Element::children($field_item) as $property) { $property_data = $field_item[$property]; // If there is translation data for the field property, save it. if (isset($property_data['#translation']['#text']) && $property_data['#translate']) { $translation->get($name)->offsetGet($delta)->set($property, $property_data['#translation']['#text']); } elseif (isset($embeded_fields[$entity->getEntityTypeId()][$name])) { $this->doSaveTranslations($translation->get($name)->offsetGet($delta)->{$property}, $property_data, $target_langcode); } } } } $translation->save(); }