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;
trait DevelGenerateSetupTrait {
use CommentTestTrait;
use EntityReferenceTestTrait;
protected $vocabulary;
public function setUpData() {
$entity_type_manager = $this->container
->get('entity_type.manager');
if ($this->profile != 'standard') {
$this
->drupalCreateContentType(array(
'type' => 'page',
'name' => 'Basic Page',
));
$this
->drupalCreateContentType(array(
'type' => 'article',
'name' => 'Article',
));
$this
->addDefaultCommentField('node', 'article');
}
$this->vocabulary = Vocabulary::create([
'name' => $this
->randomMachineName(),
'description' => $this
->randomMachineName(),
'vid' => mb_strtolower($this
->randomMachineName()),
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
'weight' => mt_rand(0, 10),
]);
$this->vocabulary
->save();
$field_name = 'taxonomy_' . $this->vocabulary
->id();
$handler_settings = array(
'target_bundles' => array(
$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, array(
'type' => 'options_select',
))
->save();
$entity_type_manager
->getStorage('entity_view_display')
->load('node.article.default')
->setComponent($field_name, array(
'type' => 'entity_reference_label',
))
->save();
}
}