outline-8.x-1.x-dev/tests/src/Functional/OutlineTranslationTestTrait.php
tests/src/Functional/OutlineTranslationTestTrait.php
<?php
namespace Drupal\Tests\outline\Functional;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
/**
* Provides common testing base for translated outline entries.
*/
trait OutlineTranslationTestTrait {
use EntityReferenceTestTrait;
/**
* The outline.
*
* @var \Drupal\outline\Entity\Outline
*/
protected $outline;
/**
* The field name for our outline entry field.
*
* @var string
*/
protected $entryFieldName = '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();
}
/**
* 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('outline_entry', $this->outline->id(), TRUE);
}
/**
* Adds entry reference field for the article content type.
*/
protected function setUpEntryReferenceField() {
$handler_settings = [
'target_bundles' => [
$this->outline->id() => $this->outline->id(),
],
'auto_create' => TRUE,
];
$this->createEntityReferenceField('node', 'article', $this->entryFieldName, NULL, 'outline_entry', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$field_storage = FieldStorageConfig::loadByName('node', $this->entryFieldName);
$field_storage->setTranslatable(FALSE);
$field_storage->save();
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');
$display_repository->getFormDisplay('node', 'article')
->setComponent($this->entryFieldName, [
'type' => 'entity_reference_autocomplete_tags',
])
->save();
$display_repository->getViewDisplay('node', 'article')
->setComponent($this->entryFieldName, [
'type' => 'entity_reference_label',
])
->save();
}
}
