Newer
Older
Alex Pott
committed
<?php
namespace Drupal\taxonomy\Tests;
Lee Rowlands
committed
@trigger_error(__NAMESPACE__ . '\TaxonomyTranslationTestTrait is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use \Drupal\Tests\taxonomy\Functional\TaxonomyTranslationTestTrait', E_USER_DEPRECATED);
Alex Pott
committed
use Drupal\Core\Field\FieldStorageDefinitionInterface;
Alex Pott
committed
use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
Angie Byron
committed
use Drupal\field\Entity\FieldStorageConfig;
Alex Pott
committed
use Drupal\language\Entity\ConfigurableLanguage;
/**
* Provides common testing base for translated taxonomy terms.
Lee Rowlands
committed
*
* @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
* Use \Drupal\Tests\taxonomy\Functional\TaxonomyTranslationTestTrait
Alex Pott
committed
*/
trait TaxonomyTranslationTestTrait {
Angie Byron
committed
use EntityReferenceTestTrait;
Alex Pott
committed
/**
* The vocabulary.
*
Lee Rowlands
committed
* @var \Drupal\taxonomy\Entity\Vocabulary
Alex Pott
committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
*/
protected $vocabulary;
/**
* The field name for our taxonomy term field.
*
* @var string
*/
protected $termFieldName = 'field_tag';
/**
* The langcode of the source language.
*
* @var string
*/
protected $baseLangcode = 'en';
/**
* Target langcode for translation.
*
* @var string
*/
protected $translateToLangcode = 'hu';
/**
* The node to check the translated value on.
*
* @var \Drupal\node\Entity\Node
*/
protected $node;
/**
* Adds additional languages.
*/
protected function setupLanguages() {
ConfigurableLanguage::createFromLangcode($this->translateToLangcode)->save();
$this->rebuildContainer();
Alex Pott
committed
}
/**
* Enables translations where it needed.
*/
protected function enableTranslation() {
// Enable translation for the current entity type and ensure the change is
// picked up.
\Drupal::service('content_translation.manager')->setEnabled('node', 'article', TRUE);
\Drupal::service('content_translation.manager')->setEnabled('taxonomy_term', $this->vocabulary->id(), TRUE);
drupal_static_reset();
\Drupal::entityManager()->clearCachedDefinitions();
\Drupal::service('router.builder')->rebuild();
\Drupal::service('entity.definition_update_manager')->applyUpdates();
Alex Pott
committed
}
/**
* Adds term reference field for the article content type.
*/
Alex Pott
committed
protected function setUpTermReferenceField() {
$handler_settings = [
'target_bundles' => [
Angie Byron
committed
$this->vocabulary->id() => $this->vocabulary->id(),
],
Angie Byron
committed
'auto_create' => TRUE,
];
Angie Byron
committed
$this->createEntityReferenceField('node', 'article', $this->termFieldName, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$field_storage = FieldStorageConfig::loadByName('node', $this->termFieldName);
Alex Pott
committed
$field_storage->setTranslatable(FALSE);
Angie Byron
committed
$field_storage->save();
Alex Pott
committed
entity_get_form_display('node', 'article', 'default')
->setComponent($this->termFieldName, [
Angie Byron
committed
'type' => 'entity_reference_autocomplete_tags',
])
Alex Pott
committed
->save();
entity_get_display('node', 'article', 'default')
->setComponent($this->termFieldName, [
Angie Byron
committed
'type' => 'entity_reference_label',
])