You are here

public function Xml::import in TMGMT Translator Smartling 8.4

Same name and namespace in other branches
  1. 8 src/Plugin/tmgmt_file/Format/Xml.php \Drupal\tmgmt_smartling\Plugin\tmgmt_file\Format\Xml::import()
  2. 8.2 src/Plugin/tmgmt_file/Format/Xml.php \Drupal\tmgmt_smartling\Plugin\tmgmt_file\Format\Xml::import()
  3. 8.3 src/Plugin/tmgmt_file/Format/Xml.php \Drupal\tmgmt_smartling\Plugin\tmgmt_file\Format\Xml::import()

Implements TMGMTFileExportInterface::import().

Overrides Html::import

File

src/Plugin/tmgmt_file/Format/Xml.php, line 84

Class

Xml
Export into XML.

Namespace

Drupal\tmgmt_smartling\Plugin\tmgmt_file\Format

Code

public function import($imported_file, $job = TRUE) {
  libxml_use_internal_errors(true);
  $dom = new \DOMDocument();
  $dom
    ->loadHTMLFile($imported_file);
  $xml = simplexml_import_dom($dom);
  $data = [];
  $lock_fields_manager = \Drupal::getContainer()
    ->get('tmgmt_smartling.lock_fields_manager');

  // Get job items data from xml.
  foreach ($xml
    ->xpath("//div[@class='atom']|//span[@class='atom']") as $atom) {

    // Assets are our strings (eq fields in nodes).
    $key = $this
      ->decodeIdSafeBase64((string) $atom['id']);
    $data[$key]['#text'] = (string) $atom;
    $sl_variant_data = $this
      ->parseSmartlingSlVariantKey((string) $atom['sl-variant']);
    $lang_code = $job
      ->getTargetLangcode();
    if ($sl_variant_data) {
      $locked_fields = $lock_fields_manager
        ->getLockedFieldsByContentEntityData($sl_variant_data['entity_type'], $sl_variant_data['entity_id'], $lang_code);

      // In case if field which is being processed is locked we don't read
      // its value from file but we do read its value from entity's translated
      // field instead.
      if (in_array($sl_variant_data['field_name'], $locked_fields)) {
        $data[$key]['#text'] = $lock_fields_manager
          ->getLockedFieldValue($sl_variant_data['entity_type'], $sl_variant_data['entity_id'], $lang_code, $sl_variant_data['field_name'], $sl_variant_data['field_index'], $sl_variant_data['field_value_name']);
      }
    }

    // If we have some markup in plain text fields we need to decode it.
    if ($atom
      ->getName() == 'span') {
      $data[$key]['#text'] = html_entity_decode($data[$key]['#text']);
    }
    $data[$key]['#text'] = $this
      ->unEscapePluralStringDelimiter($data[$key]['#text']);
  }
  $this->moduleHandler
    ->alter('tmgmt_smartling_xml_file_import_data', $data);
  return \Drupal::service('tmgmt.data')
    ->unflatten($data);

  // TODO: identical filename task.
  // Map job items from xml to job items from a given job.
  // $result = [];
  // $data = \Drupal::service('tmgmt.data')->unflatten($data);
  //
  // foreach ($data as $data_key => $data_item) {
  //   $conditions = explode(':', $data_key);
  //   $job_item = $job->getItems([
  //     'item_type' => $conditions[0],
  //     'item_id' => $conditions[1],
  //   ]);
  //   $job_item = reset($job_item);
  //
  //   if (!empty($job_item)) {
  //     $result[$job_item->id()] = $data_item;
  //   }
  // }
  //
  // return $result;
}