1d38a352089398d21468b8814e5d6f8ea8d942ea
[yaffs-website] / web / modules / contrib / devel / devel_generate / src / DevelGenerateBase.php
1 <?php
2
3 namespace Drupal\devel_generate;
4
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;
10
11 /**
12  * Provides a base DevelGenerate plugin implementation.
13  */
14 abstract class DevelGenerateBase extends PluginBase implements DevelGenerateBaseInterface {
15
16   /**
17    * The plugin settings.
18    *
19    * @var array
20    */
21   protected $settings = array();
22
23   /**
24    * The random data generator.
25    *
26    * @var \Drupal\Component\Utility\Random
27    */
28   protected $random;
29
30   /**
31    * {@inheritdoc}
32    */
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();
37     }
38     return isset($this->settings[$key]) ? $this->settings[$key] : NULL;
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function getDefaultSettings() {
45     $definition = $this->getPluginDefinition();
46     return $definition['settings'];
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function getSettings() {
53     return $this->settings;
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function settingsForm(array $form, FormStateInterface $form_state) {
60     return array();
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   function settingsFormValidate(array $form, FormStateInterface $form_state) {
67     // Validation is optional.
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function generate(array $values) {
74     $this->generateElements($values);
75     $this->setMessage('Generate process complete.');
76   }
77
78   /**
79    * Business logic relating with each DevelGenerate plugin
80    *
81    * @param array $values
82    *   The input values from the settings form.
83    */
84   protected function generateElements(array $values) {
85
86   }
87
88   /**
89    * Populate the fields on a given entity with sample values.
90    *
91    * @param \Drupal\Core\Entity\EntityInterface $entity
92    *   The entity to be enriched with sample field values.
93    */
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()));
97
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]);
101       }
102     }
103
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'
109         $max = rand(1, 3);
110       }
111       $field_name = $field_storage->getName();
112       $entity->$field_name->generateSampleItems($max);
113     }
114   }
115
116   /**
117    * {@inheritdoc}
118    */
119   public function handleDrushParams($args) {
120
121   }
122
123   /**
124    * Set a message for either drush or the web interface.
125    *
126    * @param string $msg
127    *   The message to display.
128    * @param string $type
129    *   (optional) The message type, as defined by drupal_set_message(). Defaults
130    *   to 'status'
131    */
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);
137     }
138     $function($msg, $type);
139   }
140
141   /**
142    * Check if a given param is a number.
143    *
144    * @param mixed $number
145    *   The parameter to check.
146    *
147    * @return bool
148    *   TRUE if the parameter is a number, FALSE otherwise.
149    */
150   public static function isNumber($number) {
151     if ($number == NULL) return FALSE;
152     if (!is_numeric($number)) return FALSE;
153     return TRUE;
154   }
155
156   /**
157    * Returns the random data generator.
158    *
159    * @return \Drupal\Component\Utility\Random
160    *   The random data generator.
161    */
162   protected function getRandom() {
163     if (!$this->random) {
164       $this->random = new Random();
165     }
166     return $this->random;
167   }
168
169   protected function isDrush8() {
170     return function_exists('drush_drupal_load_autoloader');
171   }
172 }