class TermDevelGenerate in Devel 8
Same name and namespace in other branches
- 8.3 devel_generate/src/Plugin/DevelGenerate/TermDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\TermDevelGenerate
- 8.2 devel_generate/src/Plugin/DevelGenerate/TermDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\TermDevelGenerate
- 4.x devel_generate/src/Plugin/DevelGenerate/TermDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\TermDevelGenerate
Provides a TermDevelGenerate plugin.
Plugin annotation
@DevelGenerate(
id = "term",
label = @Translation("terms"),
description = @Translation("Generate a given number of terms. Optionally delete current terms."),
url = "term",
permission = "administer devel_generate",
settings = {
"num" = 10,
"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\TermDevelGenerate implements ContainerFactoryPluginInterface
- class \Drupal\devel_generate\DevelGenerateBase implements DevelGenerateBaseInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of TermDevelGenerate
File
- devel_generate/
src/ Plugin/ DevelGenerate/ TermDevelGenerate.php, line 28
Namespace
Drupal\devel_generate\Plugin\DevelGenerateView source
class TermDevelGenerate extends DevelGenerateBase implements ContainerFactoryPluginInterface {
/**
* The vocabulary storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $vocabularyStorage;
/**
* The term storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $termStorage;
/**
* Constructs a new TermDevelGenerate 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 $vocabulary_storage
* The vocabulary storage.
* @param \Drupal\Core\Entity\EntityStorageInterface $term_storage
* The term storage.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $vocabulary_storage, EntityStorageInterface $term_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->vocabularyStorage = $vocabulary_storage;
$this->termStorage = $term_storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$entity_manager = $container
->get('entity.manager');
return new static($configuration, $plugin_id, $plugin_definition, $entity_manager
->getStorage('taxonomy_vocabulary'), $entity_manager
->getStorage('taxonomy_term'));
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$options = array();
foreach ($this->vocabularyStorage
->loadMultiple() as $vocabulary) {
$options[$vocabulary
->id()] = $vocabulary
->label();
}
$form['vids'] = array(
'#type' => 'select',
'#multiple' => TRUE,
'#title' => $this
->t('Vocabularies'),
'#required' => TRUE,
'#default_value' => 'tags',
'#options' => $options,
'#description' => $this
->t('Restrict terms to these vocabularies.'),
);
$form['num'] = array(
'#type' => 'number',
'#title' => $this
->t('Number of terms?'),
'#default_value' => $this
->getSetting('num'),
'#required' => TRUE,
'#min' => 0,
);
$form['title_length'] = array(
'#type' => 'number',
'#title' => $this
->t('Maximum number of characters in term names'),
'#default_value' => $this
->getSetting('title_length'),
'#required' => TRUE,
'#min' => 2,
'#max' => 255,
);
$form['kill'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Delete existing terms in specified vocabularies before generating new terms.'),
'#default_value' => $this
->getSetting('kill'),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function generateElements(array $values) {
if ($values['kill']) {
$this
->deleteVocabularyTerms($values['vids']);
$this
->setMessage($this
->t('Deleted existing terms.'));
}
$vocabs = $this->vocabularyStorage
->loadMultiple($values['vids']);
$new_terms = $this
->generateTerms($values['num'], $vocabs, $values['title_length']);
if (!empty($new_terms)) {
$this
->setMessage($this
->t('Created the following new terms: @terms', array(
'@terms' => implode(', ', $new_terms),
)));
}
}
/**
* Deletes all terms of given vocabularies.
*
* @param array $vids
* Array of vocabulary vid.
*/
protected function deleteVocabularyTerms($vids) {
$tids = $this->vocabularyStorage
->getToplevelTids($vids);
$terms = $this->termStorage
->loadMultiple($tids);
$this->termStorage
->delete($terms);
}
/**
* Generates taxonomy terms for a list of given vocabularies.
*
* @param int $records
* Number of terms to create in total.
* @param \Drupal\taxonomy\TermInterface[] $vocabs
* List of vocabularies to populate.
* @param int $maxlength
* (optional) Maximum length per term.
*
* @return array
* The list of names of the created terms.
*/
protected function generateTerms($records, $vocabs, $maxlength = 12) {
$terms = array();
// Insert new data:
$max = db_query('SELECT MAX(tid) FROM {taxonomy_term_data}')
->fetchField();
$start = time();
for ($i = 1; $i <= $records; $i++) {
$name = $this
->getRandom()
->word(mt_rand(2, $maxlength));
$values = array(
'name' => $name,
'description' => 'description of ' . $name,
'format' => filter_fallback_format(),
'weight' => mt_rand(0, 10),
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
);
switch ($i % 2) {
case 1:
$vocab = $vocabs[array_rand($vocabs)];
$values['vid'] = $vocab
->id();
$values['parent'] = array(
0,
);
break;
default:
while (TRUE) {
// Keep trying to find a random parent.
$candidate = mt_rand(1, $max);
$query = db_select('taxonomy_term_data', 't');
$parent = $query
->fields('t', array(
'tid',
'vid',
))
->condition('t.vid', array_keys($vocabs), 'IN')
->condition('t.tid', $candidate, '>=')
->range(0, 1)
->execute()
->fetchAssoc();
if ($parent['tid']) {
break;
}
}
$values['parent'] = array(
$parent['tid'],
);
// Slight speedup due to this property being set.
$values['vid'] = $parent['vid'];
break;
}
$term = $this->termStorage
->create($values);
// Populate all fields with sample values.
$this
->populateFields($term);
$term
->save();
$max++;
// Limit memory usage. Only report first 20 created terms.
if ($i < 20) {
$terms[] = $term
->label();
}
unset($term);
}
return $terms;
}
/**
* {@inheritdoc}
*/
public function validateDrushParams($args, $options = []) {
$vocabulary_name = array_shift($args);
$number = array_shift($args);
if ($number === NULL) {
$number = 10;
}
if (!$vocabulary_name) {
throw new \Exception(dt('Please provide a vocabulary machine name.'));
}
if (!$this
->isNumber($number)) {
throw new \Exception(dt('Invalid number of terms: @num', array(
'@num' => $number,
)));
}
// Try to convert machine name to a vocabulary id.
if (!($vocabulary = $this->vocabularyStorage
->load($vocabulary_name))) {
throw new \Exception(dt('Invalid vocabulary name: @name', array(
'@name' => $vocabulary_name,
)));
}
$values = [
'num' => $number,
'kill' => $this
->isDrush8() ? drush_get_option('kill') : $options['kill'],
'title_length' => 12,
'vids' => [
$vocabulary
->id(),
],
];
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 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 | 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 | ||
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 | Set a message for either drush or the web interface. | |
DevelGenerateBase:: |
function |
Form validation handler. Overrides DevelGenerateBaseInterface:: |
1 | |
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. | |
TermDevelGenerate:: |
protected | property | The term storage. | |
TermDevelGenerate:: |
protected | property | The vocabulary storage. | |
TermDevelGenerate:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
TermDevelGenerate:: |
protected | function | Deletes all terms of given vocabularies. | |
TermDevelGenerate:: |
public | function |
Business logic relating with each DevelGenerate plugin Overrides DevelGenerateBase:: |
|
TermDevelGenerate:: |
protected | function | Generates taxonomy terms for a list of given vocabularies. | |
TermDevelGenerate:: |
public | function |
Returns the form for the plugin. Overrides DevelGenerateBase:: |
|
TermDevelGenerate:: |
public | function |
Responsible for validating Drush params. Overrides DevelGenerateBaseInterface:: |
|
TermDevelGenerate:: |
public | function |
Constructs a new TermDevelGenerate object. Overrides PluginBase:: |