View source
<?php
namespace Drupal\acquia_lift\Tests;
use Drupal\Core\Language\LanguageInterface;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\Entity\Term;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
trait FixturesDataTrait {
private function createVocabulary() {
$vocabulary = Vocabulary::create([
'name' => $this
->randomMachineName(),
'description' => $this
->randomMachineName(),
'vid' => mb_strtolower($this
->randomMachineName()),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'weight' => mt_rand(0, 10),
]);
$vocabulary
->save();
return $vocabulary;
}
private function createTerm(Vocabulary $vocabulary, $values = []) {
$filter_formats = filter_formats();
$format = array_pop($filter_formats);
$term = Term::create($values + [
'name' => $this
->randomMachineName(),
'description' => [
'value' => $this
->randomMachineName(),
'format' => $format
->id(),
],
'vid' => $vocabulary
->id(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$term
->save();
return $term;
}
private function createFieldWithStorage($field_name, $entity_type, $bundle, $target_bundles, $storage_settings, $storage_type) {
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => $entity_type,
'translatable' => FALSE,
'settings' => $storage_settings,
'type' => $storage_type,
'cardinality' => 1,
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'entity_type' => $entity_type,
'bundle' => $bundle,
'settings' => [
'handler' => 'default',
'handler_settings' => [
'target_bundles' => $target_bundles,
],
],
]);
$field
->save();
}
}