You are here

tmgmt_smartling.install in TMGMT Translator Smartling 8.2

Update function for the tmgmt_smartling module.

File

tmgmt_smartling.install
View source
<?php

/**
 * @file
 * Update function for the tmgmt_smartling module.
 */
use Drupal\Core\Utility\UpdateException;

/**
 * Set default custom_regexp_placeholder string.
 */
function tmgmt_smartling_update_8001() {
  $custom_regexp_placeholder = \Drupal::config('tmgmt.translator.smartling')
    ->get('settings.custom_regexp_placeholder');

  // Update custom_regexp_placeholder if not already set.
  if (empty($custom_regexp_placeholder)) {
    \Drupal::configFactory()
      ->getEditable('tmgmt.translator.smartling')
      ->set('settings.custom_regexp_placeholder', '(@|%|!)[\\w-]+')
      ->save(TRUE);
  }
}

/**
 * Add file name into job entries.
 */
function tmgmt_smartling_update_8002() {
  drupal_flush_all_caches();

  // Apply new field to a job entity.
  Drupal::service('entity.definition_update_manager')
    ->applyUpdates();
  Drupal::database()
    ->update('tmgmt_job')
    ->expression('job_file_name', 'CONCAT(:job, tmgmt_job.tjid, :underscore, tmgmt_job.source_language, :underscore, tmgmt_job.target_language, :extension)', [
    ':job' => 'JobID',
    ':underscore' => '_',
    ':extension' => '.xlf',
  ])
    ->execute();
  $translator_ids = \Drupal::configFactory()
    ->listAll('tmgmt.translator');
  foreach ($translator_ids as $id) {
    $config = \Drupal::configFactory()
      ->getEditable($id);
    if ($config && $config
      ->get('plugin') === 'smartling') {
      $config
        ->set('settings.export_format', 'xlf')
        ->save(TRUE);
    }
  }
}

/**
 * Enable needed dependency.
 */
function tmgmt_smartling_update_8003() {

  // Check if dependency exists. Not enabled/disabled, but exists or not.
  $dependency_exists = module_load_include('module', 'tmgmt_extension_suit');
  if (!$dependency_exists) {
    throw new UpdateException('Unresolved dependency: tmgmt_extension_suit (missing).');
  }

  // Enable module if it's not enabled yet.
  if (!Drupal::service('module_handler')
    ->moduleExists('tmgmt_extension_suit')) {
    Drupal::service('module_installer')
      ->install([
      'tmgmt_extension_suit',
    ]);
  }
}

/**
 * Migrate users from fake xml (html) to new xml format.
 */
function tmgmt_smartling_update_8004() {
  $translator_ids = \Drupal::configFactory()
    ->listAll('tmgmt.translator');
  foreach ($translator_ids as $id) {
    $config = \Drupal::configFactory()
      ->getEditable($id);
    if ($config && $config
      ->get('plugin') === 'smartling' && $config
      ->get('settings')['export_format'] === 'html') {
      $config
        ->set('settings.export_format', 'xml')
        ->save(TRUE);
    }
  }

  // There are might be affected titles with encoded special chars. Fix it.
  \Drupal::database()
    ->update('node_field_data', [
    'allow_delimiter_in_query' => TRUE,
  ])
    ->expression('title', "REPLACE(title, '&amp;', '&')")
    ->condition('title', '%&amp;%', 'LIKE')
    ->condition('langcode', 'en', '!=')
    ->execute();
  \Drupal::database()
    ->update('node_field_revision', [
    'allow_delimiter_in_query' => TRUE,
  ])
    ->expression('title', 'REPLACE(title, \'&amp;\', \'&\')')
    ->condition('title', '%&amp;%', 'LIKE')
    ->condition('langcode', 'en', '!=')
    ->execute();
}

/**
 * Add new field to a job entity.
 */
function tmgmt_smartling_update_8005() {
  drupal_flush_all_caches();
  Drupal::service('entity.definition_update_manager')
    ->applyUpdates();
}

Functions

Namesort descending Description
tmgmt_smartling_update_8001 Set default custom_regexp_placeholder string.
tmgmt_smartling_update_8002 Add file name into job entries.
tmgmt_smartling_update_8003 Enable needed dependency.
tmgmt_smartling_update_8004 Migrate users from fake xml (html) to new xml format.
tmgmt_smartling_update_8005 Add new field to a job entity.