View source
<?php
namespace Drupal\sitemap\Tests;
use Drupal\taxonomy\Tests\TaxonomyTestBase;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\taxonomy\Entity\Vocabulary;
abstract class SitemapTaxonomyTestBase extends TaxonomyTestBase {
public static $modules = [
'sitemap',
'node',
'taxonomy',
];
protected $vocabulary;
protected $fieldTagsName;
protected $terms;
protected function setUp() {
parent::setUp();
$this->vocabulary = $this
->createVocabulary();
$this->user = $this
->drupalCreateUser([
'administer sitemap',
'access sitemap',
'administer nodes',
'create article content',
'administer taxonomy',
]);
$this
->drupalLogin($this->user);
$vid = $this->vocabulary
->id();
$edit = [
"show_vocabularies[{$vid}]" => $vid,
];
$this
->drupalPostForm('admin/config/search/sitemap', $edit, t('Save configuration'));
}
protected function createTerms(Vocabulary $vocabulary) {
$terms = [
$this
->createTerm($vocabulary),
$this
->createTerm($vocabulary),
$this
->createTerm($vocabulary),
];
$this->terms = $terms;
$edit = [
'terms[tid:' . $terms[0]
->id() . ':0][term][tid]' => $terms[0]
->id(),
'terms[tid:' . $terms[0]
->id() . ':0][term][parent]' => 0,
'terms[tid:' . $terms[0]
->id() . ':0][term][depth]' => 0,
'terms[tid:' . $terms[0]
->id() . ':0][weight]' => 0,
'terms[tid:' . $terms[1]
->id() . ':0][term][tid]' => $terms[1]
->id(),
'terms[tid:' . $terms[1]
->id() . ':0][term][parent]' => $terms[0]
->id(),
'terms[tid:' . $terms[1]
->id() . ':0][term][depth]' => 1,
'terms[tid:' . $terms[1]
->id() . ':0][weight]' => 0,
'terms[tid:' . $terms[2]
->id() . ':0][term][tid]' => $terms[2]
->id(),
'terms[tid:' . $terms[2]
->id() . ':0][term][parent]' => $terms[1]
->id(),
'terms[tid:' . $terms[2]
->id() . ':0][term][depth]' => 2,
'terms[tid:' . $terms[2]
->id() . ':0][weight]' => 0,
];
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $vocabulary
->get('vid') . '/overview', $edit, t('Save'));
return $terms;
}
protected function createNodeWithTerms(array $terms = []) {
if (empty($terms)) {
$this->terms = $this
->createTerms($this->vocabulary);
}
$this
->addEntityreferenceField();
$values = [];
foreach ($terms as $term) {
$values[] = $term
->getName();
}
$title = $this
->randomString();
$edit = [
'title[0][value]' => $title,
$this->fieldTagsName . '[target_id]' => implode(',', $values),
];
$this
->drupalPostForm('node/add/article', $edit, t('Save'));
}
protected function addEntityreferenceField() {
$this->fieldTagsName = 'field_' . $this->vocabulary
->id();
$handler_settings = [
'target_bundles' => [
$this->vocabulary
->id() => $this->vocabulary
->id(),
],
'auto_create' => TRUE,
];
$this
->createEntityReferenceField('node', 'article', $this->fieldTagsName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
EntityFormDisplay::load('node.article.default')
->setComponent($this->fieldTagsName, [
'type' => 'entity_reference_autocomplete_tags',
])
->save();
}
}