View source
<?php
namespace Drupal\devel_generate\Plugin\DevelGenerate;
use Drupal\content_translation\ContentTranslationManagerInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\devel_generate\DevelGenerateBase;
use Drupal\taxonomy\TermInterface;
use Drush\Utils\StringUtils;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TermDevelGenerate extends DevelGenerateBase implements ContainerFactoryPluginInterface {
protected $vocabularyStorage;
protected $termStorage;
protected $database;
protected $moduleHandler;
protected $languageManager;
protected $contentTranslationManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $vocabulary_storage, EntityStorageInterface $term_storage, Connection $database, ModuleHandlerInterface $module_handler, LanguageManagerInterface $language_manager, ContentTranslationManagerInterface $content_translation_manager = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->vocabularyStorage = $vocabulary_storage;
$this->termStorage = $term_storage;
$this->database = $database;
$this->moduleHandler = $module_handler;
$this->languageManager = $language_manager;
$this->contentTranslationManager = $content_translation_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$entity_type_manager = $container
->get('entity_type.manager');
return new static($configuration, $plugin_id, $plugin_definition, $entity_type_manager
->getStorage('taxonomy_vocabulary'), $entity_type_manager
->getStorage('taxonomy_term'), $container
->get('database'), $container
->get('module_handler'), $container
->get('language_manager'), $container
->has('content_translation.manager') ? $container
->get('content_translation.manager') : NULL);
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$options = [];
foreach ($this->vocabularyStorage
->loadMultiple() as $vocabulary) {
$options[$vocabulary
->id()] = $vocabulary
->label();
}
asort($options);
$default_vids = array_key_exists('tags', $options) ? 'tags' : '';
$form['vids'] = [
'#type' => 'select',
'#multiple' => TRUE,
'#title' => $this
->t('Vocabularies'),
'#required' => TRUE,
'#default_value' => $default_vids,
'#options' => $options,
'#description' => $this
->t('Restrict terms to these vocabularies.'),
];
$form['num'] = [
'#type' => 'number',
'#title' => $this
->t('Number of terms'),
'#default_value' => $this
->getSetting('num'),
'#required' => TRUE,
'#min' => 0,
];
$form['title_length'] = [
'#type' => 'number',
'#title' => $this
->t('Maximum number of characters in term names'),
'#default_value' => $this
->getSetting('title_length'),
'#required' => TRUE,
'#min' => 2,
'#max' => 255,
];
$form['minimum_depth'] = [
'#type' => 'number',
'#title' => $this
->t('Minimum depth for new terms in the vocabulary hierarchy'),
'#description' => $this
->t('Enter a value from 1 to 20.'),
'#default_value' => $this
->getSetting('minimum_depth'),
'#min' => 1,
'#max' => 20,
];
$form['maximum_depth'] = [
'#type' => 'number',
'#title' => $this
->t('Maximum depth for new terms in the vocabulary hierarchy'),
'#description' => $this
->t('Enter a value from 1 to 20.'),
'#default_value' => $this
->getSetting('maximum_depth'),
'#min' => 1,
'#max' => 20,
];
$form['kill'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Delete existing terms in specified vocabularies before generating new terms.'),
'#default_value' => $this
->getSetting('kill'),
];
$form += $this
->getLanguageForm('terms');
return $form;
}
public function generateElements(array $values) {
$new_terms = $this
->generateTerms($values);
if (!empty($new_terms['terms'])) {
$this
->setMessage($this
->formatPlural($new_terms['terms'], 'Created 1 new term', 'Created @count new terms'));
$format_terms_func = function ($data, $level) {
if ($data['total'] > 10) {
$data['terms'][] = '...';
}
return $this
->formatPlural($data['total'], '1 new term at level @level (@terms)', '@count new terms at level @level (@terms)', [
'@level' => $level,
'@terms' => implode(',', $data['terms']),
]);
};
foreach ($new_terms['vocabs'] as $vid => $vlabel) {
if (array_key_exists($vid, $new_terms)) {
ksort($new_terms[$vid]);
$termlist = implode(', ', array_map($format_terms_func, $new_terms[$vid], array_keys($new_terms[$vid])));
$this
->setMessage($this
->t('In vocabulary @vlabel: @termlist', [
'@vlabel' => $vlabel,
'@termlist' => $termlist,
]));
}
else {
$this
->setMessage($this
->t('In vocabulary @vlabel: No terms created', [
'@vlabel' => $vlabel,
]));
}
}
}
if ($new_terms['terms_translations'] > 0) {
$this
->setMessage($this
->formatPlural($new_terms['terms_translations'], 'Created 1 term translation', 'Created @count term translations'));
}
}
protected function deleteVocabularyTerms(array $vids) {
$tids = $this->vocabularyStorage
->getToplevelTids($vids);
$terms = $this->termStorage
->loadMultiple($tids);
$total_deleted = 0;
foreach ($vids as $vid) {
$total_deleted += count($this->termStorage
->loadTree($vid));
}
$this->termStorage
->delete($terms);
return $total_deleted;
}
protected function generateTerms(array $parameters) {
$info = [
'terms' => 0,
'terms_translations' => 0,
];
$min_depth = $parameters['minimum_depth'];
$max_depth = $parameters['maximum_depth'];
$vocabs = array_combine($parameters['vids'], $parameters['vids']);
$all_parents = [];
foreach ($parameters['vids'] as $vid) {
$info['vocabs'][$vid] = $this->vocabularyStorage
->load($vid)
->label();
$all_parents[$vid] = [
'top_level' => [],
'lower_levels' => [],
];
for ($depth = 1; $depth < $max_depth; $depth++) {
$query = \Drupal::entityQuery('taxonomy_term')
->condition('vid', $vid);
if ($depth == 1) {
$query
->condition('parent', 0);
}
else {
$query
->condition('parent', $ids, 'IN');
}
$ids = $query
->execute();
if (empty($ids)) {
break;
}
if ($depth == $min_depth - 1) {
$all_parents[$vid]['top_level'] = array_fill_keys($ids, $depth);
}
elseif ($depth >= $min_depth) {
$all_parents[$vid]['lower_levels'] += array_fill_keys($ids, $depth);
}
}
if ($min_depth == 1) {
$all_parents[$vid]['top_level'] = [
0 => 0,
];
}
elseif (empty($all_parents[$vid]['top_level'])) {
unset($vocabs[$vid]);
}
}
if (empty($vocabs)) {
throw new \Exception(sprintf('Invalid minimum depth %s because there are no terms in any vocabulary at depth %s', $min_depth, $min_depth - 1));
}
if ($parameters['kill']) {
$deleted = $this
->deleteVocabularyTerms($vocabs);
$this
->setMessage($this
->formatPlural($deleted, 'Deleted 1 existing term', 'Deleted @count existing terms'));
}
for ($i = 1; $i <= $parameters['num']; $i++) {
$vid = array_rand($vocabs);
$group = mt_rand(0, 100) < 50 || empty($all_parents[$vid]['lower_levels']) ? 'top_level' : 'lower_levels';
$parent = array_rand($all_parents[$vid][$group]);
$depth = $all_parents[$vid][$group][$parent] + 1;
$name = $this
->getRandom()
->word(mt_rand(2, $parameters['title_length']));
$values = [
'name' => $name,
'description' => 'Description of ' . $name . ' (depth ' . $depth . ')',
'format' => filter_fallback_format(),
'weight' => mt_rand(0, 10),
'vid' => $vid,
'parent' => [
$parent,
],
];
if (isset($parameters['add_language'])) {
$values['langcode'] = $this
->getLangcode($parameters['add_language']);
}
$term = $this->termStorage
->create($values);
$term->devel_generate = TRUE;
$this
->populateFields($term);
$term
->save();
if (isset($parameters['translate_language']) && !empty($parameters['translate_language'])) {
$info['terms_translations'] += $this
->generateTermTranslation($parameters['translate_language'], $term);
}
if ($depth < $max_depth) {
$all_parents[$vid]['lower_levels'] += [
$term
->id() => $depth,
];
}
$info['terms']++;
@$info[$vid][$depth]['total']++;
if (!isset($info[$vid][$depth]['terms']) || count($info[$vid][$depth]['terms']) < 10) {
$info[$vid][$depth]['terms'][] = $term
->label();
}
unset($term);
}
return $info;
}
protected function generateTermTranslation(array $translate_language, TermInterface $term) {
if (is_null($this->contentTranslationManager)) {
return 0;
}
if (!$this->contentTranslationManager
->isEnabled('taxonomy_term', $term
->bundle())) {
return 0;
}
if ($term->langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED || $term->langcode == LanguageInterface::LANGCODE_NOT_APPLICABLE) {
return 0;
}
$num_translations = 0;
$skip_languages = [
LanguageInterface::LANGCODE_NOT_SPECIFIED,
LanguageInterface::LANGCODE_NOT_APPLICABLE,
$term->langcode->value,
];
foreach ($translate_language as $langcode) {
if (in_array($langcode, $skip_languages)) {
continue;
}
$translation_term = $term
->addTranslation($langcode);
$translation_term
->setName($term
->getName() . ' (' . $langcode . ')');
$this
->populateFields($translation_term);
$translation_term
->save();
$num_translations++;
}
return $num_translations;
}
public function validateDrushParams(array $args, array $options = []) {
$defaultSettings = $this
->getDefaultSettings();
$bundles = StringUtils::csvToarray($options['bundles']);
if (count($bundles) < 1) {
throw new \Exception(dt('Please provide a vocabulary machine name (--bundles).'));
}
foreach ($bundles as $bundle) {
if (!$this->vocabularyStorage
->load($bundle)) {
throw new \Exception(dt('Invalid vocabulary machine name: @name', [
'@name' => $bundle,
]));
}
}
$number = array_shift($args) ?: $defaultSettings['num'];
if (!$this
->isNumber($number)) {
throw new \Exception(dt('Invalid number of terms: @num', [
'@num' => $number,
]));
}
$minimum_depth = $options['min-depth'] ?? $defaultSettings['minimum_depth'];
$maximum_depth = $options['max-depth'] ?? $defaultSettings['maximum_depth'];
if ($minimum_depth < 1 || $minimum_depth > 20 || $maximum_depth < 1 || $maximum_depth > 20 || $minimum_depth > $maximum_depth) {
throw new \Exception(dt('The depth values must be in the range 1 to 20 and min-depth cannot be larger than max-depth (values given: min-depth @min, max-depth @max)', [
'@min' => $minimum_depth,
'@max' => $maximum_depth,
]));
}
$values = [
'num' => $number,
'kill' => $options['kill'],
'title_length' => 12,
'vids' => $bundles,
'minimum_depth' => $minimum_depth,
'maximum_depth' => $maximum_depth,
];
$add_language = StringUtils::csvToArray($options['languages']);
$valid_languages = array_keys($this->languageManager
->getLanguages(LanguageInterface::STATE_ALL));
$values['add_language'] = array_intersect($add_language, $valid_languages);
$translate_language = StringUtils::csvToArray($options['translations']);
$values['translate_language'] = array_intersect($translate_language, $valid_languages);
return $values;
}
}