You are here

lingotek_test.module in Lingotek Translation 3.3.x

Provides a fake Lingotek API endpoint and other help for testing purposes.

File

tests/modules/lingotek_test/lingotek_test.module
View source
<?php

/**
 * @file
 * Provides a fake Lingotek API endpoint and other help for testing purposes.
 */
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\lingotek\LingotekProfileInterface;

/**
 * Implements hook_lingotek_content_entity_translation_presave().
 *
 * If the translation being saved is a press release, and it is the first time it
 * is downloaded, always save them unpublished.
 */
function lingotek_test_lingotek_content_entity_translation_presave(ContentEntityInterface &$translation, $langcode, $data) {
  if ($translation
    ->getEntityTypeId() === 'node' && $translation
    ->bundle() === 'press_release') {
    if ($translation
      ->isNewTranslation()) {

      /** @var \Drupal\node\NodeInterface $translation */
      $translation
        ->setUnpublished();
    }
  }
}

/**
 * Implements hook_lingotek_content_entity_get_profile().
 *
 * If the document being uploaded is a comment, use the profile from the parent.
 */
function lingotek_test_lingotek_content_entity_get_profile(ContentEntityInterface $entity, LingotekProfileInterface &$profile = NULL, $provide_default = TRUE) {
  if ($entity
    ->getEntityTypeId() === 'comment') {

    /** @var \Drupal\comment\CommentInterface $entity */
    $commented = $entity
      ->getCommentedEntity();

    /** @var \Drupal\lingotek\LingotekConfigurationServiceInterface $lingotek_config */
    $lingotek_config = \Drupal::service('lingotek.configuration');
    $profile = $lingotek_config
      ->getEntityProfile($commented, FALSE);
  }
  if ($entity
    ->getEntityTypeId() === 'node' && $entity
    ->bundle() === 'null_profile') {
    $profile = NULL;
  }
}

/**
 * Implements hook_lingotek_content_entity_document_upload().
 *
 * If the document being uploaded is a press release, we add a field with the
 * publication and we modify the original url.
 */
function lingotek_test_lingotek_content_entity_document_upload(array &$source_data, ContentEntityInterface &$entity, &$url) {
  if ($entity
    ->getEntityTypeId() === 'node' && $entity
    ->bundle() === 'animal') {
    $url = \Drupal::request()
      ->getBasePath() . '/animal/2016/llamas-are-cool';
    $source_data['animal_date'] = '2016-05-01';
  }
}

/**
 * Implements hook_lingotek_content_entity_translation_presave().
 */
function lingotek_test_lingotek_config_entity_translation_presave(ConfigEntityInterface &$translation, $langcode, &$data) {
  switch ($translation
    ->getEntityTypeId()) {
    case 'block':

      // Lowercase all [tokens] and remove the asterisks prefix.
      $yaml = Yaml::encode($data);
      $yaml = preg_replace_callback('/\\[\\*\\*\\*([^]]+)\\*\\*\\*\\]/', function ($matches) {
        return '[' . strtolower($matches[1]) . ']';
      }, $yaml);
      $data = Yaml::decode($yaml);
      break;
  }
}

/**
 * Implements hook_lingotek_config_entity_document_upload().
 */
function lingotek_test_lingotek_config_entity_document_upload(array &$source_data, ConfigEntityInterface &$entity, &$url) {
  switch ($entity
    ->getEntityTypeId()) {
    case 'block':

      // Uppercase all [tokens] and prefix with asterisks.
      $yaml = Yaml::encode($source_data);
      $yaml = preg_replace_callback('/\\[([a-z][^]]+)\\]/', function ($matches) {
        return '[***' . strtoupper($matches[1]) . '***]';
      }, $yaml);
      $source_data = Yaml::decode($yaml);
      break;
  }
}

/**
 * Implements hook_lingotek_content_object_translation_upload().
 */
function lingotek_test_lingotek_config_object_document_upload(array &$data, $config_name) {
  if ($config_name === 'lingotek_test_config_object.settings') {

    // Replace Llamas with Cats.
    $data['lingotek_test_config_object.settings']['property_3'] = str_replace('Llamas', 'Cats', $data['lingotek_test_config_object.settings']['property_3']);
    $data['lingotek_test_config_object.settings']['property_5'] = str_replace('Llamas', 'Cats', $data['lingotek_test_config_object.settings']['property_5']);
  }
}

/**
 * Implements hook_lingotek_content_object_translation_presave().
 */
function lingotek_test_lingotek_config_object_translation_presave(array &$data, $config_name) {
  if ($config_name === 'lingotek_test_config_object.settings') {

    // Replace Gatos with Perros.
    $data['lingotek_test_config_object.settings']['property_3'] = str_replace('Gatos', 'Perros', $data['lingotek_test_config_object.settings']['property_3']);
    $data['lingotek_test_config_object.settings']['property_5'] = str_replace('Gatos', 'Perros', $data['lingotek_test_config_object.settings']['property_5']);
  }
}