abstract class DevelGenerateBase in Devel 8.3
Same name and namespace in other branches
- 8 devel_generate/src/DevelGenerateBase.php \Drupal\devel_generate\DevelGenerateBase
 - 8.2 devel_generate/src/DevelGenerateBase.php \Drupal\devel_generate\DevelGenerateBase
 - 4.x devel_generate/src/DevelGenerateBase.php \Drupal\devel_generate\DevelGenerateBase
 
Provides a base DevelGenerate plugin implementation.
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\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
 
Expanded class hierarchy of DevelGenerateBase
8 files declare their use of DevelGenerateBase
- ContentDevelGenerate.php in devel_generate/
src/ Plugin/ DevelGenerate/ ContentDevelGenerate.php  - devel_generate.module in devel_generate/
devel_generate.module  - ExampleDevelGenerate.php in devel_generate/
tests/ modules/ devel_generate_example/ src/ Plugin/ DevelGenerate/ ExampleDevelGenerate.php  - MediaDevelGenerate.php in devel_generate/
src/ Plugin/ DevelGenerate/ MediaDevelGenerate.php  - MenuDevelGenerate.php in devel_generate/
src/ Plugin/ DevelGenerate/ MenuDevelGenerate.php  
File
- devel_generate/
src/ DevelGenerateBase.php, line 16  
Namespace
Drupal\devel_generateView source
abstract class DevelGenerateBase extends PluginBase implements DevelGenerateBaseInterface {
  /**
   * The plugin settings.
   *
   * @var array
   */
  protected $settings = [];
  /**
   * The random data generator.
   *
   * @var \Drupal\Component\Utility\Random
   */
  protected $random;
  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;
  /**
   * {@inheritdoc}
   */
  public function getSetting($key) {
    // Merge defaults if we have no value for the key.
    if (!array_key_exists($key, $this->settings)) {
      $this->settings = $this
        ->getDefaultSettings();
    }
    return isset($this->settings[$key]) ? $this->settings[$key] : NULL;
  }
  /**
   * {@inheritdoc}
   */
  public function getDefaultSettings() {
    $definition = $this
      ->getPluginDefinition();
    return $definition['settings'];
  }
  /**
   * {@inheritdoc}
   */
  public function getSettings() {
    return $this->settings;
  }
  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    return [];
  }
  /**
   * {@inheritdoc}
   */
  public function settingsFormValidate(array $form, FormStateInterface $form_state) {
    // Validation is optional.
  }
  /**
   * {@inheritdoc}
   */
  public function generate(array $values) {
    $this
      ->generateElements($values);
    $this
      ->setMessage('Generate process complete.');
  }
  /**
   * Business logic relating with each DevelGenerate plugin.
   *
   * @param array $values
   *   The input values from the settings form.
   */
  protected function generateElements(array $values) {
  }
  /**
   * Populate the fields on a given entity with sample values.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity to be enriched with sample field values.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public static function populateFields(EntityInterface $entity) {
    $properties = [
      'entity_type' => $entity
        ->getEntityType()
        ->id(),
      'bundle' => $entity
        ->bundle(),
    ];
    $field_config_storage = \Drupal::entityTypeManager()
      ->getStorage('field_config');
    /* @var \Drupal\field\FieldConfigInterface[] $instances */
    $instances = $field_config_storage
      ->loadByProperties($properties);
    if ($skips = function_exists('drush_get_option') ? drush_get_option('skip-fields', '') : @$_REQUEST['skip-fields']) {
      foreach (explode(',', $skips) as $skip) {
        unset($instances[$skip]);
      }
    }
    foreach ($instances as $instance) {
      $field_storage = $instance
        ->getFieldStorageDefinition();
      $max = $cardinality = $field_storage
        ->getCardinality();
      if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
        // Just an arbitrary number for 'unlimited'.
        $max = rand(1, 3);
      }
      $field_name = $field_storage
        ->getName();
      $entity->{$field_name}
        ->generateSampleItems($max);
    }
  }
  /**
   * {@inheritdoc}
   */
  public function handleDrushParams($args) {
  }
  /**
   * Set a message for either drush or the web interface.
   *
   * @param string $msg
   *   The message to display.
   * @param string $type
   *   (optional) The message type, as defined in MessengerInterface. Defaults
   *   to MessengerInterface::TYPE_STATUS.
   */
  protected function setMessage($msg, $type = MessengerInterface::TYPE_STATUS) {
    if (function_exists('drush_log')) {
      $msg = strip_tags($msg);
      drush_log($msg);
    }
    else {
      \Drupal::messenger()
        ->addMessage($msg, $type);
    }
  }
  /**
   * Check if a given param is a number.
   *
   * @param mixed $number
   *   The parameter to check.
   *
   * @return bool
   *   TRUE if the parameter is a number, FALSE otherwise.
   */
  public static function isNumber($number) {
    if ($number == NULL) {
      return FALSE;
    }
    if (!is_numeric($number)) {
      return FALSE;
    }
    return TRUE;
  }
  /**
   * Gets the entity type manager service.
   *
   * @return \Drupal\Core\Entity\EntityTypeManagerInterface
   *   The entity type manager service.
   */
  protected function getEntityTypeManager() {
    if (!$this->entityTypeManager) {
      $this->entityTypeManager = \Drupal::entityTypeManager();
    }
    return $this->entityTypeManager;
  }
  /**
   * Returns the random data generator.
   *
   * @return \Drupal\Component\Utility\Random
   *   The random data generator.
   */
  protected function getRandom() {
    if (!$this->random) {
      $this->random = new Random();
    }
    return $this->random;
  }
  /**
   * Generates a random sentence of specific length.
   *
   * Words are randomly selected with length from 2 up to the optional parameter
   * $max_word_length. The first word is capitalised. No ending period is added.
   *
   * @param int $sentence_length
   *   The total length of the sentence, including the word-separating spaces.
   * @param int $max_word_length
   *   (optional) Maximum length of each word. Defaults to 8.
   *
   * @return string
   *   A sentence of the required length.
   */
  protected function randomSentenceOfLength($sentence_length, $max_word_length = 8) {
    // Maximum word length cannot be longer than the sentence length.
    $max_word_length = min($sentence_length, $max_word_length);
    $words = [];
    $remainder = $sentence_length;
    do {
      if ($remainder <= $max_word_length) {
        // If near enough to the end then generate the exact length word to fit.
        $next_word = $remainder;
      }
      else {
        // Cannot fill the remaining space with one word, so choose a random
        // length, short enough for a following word of at least minimum length.
        $next_word = mt_rand(2, min($max_word_length, $remainder - 3));
      }
      $words[] = $this
        ->getRandom()
        ->word($next_word);
      $remainder = $remainder - $next_word - 1;
    } while ($remainder > 0);
    $sentence = ucfirst(implode(' ', $words));
    return $sentence;
  }
  /**
   * Determines if Drush is version 8.
   *
   * Later versions do not have the 'drush_drupal_load_autoloader' function so
   * will return FALSE for 9 and higher.
   */
  protected function isDrush8() {
    return function_exists('drush_drupal_load_autoloader');
  }
  /**
   * Creates the language and translation section of the form.
   *
   * This is used by both Content and Term generation.
   *
   * @param string $items
   *   The name of the things that are being generated - 'nodes' or 'terms'.
   *
   * @return array
   *   The language details section of the form.
   */
  protected function getLanguageForm($items) {
    // We always need a language, even if the language module is not installed.
    $options = [];
    $languages = $this->languageManager
      ->getLanguages(LanguageInterface::STATE_CONFIGURABLE);
    foreach ($languages as $langcode => $language) {
      $options[$langcode] = $language
        ->getName();
    }
    $language_module_exists = $this->moduleHandler
      ->moduleExists('language');
    $translation_module_exists = $this->moduleHandler
      ->moduleExists('content_translation');
    $form['language'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Language'),
      '#open' => $language_module_exists,
    ];
    $form['language']['add_language'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Select the primary language(s) for @items', [
        '@items' => $items,
      ]),
      '#multiple' => TRUE,
      '#description' => $language_module_exists ? '' : $this
        ->t('Disabled - requires Language module'),
      '#options' => $options,
      '#default_value' => [
        $this->languageManager
          ->getDefaultLanguage()
          ->getId(),
      ],
      '#disabled' => !$language_module_exists,
    ];
    $form['language']['translate_language'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Select the language(s) for translated @items', [
        '@items' => $items,
      ]),
      '#multiple' => TRUE,
      '#description' => $translation_module_exists ? $this
        ->t('Translated @items will be created for each language selected.', [
        '@items' => $items,
      ]) : $this
        ->t('Disabled - requires Content Translation module.'),
      '#options' => $options,
      '#disabled' => !$translation_module_exists,
    ];
    return $form;
  }
  /**
   * Return a language code.
   *
   * @param array $add_language
   *   Optional array of language codes from which to select one at random.
   *   If empty then return the site's default language.
   *
   * @return string
   *   The language code to use.
   */
  protected function getLangcode(array $add_language) {
    if (empty($add_language)) {
      return $this->languageManager
        ->getDefaultLanguage()
        ->getId();
    }
    return $add_language[array_rand($add_language)];
  }
}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 entity type manager service. | |
| 
            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:: | 
                  protected | function | Business logic relating with each DevelGenerate plugin. | 7 | 
| 
            DevelGenerateBase:: | 
                  public | function | 
            Returns the default settings for the plugin. Overrides DevelGenerateBaseInterface:: | 
                  |
| 
            DevelGenerateBase:: | 
                  protected | function | Gets the entity type manager service. | |
| 
            DevelGenerateBase:: | 
                  protected | function | Return a language code. | 1 | 
| 
            DevelGenerateBase:: | 
                  protected | function | Creates the language and translation section of the form. | |
| 
            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 | Determines if Drush is version 8. | |
| 
            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 | Generates a random sentence of specific length. | |
| 
            DevelGenerateBase:: | 
                  protected | function | Set a message for either drush or the web interface. | |
| 
            DevelGenerateBase:: | 
                  public | function | 
            Returns the form for the plugin. Overrides DevelGenerateBaseInterface:: | 
                  7 | 
| 
            DevelGenerateBase:: | 
                  public | function | 
            Form validation handler. Overrides DevelGenerateBaseInterface:: | 
                  2 | 
| 
            DevelGenerateBaseInterface:: | 
                  public | function | Responsible for validating Drush params. | 7 | 
| 
            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. | |
| 
            PluginBase:: | 
                  public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 | 
| 
            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. |