Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / taxonomy / src / Tests / TaxonomyTranslationTestTrait.php
1 <?php
2
3 namespace Drupal\taxonomy\Tests;
4
5 @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);
6
7 use Drupal\Core\Field\FieldStorageDefinitionInterface;
8 use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
9 use Drupal\field\Entity\FieldStorageConfig;
10 use Drupal\language\Entity\ConfigurableLanguage;
11
12 /**
13  * Provides common testing base for translated taxonomy terms.
14  *
15  * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
16  *   Use \Drupal\Tests\taxonomy\Functional\TaxonomyTranslationTestTrait
17  */
18 trait TaxonomyTranslationTestTrait {
19
20   use EntityReferenceTestTrait;
21
22   /**
23    * The vocabulary.
24    *
25    * @var \Drupal\taxonomy\Entity\Vocabulary;
26    */
27   protected $vocabulary;
28
29   /**
30    * The field name for our taxonomy term field.
31    *
32    * @var string
33    */
34   protected $termFieldName = 'field_tag';
35
36   /**
37    * The langcode of the source language.
38    *
39    * @var string
40    */
41   protected $baseLangcode = 'en';
42
43   /**
44    * Target langcode for translation.
45    *
46    * @var string
47    */
48   protected $translateToLangcode = 'hu';
49
50   /**
51    * The node to check the translated value on.
52    *
53    * @var \Drupal\node\Entity\Node
54    */
55   protected $node;
56
57   /**
58    * Adds additional languages.
59    */
60   protected function setupLanguages() {
61     ConfigurableLanguage::createFromLangcode($this->translateToLangcode)->save();
62     $this->rebuildContainer();
63   }
64
65   /**
66    * Enables translations where it needed.
67    */
68   protected function enableTranslation() {
69     // Enable translation for the current entity type and ensure the change is
70     // picked up.
71     \Drupal::service('content_translation.manager')->setEnabled('node', 'article', TRUE);
72     \Drupal::service('content_translation.manager')->setEnabled('taxonomy_term', $this->vocabulary->id(), TRUE);
73     drupal_static_reset();
74     \Drupal::entityManager()->clearCachedDefinitions();
75     \Drupal::service('router.builder')->rebuild();
76     \Drupal::service('entity.definition_update_manager')->applyUpdates();
77   }
78
79   /**
80    * Adds term reference field for the article content type.
81    *
82    * @param bool $translatable
83    *   (optional) If TRUE, create a translatable term reference field. Defaults
84    *   to FALSE.
85    */
86   protected function setUpTermReferenceField() {
87     $handler_settings = [
88       'target_bundles' => [
89         $this->vocabulary->id() => $this->vocabulary->id(),
90       ],
91       'auto_create' => TRUE,
92     ];
93     $this->createEntityReferenceField('node', 'article', $this->termFieldName, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
94     $field_storage = FieldStorageConfig::loadByName('node', $this->termFieldName);
95     $field_storage->setTranslatable(FALSE);
96     $field_storage->save();
97
98     entity_get_form_display('node', 'article', 'default')
99       ->setComponent($this->termFieldName, [
100         'type' => 'entity_reference_autocomplete_tags',
101       ])
102       ->save();
103     entity_get_display('node', 'article', 'default')
104       ->setComponent($this->termFieldName, [
105         'type' => 'entity_reference_label',
106       ])
107       ->save();
108   }
109
110 }