3 namespace Drupal\devel_generate;
5 use Drupal\Component\Utility\Random;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Field\FieldStorageDefinitionInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Plugin\PluginBase;
12 * Provides a base DevelGenerate plugin implementation.
14 abstract class DevelGenerateBase extends PluginBase implements DevelGenerateBaseInterface {
17 * The plugin settings.
21 protected $settings = array();
24 * The random data generator.
26 * @var \Drupal\Component\Utility\Random
33 public function getSetting($key) {
34 // Merge defaults if we have no value for the key.
35 if (!array_key_exists($key, $this->settings)) {
36 $this->settings = $this->getDefaultSettings();
38 return isset($this->settings[$key]) ? $this->settings[$key] : NULL;
44 public function getDefaultSettings() {
45 $definition = $this->getPluginDefinition();
46 return $definition['settings'];
52 public function getSettings() {
53 return $this->settings;
59 public function settingsForm(array $form, FormStateInterface $form_state) {
66 function settingsFormValidate(array $form, FormStateInterface $form_state) {
67 // Validation is optional.
73 public function generate(array $values) {
74 $this->generateElements($values);
75 $this->setMessage('Generate process complete.');
79 * Business logic relating with each DevelGenerate plugin
81 * @param array $values
82 * The input values from the settings form.
84 protected function generateElements(array $values) {
89 * Populate the fields on a given entity with sample values.
91 * @param \Drupal\Core\Entity\EntityInterface $entity
92 * The entity to be enriched with sample field values.
94 public static function populateFields(EntityInterface $entity) {
95 /** @var \Drupal\field\FieldConfigInterface[] $instances */
96 $instances = entity_load_multiple_by_properties('field_config', array('entity_type' => $entity->getEntityType()->id(), 'bundle' => $entity->bundle()));
98 if ($skips = function_exists('drush_get_option') ? drush_get_option('skip-fields', '') : @$_REQUEST['skip-fields']) {
99 foreach (explode(',', $skips) as $skip) {
100 unset($instances[$skip]);
104 foreach ($instances as $instance) {
105 $field_storage = $instance->getFieldStorageDefinition();
106 $max = $cardinality = $field_storage->getCardinality();
107 if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
108 // Just an arbitrary number for 'unlimited'
111 $field_name = $field_storage->getName();
112 $entity->$field_name->generateSampleItems($max);
119 public function handleDrushParams($args) {
124 * Set a message for either drush or the web interface.
127 * The message to display.
128 * @param string $type
129 * (optional) The message type, as defined by drupal_set_message(). Defaults
132 protected function setMessage($msg, $type = 'status') {
133 $function = 'drupal_set_message';
134 if (function_exists('drush_log')) {
135 $function = 'drush_log';
136 $msg = strip_tags($msg);
138 $function($msg, $type);
142 * Check if a given param is a number.
144 * @param mixed $number
145 * The parameter to check.
148 * TRUE if the parameter is a number, FALSE otherwise.
150 public static function isNumber($number) {
151 if ($number == NULL) return FALSE;
152 if (!is_numeric($number)) return FALSE;
157 * Returns the random data generator.
159 * @return \Drupal\Component\Utility\Random
160 * The random data generator.
162 protected function getRandom() {
163 if (!$this->random) {
164 $this->random = new Random();
166 return $this->random;
169 protected function isDrush8() {
170 return function_exists('drush_drupal_load_autoloader');