class UserDevelGenerate in Devel 8
Same name and namespace in other branches
- 8.3 devel_generate/src/Plugin/DevelGenerate/UserDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\UserDevelGenerate
- 8.2 devel_generate/src/Plugin/DevelGenerate/UserDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\UserDevelGenerate
- 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 DerivativeInspectionInterface, PluginInspectionInterface- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait- class \Drupal\devel_generate\DevelGenerateBase implements DevelGenerateBaseInterface- class \Drupal\devel_generate\Plugin\DevelGenerate\UserDevelGenerate implements ContainerFactoryPluginInterface
 
 
- class \Drupal\devel_generate\DevelGenerateBase implements DevelGenerateBaseInterface
 
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of UserDevelGenerate
File
- devel_generate/src/ Plugin/ DevelGenerate/ UserDevelGenerate.php, line 29 
Namespace
Drupal\devel_generate\Plugin\DevelGenerateView source
class UserDevelGenerate extends DevelGenerateBase implements ContainerFactoryPluginInterface {
  /**
   * The user storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $userStorage;
  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;
  /**
   * Constructs a new UserDevelGenerate 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 user storage.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $entity_storage, DateFormatterInterface $date_formatter) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->userStorage = $entity_storage;
    $this->dateFormatter = $date_formatter;
  }
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity.manager')
      ->getStorage('user'), $container
      ->get('date.formatter'));
  }
  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $form['num'] = array(
      '#type' => 'number',
      '#title' => $this
        ->t('How many users would you like to generate?'),
      '#default_value' => $this
        ->getSetting('num'),
      '#required' => TRUE,
      '#min' => 0,
    );
    $form['kill'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Delete all users (except user id 1) before generating new users.'),
      '#default_value' => $this
        ->getSetting('kill'),
    );
    $options = user_role_names(TRUE);
    unset($options[DRUPAL_AUTHENTICATED_RID]);
    $form['roles'] = array(
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Which roles should the users receive?'),
      '#description' => $this
        ->t('Users always receive the <em>authenticated user</em> role.'),
      '#options' => $options,
    );
    $form['pass'] = array(
      '#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 = array(
      1 => $this
        ->t('Now'),
    );
    foreach (array(
      3600,
      86400,
      604800,
      2592000,
      31536000,
    ) as $interval) {
      $options[$interval] = $this->dateFormatter
        ->formatInterval($interval, 1) . ' ' . $this
        ->t('ago');
    }
    $form['time_range'] = array(
      '#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) {
    $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, '>')
        ->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 = array();
      while (count($names) < $num) {
        $name = $this
          ->getRandom()
          ->word(mt_rand(6, 12));
        $names[$name] = '';
      }
      if (empty($roles)) {
        $roles = array(
          DRUPAL_AUTHENTICATED_RID,
        );
      }
      foreach ($names as $name => $value) {
        $account = $this->userStorage
          ->create(array(
          'uid' => NULL,
          'name' => $name,
          'pass' => $pass,
          'mail' => $name . '@example.com',
          'status' => 1,
          'created' => REQUEST_TIME - 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.', array(
      '@num_users' => $this
        ->formatPlural($num, '1 user', '@count users'),
    )));
  }
  /**
   * {@inheritdoc}
   */
  public function validateDrushParams($args, $options = []) {
    $values = array(
      'num' => array_shift($args),
      'time_range' => 0,
    );
    if ($this
      ->isDrush8()) {
      $values += [
        'roles' => explode(',', drush_get_option('roles', '')),
        'kill' => drush_get_option('kill'),
        'pass' => drush_get_option('pass', NULL),
      ];
    }
    else {
      $values += [
        'roles' => StringUtils::csvToArray($options['roles']),
        'kill' => $options['kill'],
        'pass' => $options['pass'],
      ];
    }
    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. | |
| UserDevelGenerate:: | protected | property | The date formatter service. | |
| UserDevelGenerate:: | protected | property | The user storage. | |
| UserDevelGenerate:: | public static | function | Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: | |
| UserDevelGenerate:: | protected | function | Business logic relating with each DevelGenerate plugin Overrides DevelGenerateBase:: | |
| UserDevelGenerate:: | public | function | Returns the form for the plugin. Overrides DevelGenerateBase:: | |
| UserDevelGenerate:: | public | function | Responsible for validating Drush params. Overrides DevelGenerateBaseInterface:: | |
| UserDevelGenerate:: | public | function | Constructs a new UserDevelGenerate object. Overrides PluginBase:: | 
