Namespace
Drupal\devel_generate_example\Plugin\DevelGenerate
File
-
devel_generate/tests/modules/devel_generate_example/src/Plugin/DevelGenerate/ExampleDevelGenerate.php
View source
<?php
namespace Drupal\devel_generate_example\Plugin\DevelGenerate;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\devel_generate\DevelGenerateBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ExampleDevelGenerate extends DevelGenerateBase implements ContainerFactoryPluginInterface {
protected TimeInterface $time;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : static {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->time = $container->get('datetime.time');
return $instance;
}
public function settingsForm(array $form, FormStateInterface $form_state) : array {
$form['num'] = [
'#type' => 'textfield',
'#title' => $this->t('How many examples would you like to generate?'),
'#default_value' => $this->getSetting('num'),
'#size' => 10,
];
$form['kill'] = [
'#type' => 'checkbox',
'#title' => $this->t('Delete all examples before generating new examples.'),
'#default_value' => $this->getSetting('kill'),
];
return $form;
}
protected function generateElements(array $values) : void {
$num = $values['num'];
$kill = $values['kill'];
if ($kill) {
$this->setMessage($this->t('Old examples have been deleted.'));
}
$edit = [
'uid' => NULL,
'name' => 'example_devel_generate',
'pass' => '',
'mail' => 'example_devel_generate@example.com',
'status' => 1,
'created' => $this->time
->getRequestTime(),
'roles' => '',
'devel_generate' => TRUE,
];
$account = user_load_by_name('example_devel_generate');
if (!$account) {
$account = $this->entityTypeManager
->getStorage('user')
->create($edit);
}
$this->populateFields($account);
$account->save();
$this->setMessage($this->t('@num_examples created.', [
'@num_examples' => $this->formatPlural($num, '1 example', '@count examples'),
]));
}
public function validateDrushParams(array $args, array $options = []) : array {
return [
'num' => $options['num'],
'kill' => $options['kill'],
];
}
}
Classes