class UserDevelGenerate
Same name in other branches
- 4.x devel_generate/src/Plugin/DevelGenerate/UserDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\UserDevelGenerate
Provides a UserDevelGenerate plugin.
Plugin annotation
@DevelGenerate(
id = "user",
label = @Translation("users"),
description = @Translation("Generate a given number of users. Optionally delete current users."),
url = "user",
permission = "administer devel_generate",
settings = {
"num" = 50,
"kill" = FALSE,
"pass" = ""
}
)
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\UserDevelGenerate 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 UserDevelGenerate
File
-
devel_generate/
src/ Plugin/ DevelGenerate/ UserDevelGenerate.php, line 31
Namespace
Drupal\devel_generate\Plugin\DevelGenerateView source
class UserDevelGenerate extends DevelGenerateBase implements ContainerFactoryPluginInterface {
/**
* The user storage.
*/
protected UserStorageInterface $userStorage;
/**
* The date formatter service.
*/
protected DateFormatterInterface $dateFormatter;
/**
* Provides system time.
*/
protected TimeInterface $time;
/**
* The role storage.
*/
protected RoleStorageInterface $roleStorage;
/**
* {@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->userStorage = $entity_type_manager->getStorage('user');
$instance->dateFormatter = $container->get('date.formatter');
$instance->time = $container->get('datetime.time');
$instance->roleStorage = $entity_type_manager->getStorage('user_role');
return $instance;
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) : array {
$form['num'] = [
'#type' => 'number',
'#title' => $this->t('How many users would you like to generate?'),
'#default_value' => $this->getSetting('num'),
'#required' => TRUE,
'#min' => 0,
];
$form['kill'] = [
'#type' => 'checkbox',
'#title' => $this->t('Delete all users (except user id 1) before generating new users.'),
'#default_value' => $this->getSetting('kill'),
];
$roles = array_map(static fn($role): string => $role->label(), $this->roleStorage
->loadMultiple());
unset($roles[AccountInterface::AUTHENTICATED_ROLE], $roles[AccountInterface::ANONYMOUS_ROLE]);
$form['roles'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Which roles should the users receive?'),
'#description' => $this->t('Users always receive the <em>authenticated user</em> role.'),
'#options' => $roles,
];
$form['pass'] = [
'#type' => 'textfield',
'#title' => $this->t('Password to be set'),
'#default_value' => $this->getSetting('pass'),
'#size' => 32,
'#description' => $this->t('Leave this field empty if you do not need to set a password'),
];
$options = [
1 => $this->t('Now'),
];
foreach ([
3600,
86400,
604800,
2592000,
31536000,
] as $interval) {
$options[$interval] = $this->dateFormatter
->formatInterval($interval, 1) . ' ' . $this->t('ago');
}
$form['time_range'] = [
'#type' => 'select',
'#title' => $this->t('How old should user accounts be?'),
'#description' => $this->t('User ages will be distributed randomly from the current time, back to the selected time.'),
'#options' => $options,
'#default_value' => 604800,
];
return $form;
}
/**
* {@inheritdoc}
*/
protected function generateElements(array $values) : void {
$num = $values['num'];
$kill = $values['kill'];
$pass = $values['pass'];
$age = $values['time_range'];
$roles = array_filter($values['roles']);
if ($kill) {
$uids = $this->userStorage
->getQuery()
->condition('uid', 1, '>')
->accessCheck(FALSE)
->execute();
$users = $this->userStorage
->loadMultiple($uids);
$this->userStorage
->delete($users);
$this->setMessage($this->formatPlural(count($uids), '1 user deleted', '@count users deleted.'));
}
if ($num > 0) {
$names = [];
while (count($names) < $num) {
$name = $this->getRandom()
->word(mt_rand(6, 12));
$names[$name] = '';
}
if ($roles === []) {
$roles = [
AccountInterface::AUTHENTICATED_ROLE,
];
}
foreach (array_keys($names) as $name) {
/** @var \Drupal\user\UserInterface $account */
$account = $this->userStorage
->create([
'uid' => NULL,
'name' => $name,
'pass' => $pass,
'mail' => $name . '@example.com',
'status' => 1,
'created' => $this->time
->getRequestTime() - mt_rand(0, $age),
'roles' => array_values($roles),
// A flag to let hook_user_* know that this is a generated user.
'devel_generate' => TRUE,
]);
// Populate all fields with sample values.
$this->populateFields($account);
$account->save();
}
}
$this->setMessage($this->t('@num_users created.', [
'@num_users' => $this->formatPlural($num, '1 user', '@count users'),
]));
}
/**
* {@inheritdoc}
*/
public function validateDrushParams(array $args, array $options = []) : array {
return [
'num' => array_shift($args),
'time_range' => 0,
'roles' => self::csvToArray($options['roles']),
'kill' => $options['kill'],
'pass' => $options['pass'],
];
}
}
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 | |
UserDevelGenerate::$dateFormatter | protected | property | The date formatter service. | ||
UserDevelGenerate::$roleStorage | protected | property | The role storage. | ||
UserDevelGenerate::$time | protected | property | Provides system time. | ||
UserDevelGenerate::$userStorage | protected | property | The user storage. | ||
UserDevelGenerate::create | public static | function | Instantiates a new instance of this class. | Overrides DevelGenerateBase::create | |
UserDevelGenerate::generateElements | protected | function | Business logic relating with each DevelGenerate plugin. | Overrides DevelGenerateBase::generateElements | |
UserDevelGenerate::settingsForm | public | function | Returns the form for the plugin. | Overrides DevelGenerateBase::settingsForm | |
UserDevelGenerate::validateDrushParams | public | function | Responsible for validating Drush params. | Overrides DevelGenerateBaseInterface::validateDrushParams |