class ExampleDevelGenerate

Same name and namespace in other branches
  1. 4.x devel_generate/tests/modules/devel_generate_example/src/Plugin/DevelGenerate/ExampleDevelGenerate.php \Drupal\devel_generate_example\Plugin\DevelGenerate\ExampleDevelGenerate

Provides a ExampleDevelGenerate plugin.

Plugin annotation


@DevelGenerate(
  id = "devel_generate_example",
  label = "Example",
  description = "Generate a given number of examples.",
  url = "devel_generate_example",
  permission = "administer devel_generate",
  settings = {
    "num" = 50,
    "kill" = FALSE
  }
)

Hierarchy

Expanded class hierarchy of ExampleDevelGenerate

1 file declares its use of ExampleDevelGenerate
DevelGenerateManagerTest.php in devel_generate/tests/src/Unit/DevelGenerateManagerTest.php

File

devel_generate/tests/modules/devel_generate_example/src/Plugin/DevelGenerate/ExampleDevelGenerate.php, line 26

Namespace

Drupal\devel_generate_example\Plugin\DevelGenerate
View source
class ExampleDevelGenerate extends DevelGenerateBase implements ContainerFactoryPluginInterface {
  
  /**
   * Provides system time.
   */
  protected TimeInterface $time;
  
  /**
   * {@inheritdoc}
   */
  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;
  }
  
  /**
   * {@inheritdoc}
   */
  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;
  }
  
  /**
   * {@inheritdoc}
   */
  protected function generateElements(array $values) : void {
    $num = $values['num'];
    $kill = $values['kill'];
    if ($kill) {
      $this->setMessage($this->t('Old examples have been deleted.'));
    }
    // Creating user in order to demonstrate
    // how to override default business login generation.
    $edit = [
      'uid' => NULL,
      'name' => 'example_devel_generate',
      'pass' => '',
      'mail' => 'example_devel_generate@example.com',
      'status' => 1,
      'created' => $this->time
        ->getRequestTime(),
      'roles' => '',
      // A flag to let hook_user_* know that this is a generated user.
'devel_generate' => TRUE,
    ];
    $account = user_load_by_name('example_devel_generate');
    if (!$account) {
      $account = $this->entityTypeManager
        ->getStorage('user')
        ->create($edit);
    }
    // Populate all fields with sample values.
    $this->populateFields($account);
    $account->save();
    $this->setMessage($this->t('@num_examples created.', [
      '@num_examples' => $this->formatPlural($num, '1 example', '@count examples'),
    ]));
  }
  
  /**
   * {@inheritdoc}
   */
  public function validateDrushParams(array $args, array $options = []) : array {
    return [
      'num' => $options['num'],
      'kill' => $options['kill'],
    ];
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function #[\ReturnTypeWillChange] 2
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
ExampleDevelGenerate::$time protected property Provides system time.
ExampleDevelGenerate::create public static function Instantiates a new instance of this class. Overrides DevelGenerateBase::create
ExampleDevelGenerate::generateElements protected function Business logic relating with each DevelGenerate plugin. Overrides DevelGenerateBase::generateElements
ExampleDevelGenerate::settingsForm public function Returns the form for the plugin. Overrides DevelGenerateBase::settingsForm
ExampleDevelGenerate::validateDrushParams public function Responsible for validating Drush params. Overrides DevelGenerateBaseInterface::validateDrushParams
MessengerTrait::$messenger protected property The messenger. 25
MessengerTrait::messenger public function Gets the messenger. 25
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin ID.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin ID of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 87
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.