TaxonomyTranslationTestTrait.php in Drupal 10
File
core/modules/taxonomy/tests/src/Functional/TaxonomyTranslationTestTrait.php
View source
<?php
namespace Drupal\Tests\taxonomy\Functional;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
trait TaxonomyTranslationTestTrait {
use EntityReferenceTestTrait;
protected $vocabulary;
protected $termFieldName = 'field_tag';
protected $baseLangcode = 'en';
protected $translateToLangcode = 'hu';
protected $node;
protected function setupLanguages() {
ConfigurableLanguage::createFromLangcode($this->translateToLangcode)
->save();
$this
->rebuildContainer();
}
protected function enableTranslation() {
\Drupal::service('content_translation.manager')
->setEnabled('node', 'article', TRUE);
\Drupal::service('content_translation.manager')
->setEnabled('taxonomy_term', $this->vocabulary
->id(), TRUE);
}
protected function setUpTermReferenceField() {
$handler_settings = [
'target_bundles' => [
$this->vocabulary
->id() => $this->vocabulary
->id(),
],
'auto_create' => TRUE,
];
$this
->createEntityReferenceField('node', 'article', $this->termFieldName, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$field_storage = FieldStorageConfig::loadByName('node', $this->termFieldName);
$field_storage
->setTranslatable(FALSE);
$field_storage
->save();
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('node', 'article')
->setComponent($this->termFieldName, [
'type' => 'entity_reference_autocomplete_tags',
])
->save();
$display_repository
->getViewDisplay('node', 'article')
->setComponent($this->termFieldName, [
'type' => 'entity_reference_label',
])
->save();
}
}