VocabularyDevelGenerate.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Drupal\devel_generate\Plugin\DevelGenerate;
  3. use Drupal\Component\Utility\Unicode;
  4. use Drupal\Core\Entity\EntityStorageInterface;
  5. use Drupal\Core\Form\FormStateInterface;
  6. use Drupal\Core\Language\Language;
  7. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  8. use Drupal\devel_generate\DevelGenerateBase;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. /**
  11. * Provides a VocabularyDevelGenerate plugin.
  12. *
  13. * @DevelGenerate(
  14. * id = "vocabulary",
  15. * label = @Translation("vocabularies"),
  16. * description = @Translation("Generate a given number of vocabularies. Optionally delete current vocabularies."),
  17. * url = "vocabs",
  18. * permission = "administer devel_generate",
  19. * settings = {
  20. * "num" = 1,
  21. * "title_length" = 12,
  22. * "kill" = FALSE
  23. * }
  24. * )
  25. */
  26. class VocabularyDevelGenerate extends DevelGenerateBase implements ContainerFactoryPluginInterface {
  27. /**
  28. * The vocabulary storage.
  29. *
  30. * @var \Drupal\Core\Entity\EntityStorageInterface
  31. */
  32. protected $vocabularyStorage;
  33. /**
  34. * Constructs a new VocabularyDevelGenerate object.
  35. *
  36. * @param array $configuration
  37. * A configuration array containing information about the plugin instance.
  38. * @param string $plugin_id
  39. * The plugin_id for the plugin instance.
  40. * @param mixed $plugin_definition
  41. * The plugin implementation definition.
  42. * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
  43. * The vocabulary storage.
  44. */
  45. public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $entity_storage) {
  46. parent::__construct($configuration, $plugin_id, $plugin_definition);
  47. $this->vocabularyStorage = $entity_storage;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  53. return new static(
  54. $configuration, $plugin_id, $plugin_definition,
  55. $container->get('entity.manager')->getStorage('taxonomy_vocabulary')
  56. );
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function settingsForm(array $form, FormStateInterface $form_state) {
  62. $form['num'] = array(
  63. '#type' => 'number',
  64. '#title' => $this->t('Number of vocabularies?'),
  65. '#default_value' => $this->getSetting('num'),
  66. '#required' => TRUE,
  67. '#min' => 0,
  68. );
  69. $form['title_length'] = array(
  70. '#type' => 'number',
  71. '#title' => $this->t('Maximum number of characters in vocabulary names'),
  72. '#default_value' => $this->getSetting('title_length'),
  73. '#required' => TRUE,
  74. '#min' => 2,
  75. '#max' => 255,
  76. );
  77. $form['kill'] = array(
  78. '#type' => 'checkbox',
  79. '#title' => $this->t('Delete existing vocabularies before generating new ones.'),
  80. '#default_value' => $this->getSetting('kill'),
  81. );
  82. return $form;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function generateElements(array $values) {
  88. if ($values['kill']) {
  89. $this->deleteVocabularies();
  90. $this->setMessage($this->t('Deleted existing vocabularies.'));
  91. }
  92. $new_vocs = $this->generateVocabularies($values['num'], $values['title_length']);
  93. if (!empty($new_vocs)) {
  94. $this->setMessage($this->t('Created the following new vocabularies: @vocs', array('@vocs' => implode(', ', $new_vocs))));
  95. }
  96. }
  97. /**
  98. * Deletes all vocabularies.
  99. */
  100. protected function deleteVocabularies() {
  101. $vocabularies = $this->vocabularyStorage->loadMultiple();
  102. $this->vocabularyStorage->delete($vocabularies);
  103. }
  104. /**
  105. * Generates vocabularies.
  106. *
  107. * @param int $records
  108. * Number of vocabularies to create.
  109. * @param int $maxlength
  110. * (optional) Maximum length for vocabulary name.
  111. *
  112. * @return array
  113. * Array containing the generated vocabularies id.
  114. */
  115. protected function generateVocabularies($records, $maxlength = 12) {
  116. $vocabularies = array();
  117. // Insert new data:
  118. for ($i = 1; $i <= $records; $i++) {
  119. $name = $this->getRandom()->word(mt_rand(2, $maxlength));
  120. $vocabulary = $this->vocabularyStorage->create(array(
  121. 'name' => $name,
  122. 'vid' => Unicode::strtolower($name),
  123. 'langcode' => Language::LANGCODE_NOT_SPECIFIED,
  124. 'description' => "Description of $name",
  125. 'hierarchy' => 1,
  126. 'weight' => mt_rand(0, 10),
  127. 'multiple' => 1,
  128. 'required' => 0,
  129. 'relations' => 1,
  130. ));
  131. // Populate all fields with sample values.
  132. $this->populateFields($vocabulary);
  133. $vocabulary->save();
  134. $vocabularies[] = $vocabulary->id();
  135. unset($vocabulary);
  136. }
  137. return $vocabularies;
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function validateDrushParams($args) {
  143. $values = array(
  144. 'num' => array_shift($args),
  145. 'kill' => drush_get_option('kill'),
  146. 'title_length' => 12,
  147. );
  148. if ($this->isNumber($values['num']) == FALSE) {
  149. return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid number of vocabularies: @num.', array('@num' => $values['num'])));
  150. }
  151. return $values;
  152. }
  153. }