You are here

public function Html::validateImport in Translation Management Tool 8

Validates that the given file is valid and can be imported.

Parameters

$imported_file: File path to the file to be imported.

bool $is_file: (optional) Whether $imported_file is the path to a file or not.

Return value

Job Returns the corresponding translation job entity if the import file is valid, FALSE otherwise.

Overrides FormatInterface::validateImport

File

translators/tmgmt_file/src/Plugin/tmgmt_file/Format/Html.php, line 79

Class

Html
Export into HTML.

Namespace

Drupal\tmgmt_file\Plugin\tmgmt_file\Format

Code

public function validateImport($imported_file, $is_file = TRUE) {
  $dom = new \DOMDocument();
  if (!$dom
    ->loadHTMLFile($imported_file)) {
    return FALSE;
  }
  $xml = simplexml_import_dom($dom);

  // Collect meta information.
  $meta_tags = $xml
    ->xpath('//meta');
  $meta = array();
  foreach ($meta_tags as $meta_tag) {
    $meta[(string) $meta_tag['name']] = (string) $meta_tag['content'];
  }

  // Check required meta tags.
  foreach (array(
    'JobID',
    'languageSource',
    'languageTarget',
  ) as $name) {
    if (!isset($meta[$name])) {
      return FALSE;
    }
  }

  // Attempt to load the job.
  if (!($job = Job::load($meta['JobID']))) {
    \Drupal::messenger()
      ->addError(t('The imported file job id @file_id is not available.', array(
      '@file_id' => $meta['JobID'],
    )));
    return FALSE;
  }

  // Check language.
  if ($meta['languageSource'] != $job
    ->getRemoteSourceLanguage() || $meta['languageTarget'] != $job
    ->getRemoteTargetLanguage()) {
    return FALSE;
  }

  // Validation successful.
  return $job;
}