lingotek_test.module in Lingotek Translation 3.2.x
Same filename and directory in other branches
- 8 tests/modules/lingotek_test/lingotek_test.module
- 8.2 tests/modules/lingotek_test/lingotek_test.module
- 4.0.x tests/modules/lingotek_test/lingotek_test.module
- 3.0.x tests/modules/lingotek_test/lingotek_test.module
- 3.1.x tests/modules/lingotek_test/lingotek_test.module
- 3.3.x tests/modules/lingotek_test/lingotek_test.module
- 3.4.x tests/modules/lingotek_test/lingotek_test.module
- 3.5.x tests/modules/lingotek_test/lingotek_test.module
- 3.6.x tests/modules/lingotek_test/lingotek_test.module
- 3.7.x tests/modules/lingotek_test/lingotek_test.module
- 3.8.x tests/modules/lingotek_test/lingotek_test.module
Provides a fake Lingotek API endpoint and other help for testing purposes.
File
tests/modules/lingotek_test/lingotek_test.moduleView 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 === 'system.rss') {
// Replace Llamas with Cats.
$data['system.rss']['channel.description'] = str_replace('Llamas', 'Cats', $data['system.rss']['channel.description']);
}
}
/**
* Implements hook_lingotek_content_object_translation_presave().
*/
function lingotek_test_lingotek_config_object_translation_presave(array &$data, $config_name) {
if ($config_name === 'system.rss') {
// Replace Gatos with Perros.
$data['system.rss']['channel.description'] = str_replace('Gatos', 'Perros', $data['system.rss']['channel.description']);
}
}
Functions
Name | Description |
---|---|
lingotek_test_lingotek_config_entity_document_upload | Implements hook_lingotek_config_entity_document_upload(). |
lingotek_test_lingotek_config_entity_translation_presave | Implements hook_lingotek_content_entity_translation_presave(). |
lingotek_test_lingotek_config_object_document_upload | Implements hook_lingotek_content_object_translation_upload(). |
lingotek_test_lingotek_config_object_translation_presave | Implements hook_lingotek_content_object_translation_presave(). |
lingotek_test_lingotek_content_entity_document_upload | Implements hook_lingotek_content_entity_document_upload(). |
lingotek_test_lingotek_content_entity_get_profile | Implements hook_lingotek_content_entity_get_profile(). |
lingotek_test_lingotek_content_entity_translation_presave | Implements hook_lingotek_content_entity_translation_presave(). |