You are here

lingotek_test.module in Lingotek Translation 8

File

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

use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Serialization\Yaml;

/**
 * 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
        ->setPublished(FALSE);
    }
  }
}

/**
 * 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':

      // Decode all [tokens].
      $yaml = Yaml::encode($data);
      $yaml = preg_replace_callback('/\\[\\*\\*\\*([^]]+)\\*\\*\\*\\]/', function ($matches) {
        return '[' . base64_decode($matches[1]) . ']';
      }, $yaml);
      $data = Yaml::decode($yaml);
      break;
  }
}

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

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