Xml.php in TMGMT Translator Smartling 8.3
File
src/Plugin/tmgmt_file/Format/Xml.php
View source
<?php
namespace Drupal\tmgmt_smartling\Plugin\tmgmt_file\Format;
use Drupal\tmgmt\Entity\Job;
use Drupal\tmgmt\JobInterface;
use Drupal\tmgmt_file\Plugin\tmgmt_file\Format\Html;
class Xml extends Html {
public function export(JobInterface $job, $conditions = []) {
$items = [];
foreach ($job
->getItems($conditions) as $item) {
$data = \Drupal::service('tmgmt.data')
->filterTranslatable($item
->getData());
foreach ($data as $key => $value) {
$items[$item
->id()][$this
->encodeIdSafeBase64($item
->id() . '][' . $key)] = $value + [
'sl-variant' => $item
->getItemType() . '-' . $item
->getItemId() . '-' . $key,
];
}
}
$variables = [
'items' => $items,
];
$theme_registry = theme_get_registry();
$info = $theme_registry['tmgmt_smartling_xml_template'];
$template_file = $info['template'] . '.html.twig';
if (isset($info['path'])) {
$template_file = $info['path'] . '/' . $template_file;
}
return $this
->escapePluralStringDelimiter(\Drupal::service('twig')
->loadTemplate($template_file)
->render($variables));
}
public function validateImport($imported_file, $job = TRUE) {
$xml = simplexml_load_file($imported_file);
if (!$xml) {
return FALSE;
}
return TRUE;
}
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 = [];
foreach ($xml
->xpath("//div[@class='atom']|//span[@class='atom']") as $atom) {
$key = $this
->decodeIdSafeBase64((string) $atom['id']);
$data[$key]['#text'] = (string) $atom;
if ($atom
->getName() == 'span') {
$data[$key]['#text'] = html_entity_decode($data[$key]['#text']);
}
$data[$key]['#text'] = $this
->unEscapePluralStringDelimiter($data[$key]['#text']);
}
return \Drupal::service('tmgmt.data')
->unflatten($data);
}
protected function escapePluralStringDelimiter($string) {
return preg_replace("/\3/", "!PLURAL_STRING_DELIMITER", $string);
}
protected function unEscapePluralStringDelimiter($string) {
return preg_replace("/!PLURAL_STRING_DELIMITER/", "\3", $string);
}
}
Classes
Name |
Description |
Xml |
Export into XML. |