DevelGenerateBase.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Drupal\devel_generate;
  3. use Drupal\Component\Utility\Random;
  4. use Drupal\Core\Entity\EntityInterface;
  5. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  6. use Drupal\Core\Form\FormStateInterface;
  7. use Drupal\Core\Plugin\PluginBase;
  8. /**
  9. * Provides a base DevelGenerate plugin implementation.
  10. */
  11. abstract class DevelGenerateBase extends PluginBase implements DevelGenerateBaseInterface {
  12. /**
  13. * The plugin settings.
  14. *
  15. * @var array
  16. */
  17. protected $settings = array();
  18. /**
  19. * The random data generator.
  20. *
  21. * @var \Drupal\Component\Utility\Random
  22. */
  23. protected $random;
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function getSetting($key) {
  28. // Merge defaults if we have no value for the key.
  29. if (!array_key_exists($key, $this->settings)) {
  30. $this->settings = $this->getDefaultSettings();
  31. }
  32. return isset($this->settings[$key]) ? $this->settings[$key] : NULL;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function getDefaultSettings() {
  38. $definition = $this->getPluginDefinition();
  39. return $definition['settings'];
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function getSettings() {
  45. return $this->settings;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function settingsForm(array $form, FormStateInterface $form_state) {
  51. return array();
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. function settingsFormValidate(array $form, FormStateInterface $form_state) {
  57. // Validation is optional.
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function generate(array $values) {
  63. $this->generateElements($values);
  64. $this->setMessage('Generate process complete.');
  65. }
  66. /**
  67. * Business logic relating with each DevelGenerate plugin
  68. *
  69. * @param array $values
  70. * The input values from the settings form.
  71. */
  72. protected function generateElements(array $values) {
  73. }
  74. /**
  75. * Populate the fields on a given entity with sample values.
  76. *
  77. * @param \Drupal\Core\Entity\EntityInterface $entity
  78. * The entity to be enriched with sample field values.
  79. */
  80. public static function populateFields(EntityInterface $entity) {
  81. /** @var \Drupal\field\FieldConfigInterface[] $instances */
  82. $instances = entity_load_multiple_by_properties('field_config', array('entity_type' => $entity->getEntityType()->id(), 'bundle' => $entity->bundle()));
  83. if ($skips = function_exists('drush_get_option') ? drush_get_option('skip-fields', '') : @$_REQUEST['skip-fields']) {
  84. foreach (explode(',', $skips) as $skip) {
  85. unset($instances[$skip]);
  86. }
  87. }
  88. foreach ($instances as $instance) {
  89. $field_storage = $instance->getFieldStorageDefinition();
  90. $max = $cardinality = $field_storage->getCardinality();
  91. if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
  92. // Just an arbitrary number for 'unlimited'
  93. $max = rand(1, 3);
  94. }
  95. $field_name = $field_storage->getName();
  96. $entity->$field_name->generateSampleItems($max);
  97. }
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function handleDrushParams($args) {
  103. }
  104. /**
  105. * Set a message for either drush or the web interface.
  106. *
  107. * @param string $msg
  108. * The message to display.
  109. * @param string $type
  110. * (optional) The message type, as defined by drupal_set_message(). Defaults
  111. * to 'status'
  112. */
  113. protected function setMessage($msg, $type = 'status') {
  114. $function = 'drupal_set_message';
  115. if (function_exists('drush_log')) {
  116. $function = 'drush_log';
  117. $msg = strip_tags($msg);
  118. }
  119. $function($msg, $type);
  120. }
  121. /**
  122. * Check if a given param is a number.
  123. *
  124. * @param mixed $number
  125. * The parameter to check.
  126. *
  127. * @return bool
  128. * TRUE if the parameter is a number, FALSE otherwise.
  129. */
  130. public static function isNumber($number) {
  131. if ($number == NULL) return FALSE;
  132. if (!is_numeric($number)) return FALSE;
  133. return TRUE;
  134. }
  135. /**
  136. * Returns the random data generator.
  137. *
  138. * @return \Drupal\Component\Utility\Random
  139. * The random data generator.
  140. */
  141. protected function getRandom() {
  142. if (!$this->random) {
  143. $this->random = new Random();
  144. }
  145. return $this->random;
  146. }
  147. }