class VocabularyDevelGenerate
Same name in other branches
- 4.x devel_generate/src/Plugin/DevelGenerate/VocabularyDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\VocabularyDevelGenerate
Provides a VocabularyDevelGenerate plugin.
Plugin annotation
@DevelGenerate(
id = "vocabulary",
label = @Translation("vocabularies"),
description = @Translation("Generate a given number of vocabularies. Optionally delete current vocabularies."),
url = "vocabs",
permission = "administer devel_generate",
settings = {
"num" = 1,
"title_length" = 12,
"kill" = FALSE
},
dependencies = {
"taxonomy",
},
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
- class \Drupal\Core\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait
- class \Drupal\devel_generate\DevelGenerateBase extends \Drupal\Core\Plugin\PluginBase implements \Drupal\devel_generate\DevelGenerateBaseInterface
- class \Drupal\devel_generate\Plugin\DevelGenerate\VocabularyDevelGenerate extends \Drupal\devel_generate\DevelGenerateBase implements \Drupal\Core\Plugin\ContainerFactoryPluginInterface
- class \Drupal\devel_generate\DevelGenerateBase extends \Drupal\Core\Plugin\PluginBase implements \Drupal\devel_generate\DevelGenerateBaseInterface
- class \Drupal\Core\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait
Expanded class hierarchy of VocabularyDevelGenerate
File
-
devel_generate/
src/ Plugin/ DevelGenerate/ VocabularyDevelGenerate.php, line 31
Namespace
Drupal\devel_generate\Plugin\DevelGenerateView source
class VocabularyDevelGenerate extends DevelGenerateBase implements ContainerFactoryPluginInterface {
/**
* The vocabulary storage.
*/
protected VocabularyStorageInterface $vocabularyStorage;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : static {
$entity_type_manager = $container->get('entity_type.manager');
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->vocabularyStorage = $entity_type_manager->getStorage('taxonomy_vocabulary');
return $instance;
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) : array {
$form['num'] = [
'#type' => 'number',
'#title' => $this->t('Number of vocabularies?'),
'#default_value' => $this->getSetting('num'),
'#required' => TRUE,
'#min' => 0,
];
$form['title_length'] = [
'#type' => 'number',
'#title' => $this->t('Maximum number of characters in vocabulary names'),
'#default_value' => $this->getSetting('title_length'),
'#required' => TRUE,
'#min' => 2,
'#max' => 255,
];
$form['kill'] = [
'#type' => 'checkbox',
'#title' => $this->t('Delete existing vocabularies before generating new ones.'),
'#default_value' => $this->getSetting('kill'),
];
return $form;
}
/**
* {@inheritdoc}
*/
protected function generateElements(array $values) : void {
if ($values['kill']) {
$this->deleteVocabularies();
$this->setMessage($this->t('Deleted existing vocabularies.'));
}
$new_vocs = $this->generateVocabularies($values['num'], $values['title_length']);
if ($new_vocs !== []) {
$this->setMessage($this->t('Created the following new vocabularies: @vocs', [
'@vocs' => implode(', ', $new_vocs),
]));
}
}
/**
* Deletes all vocabularies.
*/
protected function deleteVocabularies() : void {
$vocabularies = $this->vocabularyStorage
->loadMultiple();
$this->vocabularyStorage
->delete($vocabularies);
}
/**
* Generates vocabularies.
*
* @param int $records
* Number of vocabularies to create.
* @param int $maxlength
* (optional) Maximum length for vocabulary name.
*
* @return array
* Array containing the generated vocabularies id.
*/
protected function generateVocabularies(int $records, int $maxlength = 12) : array {
$vocabularies = [];
// Insert new data:
for ($i = 1; $i <= $records; ++$i) {
$name = $this->getRandom()
->word(mt_rand(2, $maxlength));
$vocabulary = $this->vocabularyStorage
->create([
'name' => $name,
'vid' => mb_strtolower($name),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'description' => 'Description of ' . $name,
'hierarchy' => 1,
'weight' => mt_rand(0, 10),
'multiple' => 1,
'required' => 0,
'relations' => 1,
]);
// Populate all fields with sample values.
$this->populateFields($vocabulary);
$vocabulary->save();
$vocabularies[] = $vocabulary->id();
unset($vocabulary);
}
return $vocabularies;
}
/**
* {@inheritdoc}
*/
public function validateDrushParams(array $args, array $options = []) : array {
$values = [
'num' => array_shift($args),
'kill' => $options['kill'],
'title_length' => 12,
];
if ($this->isNumber($values['num']) == FALSE) {
throw new \Exception(dt('Invalid number of vocabularies: @num.', [
'@num' => $values['num'],
]));
}
return $values;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
DevelGenerateBase::$entityFieldManager | protected | property | The entity field manager. | ||
DevelGenerateBase::$entityTypeManager | protected | property | The entity type manager service. | ||
DevelGenerateBase::$languageManager | protected | property | The language manager. | 1 | |
DevelGenerateBase::$moduleHandler | protected | property | The module handler. | 1 | |
DevelGenerateBase::$random | protected | property | The random data generator. | ||
DevelGenerateBase::$settings | protected | property | The plugin settings. | ||
DevelGenerateBase::csvToArray | public static | function | Convert a csv string into an array of items. | ||
DevelGenerateBase::generate | public | function | Execute the instructions in common for all DevelGenerate plugin. | Overrides DevelGenerateBaseInterface::generate | |
DevelGenerateBase::getDefaultSettings | public | function | Returns the default settings for the plugin. | Overrides DevelGenerateBaseInterface::getDefaultSettings | |
DevelGenerateBase::getLangcode | protected | function | Return a language code. | 1 | |
DevelGenerateBase::getLanguageForm | protected | function | Creates the language and translation section of the form. | ||
DevelGenerateBase::getRandom | protected | function | Returns the random data generator. | ||
DevelGenerateBase::getSetting | public | function | Returns the array of settings, including defaults for missing settings. | Overrides DevelGenerateBaseInterface::getSetting | |
DevelGenerateBase::getSettings | public | function | Returns the current settings for the plugin. | Overrides DevelGenerateBaseInterface::getSettings | |
DevelGenerateBase::handleDrushParams | public | function | |||
DevelGenerateBase::isNumber | public static | function | Check if a given param is a number. | ||
DevelGenerateBase::populateFields | public | function | Populate the fields on a given entity with sample values. | ||
DevelGenerateBase::randomSentenceOfLength | protected | function | Generates a random sentence of specific length. | ||
DevelGenerateBase::setMessage | protected | function | Set a message for either drush or the web interface. | ||
DevelGenerateBase::settingsFormValidate | public | function | Form validation handler. | Overrides DevelGenerateBaseInterface::settingsFormValidate | 3 |
DevelGenerateBaseInterface::__construct | public | function | |||
PluginInspectionInterface::getPluginDefinition | public | function | Gets the definition of the plugin implementation. | 6 | |
PluginInspectionInterface::getPluginId | public | function | Gets the plugin ID of the plugin instance. | 2 | |
VocabularyDevelGenerate::$vocabularyStorage | protected | property | The vocabulary storage. | ||
VocabularyDevelGenerate::create | public static | function | Instantiates a new instance of this class. | Overrides DevelGenerateBase::create | |
VocabularyDevelGenerate::deleteVocabularies | protected | function | Deletes all vocabularies. | ||
VocabularyDevelGenerate::generateElements | protected | function | Business logic relating with each DevelGenerate plugin. | Overrides DevelGenerateBase::generateElements | |
VocabularyDevelGenerate::generateVocabularies | protected | function | Generates vocabularies. | ||
VocabularyDevelGenerate::settingsForm | public | function | Returns the form for the plugin. | Overrides DevelGenerateBase::settingsForm | |
VocabularyDevelGenerate::validateDrushParams | public | function | Responsible for validating Drush params. | Overrides DevelGenerateBaseInterface::validateDrushParams |