View source
<?php
namespace Drupal\devel_generate\Plugin\DevelGenerate;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\devel_generate\DevelGenerateBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TermDevelGenerate extends DevelGenerateBase implements ContainerFactoryPluginInterface {
protected $vocabularyStorage;
protected $termStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $vocabulary_storage, EntityStorageInterface $term_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->vocabularyStorage = $vocabulary_storage;
$this->termStorage = $term_storage;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$entity_manager = $container
->get('entity.manager');
return new static($configuration, $plugin_id, $plugin_definition, $entity_manager
->getStorage('taxonomy_vocabulary'), $entity_manager
->getStorage('taxonomy_term'));
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$options = array();
foreach ($this->vocabularyStorage
->loadMultiple() as $vocabulary) {
$options[$vocabulary
->id()] = $vocabulary
->label();
}
$form['vids'] = array(
'#type' => 'select',
'#multiple' => TRUE,
'#title' => $this
->t('Vocabularies'),
'#required' => TRUE,
'#default_value' => 'tags',
'#options' => $options,
'#description' => $this
->t('Restrict terms to these vocabularies.'),
);
$form['num'] = array(
'#type' => 'number',
'#title' => $this
->t('Number of terms?'),
'#default_value' => $this
->getSetting('num'),
'#required' => TRUE,
'#min' => 0,
);
$form['title_length'] = array(
'#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['kill'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Delete existing terms in specified vocabularies before generating new terms.'),
'#default_value' => $this
->getSetting('kill'),
);
return $form;
}
public function generateElements(array $values) {
if ($values['kill']) {
$this
->deleteVocabularyTerms($values['vids']);
$this
->setMessage($this
->t('Deleted existing terms.'));
}
$vocabs = $this->vocabularyStorage
->loadMultiple($values['vids']);
$new_terms = $this
->generateTerms($values['num'], $vocabs, $values['title_length']);
if (!empty($new_terms)) {
$this
->setMessage($this
->t('Created the following new terms: @terms', array(
'@terms' => implode(', ', $new_terms),
)));
}
}
protected function deleteVocabularyTerms($vids) {
$tids = $this->vocabularyStorage
->getToplevelTids($vids);
$terms = $this->termStorage
->loadMultiple($tids);
$this->termStorage
->delete($terms);
}
protected function generateTerms($records, $vocabs, $maxlength = 12) {
$terms = array();
$max = db_query('SELECT MAX(tid) FROM {taxonomy_term_data}')
->fetchField();
$start = time();
for ($i = 1; $i <= $records; $i++) {
$name = $this
->getRandom()
->word(mt_rand(2, $maxlength));
$values = array(
'name' => $name,
'description' => 'description of ' . $name,
'format' => filter_fallback_format(),
'weight' => mt_rand(0, 10),
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
);
switch ($i % 2) {
case 1:
$vocab = $vocabs[array_rand($vocabs)];
$values['vid'] = $vocab
->id();
$values['parent'] = array(
0,
);
break;
default:
while (TRUE) {
$candidate = mt_rand(1, $max);
$query = db_select('taxonomy_term_data', 't');
$parent = $query
->fields('t', array(
'tid',
'vid',
))
->condition('t.vid', array_keys($vocabs), 'IN')
->condition('t.tid', $candidate, '>=')
->range(0, 1)
->execute()
->fetchAssoc();
if ($parent['tid']) {
break;
}
}
$values['parent'] = array(
$parent['tid'],
);
$values['vid'] = $parent['vid'];
break;
}
$term = $this->termStorage
->create($values);
$term->devel_generate = TRUE;
$this
->populateFields($term);
$term
->save();
$max++;
if ($i < 20) {
$terms[] = $term
->label();
}
unset($term);
}
return $terms;
}
public function validateDrushParams($args, $options = []) {
$vocabulary_name = array_shift($args);
$number = array_shift($args);
if ($number === NULL) {
$number = 10;
}
if (!$vocabulary_name) {
throw new \Exception(dt('Please provide a vocabulary machine name.'));
}
if (!$this
->isNumber($number)) {
throw new \Exception(dt('Invalid number of terms: @num', array(
'@num' => $number,
)));
}
if (!($vocabulary = $this->vocabularyStorage
->load($vocabulary_name))) {
throw new \Exception(dt('Invalid vocabulary name: @name', array(
'@name' => $vocabulary_name,
)));
}
$values = [
'num' => $number,
'kill' => $this
->isDrush8() ? drush_get_option('kill') : $options['kill'],
'title_length' => 12,
'vids' => [
$vocabulary
->id(),
],
];
return $values;
}
}