1a037465aee877fe7459bbc027ce213e16f10fd1
[yaffs-website] / web / modules / contrib / devel / devel_generate / src / Plugin / DevelGenerate / VocabularyDevelGenerate.php
1 <?php
2
3 namespace Drupal\devel_generate\Plugin\DevelGenerate;
4
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;
12
13 /**
14  * Provides a VocabularyDevelGenerate plugin.
15  *
16  * @DevelGenerate(
17  *   id = "vocabulary",
18  *   label = @Translation("vocabularies"),
19  *   description = @Translation("Generate a given number of vocabularies. Optionally delete current vocabularies."),
20  *   url = "vocabs",
21  *   permission = "administer devel_generate",
22  *   settings = {
23  *     "num" = 1,
24  *     "title_length" = 12,
25  *     "kill" = FALSE
26  *   }
27  * )
28  */
29 class VocabularyDevelGenerate extends DevelGenerateBase implements ContainerFactoryPluginInterface {
30
31   /**
32    * The vocabulary storage.
33    *
34    * @var \Drupal\Core\Entity\EntityStorageInterface
35    */
36   protected $vocabularyStorage;
37
38   /**
39    * Constructs a new VocabularyDevelGenerate object.
40    *
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.
49    */
50   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $entity_storage) {
51     parent::__construct($configuration, $plugin_id, $plugin_definition);
52
53     $this->vocabularyStorage = $entity_storage;
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
60     return new static(
61       $configuration, $plugin_id, $plugin_definition,
62       $container->get('entity.manager')->getStorage('taxonomy_vocabulary')
63     );
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function settingsForm(array $form, FormStateInterface $form_state) {
70     $form['num'] = array(
71       '#type' => 'number',
72       '#title' => $this->t('Number of vocabularies?'),
73       '#default_value' => $this->getSetting('num'),
74       '#required' => TRUE,
75       '#min' => 0,
76     );
77     $form['title_length'] = array(
78       '#type' => 'number',
79       '#title' => $this->t('Maximum number of characters in vocabulary names'),
80       '#default_value' => $this->getSetting('title_length'),
81       '#required' => TRUE,
82       '#min' => 2,
83       '#max' => 255,
84     );
85     $form['kill'] = array(
86       '#type' => 'checkbox',
87       '#title' => $this->t('Delete existing vocabularies before generating new ones.'),
88       '#default_value' => $this->getSetting('kill'),
89     );
90
91     return $form;
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function generateElements(array $values) {
98     if ($values['kill']) {
99       $this->deleteVocabularies();
100       $this->setMessage($this->t('Deleted existing vocabularies.'));
101     }
102
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))));
106     }
107   }
108
109   /**
110    * Deletes all vocabularies.
111    */
112   protected function deleteVocabularies() {
113     $vocabularies = $this->vocabularyStorage->loadMultiple();
114     $this->vocabularyStorage->delete($vocabularies);
115   }
116
117   /**
118    * Generates vocabularies.
119    *
120    * @param int $records
121    *   Number of vocabularies to create.
122    * @param int $maxlength
123    *   (optional) Maximum length for vocabulary name.
124    *
125    * @return array
126    *   Array containing the generated vocabularies id.
127    */
128   protected function generateVocabularies($records, $maxlength = 12) {
129     $vocabularies = array();
130
131     // Insert new data:
132     for ($i = 1; $i <= $records; $i++) {
133       $name = $this->getRandom()->word(mt_rand(2, $maxlength));
134
135       $vocabulary = $this->vocabularyStorage->create(array(
136         'name' => $name,
137         'vid' => Unicode::strtolower($name),
138         'langcode' => Language::LANGCODE_NOT_SPECIFIED,
139         'description' => "Description of $name",
140         'hierarchy' => 1,
141         'weight' => mt_rand(0, 10),
142         'multiple' => 1,
143         'required' => 0,
144         'relations' => 1,
145       ));
146
147       // Populate all fields with sample values.
148       $this->populateFields($vocabulary);
149       $vocabulary->save();
150
151       $vocabularies[] = $vocabulary->id();
152       unset($vocabulary);
153     }
154
155     return $vocabularies;
156   }
157
158   /**
159    * {@inheritdoc}
160    */
161   public function validateDrushParams($args, $options = []) {
162     $values = array(
163       'num' => array_shift($args),
164       'kill' => $this->isDrush8() ? drush_get_option('kill') : $options['kill'],
165       'title_length' => 12,
166     );
167
168     if ($this->isNumber($values['num']) == FALSE) {
169       throw new \Exception(dt('Invalid number of vocabularies: @num.', array('@num' => $values['num'])));
170     }
171
172     return $values;
173   }
174
175 }