3 namespace Drupal\devel_generate\Plugin\DevelGenerate;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Language\Language;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Drupal\devel_generate\DevelGenerateBase;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Provides a VocabularyDevelGenerate plugin.
18 * label = @Translation("vocabularies"),
19 * description = @Translation("Generate a given number of vocabularies. Optionally delete current vocabularies."),
21 * permission = "administer devel_generate",
24 * "title_length" = 12,
29 class VocabularyDevelGenerate extends DevelGenerateBase implements ContainerFactoryPluginInterface {
32 * The vocabulary storage.
34 * @var \Drupal\Core\Entity\EntityStorageInterface
36 protected $vocabularyStorage;
39 * Constructs a new VocabularyDevelGenerate object.
41 * @param array $configuration
42 * A configuration array containing information about the plugin instance.
43 * @param string $plugin_id
44 * The plugin_id for the plugin instance.
45 * @param mixed $plugin_definition
46 * The plugin implementation definition.
47 * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
48 * The vocabulary storage.
50 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $entity_storage) {
51 parent::__construct($configuration, $plugin_id, $plugin_definition);
53 $this->vocabularyStorage = $entity_storage;
59 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
61 $configuration, $plugin_id, $plugin_definition,
62 $container->get('entity.manager')->getStorage('taxonomy_vocabulary')
69 public function settingsForm(array $form, FormStateInterface $form_state) {
72 '#title' => $this->t('Number of vocabularies?'),
73 '#default_value' => $this->getSetting('num'),
77 $form['title_length'] = array(
79 '#title' => $this->t('Maximum number of characters in vocabulary names'),
80 '#default_value' => $this->getSetting('title_length'),
85 $form['kill'] = array(
86 '#type' => 'checkbox',
87 '#title' => $this->t('Delete existing vocabularies before generating new ones.'),
88 '#default_value' => $this->getSetting('kill'),
97 public function generateElements(array $values) {
98 if ($values['kill']) {
99 $this->deleteVocabularies();
100 $this->setMessage($this->t('Deleted existing vocabularies.'));
103 $new_vocs = $this->generateVocabularies($values['num'], $values['title_length']);
104 if (!empty($new_vocs)) {
105 $this->setMessage($this->t('Created the following new vocabularies: @vocs', array('@vocs' => implode(', ', $new_vocs))));
110 * Deletes all vocabularies.
112 protected function deleteVocabularies() {
113 $vocabularies = $this->vocabularyStorage->loadMultiple();
114 $this->vocabularyStorage->delete($vocabularies);
118 * Generates vocabularies.
120 * @param int $records
121 * Number of vocabularies to create.
122 * @param int $maxlength
123 * (optional) Maximum length for vocabulary name.
126 * Array containing the generated vocabularies id.
128 protected function generateVocabularies($records, $maxlength = 12) {
129 $vocabularies = array();
132 for ($i = 1; $i <= $records; $i++) {
133 $name = $this->getRandom()->word(mt_rand(2, $maxlength));
135 $vocabulary = $this->vocabularyStorage->create(array(
137 'vid' => Unicode::strtolower($name),
138 'langcode' => Language::LANGCODE_NOT_SPECIFIED,
139 'description' => "Description of $name",
141 'weight' => mt_rand(0, 10),
147 // Populate all fields with sample values.
148 $this->populateFields($vocabulary);
151 $vocabularies[] = $vocabulary->id();
155 return $vocabularies;
161 public function validateDrushParams($args, $options = []) {
163 'num' => array_shift($args),
164 'kill' => $this->isDrush8() ? drush_get_option('kill') : $options['kill'],
165 'title_length' => 12,
168 if ($this->isNumber($values['num']) == FALSE) {
169 throw new \Exception(dt('Invalid number of vocabularies: @num.', array('@num' => $values['num'])));