View source
<?php
namespace Drupal\Tests\devel_generate\Traits;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\Language;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\language\Entity\ConfigurableLanguage;
trait DevelGenerateSetupTrait {
use CommentTestTrait;
use EntityReferenceTestTrait;
protected $vocabulary;
protected $vocabulary2;
public function setUpData() {
$admin_user = $this
->drupalCreateUser([
'administer devel_generate',
'access devel information',
'access content overview',
]);
$this
->drupalLogin($admin_user);
$entity_type_manager = $this->container
->get('entity_type.manager');
if ($this->profile != 'standard') {
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic Page',
]);
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this
->addDefaultCommentField('node', 'article');
}
\Drupal::service('content_translation.manager')
->setEnabled('node', 'article', TRUE);
ConfigurableLanguage::createFromLangcode('ca')
->save();
ConfigurableLanguage::createFromLangcode('de')
->save();
ConfigurableLanguage::createFromLangcode('fr')
->save();
$this->vocabulary = Vocabulary::create([
'name' => 'Vocab 1 ' . $this
->randomString(15),
'description' => $this
->randomMachineName(),
'vid' => 'vocab_1_' . mb_strtolower($this
->randomMachineName()),
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
]);
$this->vocabulary
->save();
\Drupal::service('content_translation.manager')
->setEnabled('taxonomy_term', $this->vocabulary
->id(), TRUE);
$field_name = 'taxonomy_' . $this->vocabulary
->id();
$handler_settings = [
'target_bundles' => [
$this->vocabulary
->id() => $this->vocabulary
->id(),
],
'auto_create' => TRUE,
];
$this
->createEntityReferenceField('node', 'article', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$entity_type_manager
->getStorage('entity_form_display')
->load('node.article.default')
->setComponent($field_name, [
'type' => 'options_select',
])
->save();
$entity_type_manager
->getStorage('entity_view_display')
->load('node.article.default')
->setComponent($field_name, [
'type' => 'entity_reference_label',
])
->save();
$this->vocabulary2 = Vocabulary::create([
'name' => 'Vocab 2 ' . $this
->randomString(15),
'vid' => 'vocab_2_' . mb_strtolower($this
->randomMachineName()),
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
]);
$this->vocabulary2
->save();
}
}