class VocabularyDevelGenerate in Devel 8.3
Same name and namespace in other branches
- 8 devel_generate/src/Plugin/DevelGenerate/VocabularyDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\VocabularyDevelGenerate
- 8.2 devel_generate/src/Plugin/DevelGenerate/VocabularyDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\VocabularyDevelGenerate
- 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
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\devel_generate\DevelGenerateBase implements DevelGenerateBaseInterface
- class \Drupal\devel_generate\Plugin\DevelGenerate\VocabularyDevelGenerate implements ContainerFactoryPluginInterface
- class \Drupal\devel_generate\DevelGenerateBase implements DevelGenerateBaseInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of VocabularyDevelGenerate
File
- devel_generate/
src/ Plugin/ DevelGenerate/ VocabularyDevelGenerate.php, line 28
Namespace
Drupal\devel_generate\Plugin\DevelGenerateView source
class VocabularyDevelGenerate extends DevelGenerateBase implements ContainerFactoryPluginInterface {
/**
* The vocabulary storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $vocabularyStorage;
/**
* Constructs a new VocabularyDevelGenerate object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
* The vocabulary storage.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $entity_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->vocabularyStorage = $entity_storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager')
->getStorage('taxonomy_vocabulary'));
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$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}
*/
public function generateElements(array $values) {
if ($values['kill']) {
$this
->deleteVocabularies();
$this
->setMessage($this
->t('Deleted existing vocabularies.'));
}
$new_vocs = $this
->generateVocabularies($values['num'], $values['title_length']);
if (!empty($new_vocs)) {
$this
->setMessage($this
->t('Created the following new vocabularies: @vocs', [
'@vocs' => implode(', ', $new_vocs),
]));
}
}
/**
* Deletes all vocabularies.
*/
protected function deleteVocabularies() {
$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($records, $maxlength = 12) {
$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' => Language::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 = []) {
$values = [
'num' => array_shift($args),
'kill' => $this
->isDrush8() ? drush_get_option('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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
DevelGenerateBase:: |
protected | property | The entity type manager service. | |
DevelGenerateBase:: |
protected | property | The random data generator. | |
DevelGenerateBase:: |
protected | property | The plugin settings. | |
DevelGenerateBase:: |
public | function |
Execute the instructions in common for all DevelGenerate plugin. Overrides DevelGenerateBaseInterface:: |
|
DevelGenerateBase:: |
public | function |
Returns the default settings for the plugin. Overrides DevelGenerateBaseInterface:: |
|
DevelGenerateBase:: |
protected | function | Gets the entity type manager service. | |
DevelGenerateBase:: |
protected | function | Return a language code. | 1 |
DevelGenerateBase:: |
protected | function | Creates the language and translation section of the form. | |
DevelGenerateBase:: |
protected | function | Returns the random data generator. | |
DevelGenerateBase:: |
public | function |
Returns the array of settings, including defaults for missing settings. Overrides DevelGenerateBaseInterface:: |
|
DevelGenerateBase:: |
public | function |
Returns the current settings for the plugin. Overrides DevelGenerateBaseInterface:: |
|
DevelGenerateBase:: |
public | function | ||
DevelGenerateBase:: |
protected | function | Determines if Drush is version 8. | |
DevelGenerateBase:: |
public static | function | Check if a given param is a number. | |
DevelGenerateBase:: |
public static | function | Populate the fields on a given entity with sample values. | |
DevelGenerateBase:: |
protected | function | Generates a random sentence of specific length. | |
DevelGenerateBase:: |
protected | function | Set a message for either drush or the web interface. | |
DevelGenerateBase:: |
public | function |
Form validation handler. Overrides DevelGenerateBaseInterface:: |
2 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
VocabularyDevelGenerate:: |
protected | property | The vocabulary storage. | |
VocabularyDevelGenerate:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
VocabularyDevelGenerate:: |
protected | function | Deletes all vocabularies. | |
VocabularyDevelGenerate:: |
public | function |
Business logic relating with each DevelGenerate plugin. Overrides DevelGenerateBase:: |
|
VocabularyDevelGenerate:: |
protected | function | Generates vocabularies. | |
VocabularyDevelGenerate:: |
public | function |
Returns the form for the plugin. Overrides DevelGenerateBase:: |
|
VocabularyDevelGenerate:: |
public | function |
Responsible for validating Drush params. Overrides DevelGenerateBaseInterface:: |
|
VocabularyDevelGenerate:: |
public | function |
Constructs a new VocabularyDevelGenerate object. Overrides PluginBase:: |