diff --git a/core/lib/Drupal/Component/Utility/Random.php b/core/lib/Drupal/Component/Utility/Random.php index 1a938b6..dc31e85 100644 --- a/core/lib/Drupal/Component/Utility/Random.php +++ b/core/lib/Drupal/Component/Utility/Random.php @@ -168,7 +168,7 @@ public function object($size = 4) { * @return string * Nonsense latin words which form sentence(s). */ - public function createSentences($min_word_count, $capitalize = FALSE) { + public function sentences($min_word_count, $capitalize = FALSE) { $dictionary = array("abbas", "abdo", "abico", "abigo", "abluo", "accumsan", "acsi", "ad", "adipiscing", "aliquam", "aliquip", "amet", "antehabeo", "appellatio", "aptent", "at", "augue", "autem", "bene", "blandit", @@ -229,7 +229,7 @@ public function createSentences($min_word_count, $capitalize = FALSE) { * * @return string */ - public function generateWord($length) { + public function word($length) { mt_srand((double) microtime()*1000000); $vowels = array("a", "e", "i", "o", "u"); @@ -253,10 +253,10 @@ public function generateWord($length) { * @param int $paragraph_count * @return string */ - public function generateParagraphs($paragraph_count = 12) { + public function paragraphs($paragraph_count = 12) { $output = ''; for ($i = 1; $i <= $paragraph_count; $i++) { - $output .= $this->createSentences(mt_rand(20, 60)) ."\n\n"; + $output .= $this->sentences(mt_rand(20, 60)) ."\n\n"; } return $output; } @@ -273,7 +273,7 @@ public function generateParagraphs($paragraph_count = 12) { * @return string * Path to image file. */ - public function generateImage($destination, $min_resolution, $max_resolution) { + public function image($destination, $min_resolution, $max_resolution) { $extension = pathinfo($destination, PATHINFO_EXTENSION); $min = explode('x', $min_resolution); $max = explode('x', $max_resolution); diff --git a/core/lib/Drupal/Core/Field/FieldItemBase.php b/core/lib/Drupal/Core/Field/FieldItemBase.php index 9dd31bf..ee518e6 100644 --- a/core/lib/Drupal/Core/Field/FieldItemBase.php +++ b/core/lib/Drupal/Core/Field/FieldItemBase.php @@ -240,7 +240,7 @@ public function delete() { } /** * {@inheritdoc} */ - public function generateSampleValue() { } + public static function generateSampleValue(FieldDefinitionInterface $field_definition) { } /** * {@inheritdoc} diff --git a/core/lib/Drupal/Core/Field/FieldItemInterface.php b/core/lib/Drupal/Core/Field/FieldItemInterface.php index 7a888d0..14993cb 100644 --- a/core/lib/Drupal/Core/Field/FieldItemInterface.php +++ b/core/lib/Drupal/Core/Field/FieldItemInterface.php @@ -208,17 +208,6 @@ public function update(); public function delete(); /** - * Generates placeholder, valid field values. - * - * Useful when populating site with placeholder content during site building - * or profiling. - * - * @return array - * An associative array of values. - */ - public function generateSampleValue(); - - /** * Defines custom revision delete behavior for field values. * * This method is called from during the process of deleting an entity @@ -228,6 +217,20 @@ public function generateSampleValue(); public function deleteRevision(); /** + * Generates placeholder field values. + * + * Useful when populating site with placeholder content during site building + * or profiling. + * + * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition + * The field definition. + * + * @return array + * An associative array of values. + */ + public static function generateSampleValue(FieldDefinitionInterface $field_definition); + + /** * Defines the field-level settings for this plugin. * * @return array diff --git a/core/lib/Drupal/Core/Field/FieldItemList.php b/core/lib/Drupal/Core/Field/FieldItemList.php index 7f0890d..ccd1cb6 100644 --- a/core/lib/Drupal/Core/Field/FieldItemList.php +++ b/core/lib/Drupal/Core/Field/FieldItemList.php @@ -281,6 +281,18 @@ public function view($display_options = array()) { /** * {@inheritdoc} */ + public function generateSampleItems($count = 1) { + $field_definition = $this->getFieldDefinition(); + $field_type_class = \Drupal::service('plugin.manager.field.field_type')->getPluginClass($field_definition->getType()); + for ($delta=0; $delta < $count; $delta++) { + $values[$delta] = $field_type_class::generateSampleValue($field_definition); + } + $this->setValue($values); + } + + /** + * {@inheritdoc} + */ public function getConstraints() { $constraints = parent::getConstraints(); // Check that the number of values doesn't exceed the field cardinality. For diff --git a/core/lib/Drupal/Core/Field/FieldItemListInterface.php b/core/lib/Drupal/Core/Field/FieldItemListInterface.php index 402aa19..3ae486c 100644 --- a/core/lib/Drupal/Core/Field/FieldItemListInterface.php +++ b/core/lib/Drupal/Core/Field/FieldItemListInterface.php @@ -181,6 +181,14 @@ public function deleteRevision(); */ public function view($display_options = array()); + /* + * Populates a specified number of field items with valid sample data. + * + * @param int $count + * The number of items to create. + */ + public function generateSampleItems($count = 1); + /** * Returns a form for the default value input. * diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/BooleanItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/BooleanItem.php index fdb7627..a7444cd 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/BooleanItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/BooleanItem.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Field\Plugin\Field\FieldType; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemBase; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Form\FormStateInterface; @@ -116,10 +117,12 @@ public function getSettableOptions(AccountInterface $account = NULL) { /** * {@inheritdoc} */ - public function generateSampleValue() { + public static function generateSampleValue(FieldDefinitionInterface $field_definition) { + // Again, how to get rid of the call to $this below? if ($allowed = $this->getPossibleValues()) { $values['value'] = array_rand(array_flip($allowed)); return $values; } } } + diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DecimalItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DecimalItem.php index 2f544f0..f7d4766 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DecimalItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DecimalItem.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Field\Plugin\Field\FieldType; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\TypedData\DataDefinition; @@ -100,8 +101,8 @@ public function preSave() { /** * {@inheritdoc} */ - public function generateSampleValue() { - $settings = $this->getSettings(); + public static function generateSampleValue(FieldDefinitionInterface $field_definition) { + $settings = $field_definition->getSettings(); $precision = $settings['precision'] ?: 10; $scale = $settings['scale'] ?: 2; // $precision - $scale is the number of digits on the left of the decimal @@ -112,16 +113,16 @@ public function generateSampleValue() { $min = is_numeric($settings['min']) ?: -pow(10, ($precision - $scale)) + 1; // Get the number of decimal digits for the $max - $decimal_digits = $this->getDecimalDigits($max); + $decimal_digits = self::getDecimalDigits($max); // Do the same for the min and keep the higher number of decimal digits. - $decimal_digits = max($this->getDecimalDigits($min), $decimal_digits); + $decimal_digits = max(self::getDecimalDigits($min), $decimal_digits); // If $min = 1.234 and $max = 1.33 then $decimal_digits = 3 $scale = rand($decimal_digits, $scale); // @see "Example #1 Calculate a random floating-point number" in // http://php.net/manual/en/function.mt-getrandmax.php $random_decimal = $min + mt_rand() / mt_getrandmax() * ($max - $min); - $values['value'] = $this->truncateDecimal($random_decimal, $scale); + $values['value'] = self::truncateDecimal($random_decimal, $scale); return $values; } @@ -135,7 +136,7 @@ public function generateSampleValue() { * @return int * The number of decimal digits. */ - function getDecimalDigits($decimal) { + protected static function getDecimalDigits($decimal) { $digits = 0; while ($decimal - round($decimal)) { $decimal *= 10; diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EmailItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EmailItem.php index 6dd1bda..e34c468 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EmailItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EmailItem.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Field\Plugin\Field\FieldType; use Drupal\Component\Utility\Random; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldItemBase; use Drupal\Core\TypedData\DataDefinition; @@ -72,7 +73,7 @@ public function getConstraints() { /** * {@inheritdoc} */ - public function generateSampleValue() { + public static function generateSampleValue(FieldDefinitionInterface $field_definition) { $random = new Random(); $values['value'] = $random->name() . '@example.com'; return $values; diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php index 671ae95..6718ea7 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php @@ -9,6 +9,7 @@ use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\TypedData\EntityDataDefinition; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldItemBase; use Drupal\Core\TypedData\DataDefinition; @@ -212,8 +213,11 @@ public function preSave() { /** * {@inheritdoc} */ - public function generateSampleValue() { - if ($referenceable = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($this->getFieldDefinition(), $this->getentity())->getReferenceableEntities()) { + public static function generateSampleValue(FieldDefinitionInterface $field_definition) { + $manager = \Drupal::service('plugin.manager.entity_reference.selection'); + // @todo How to get rid of $this in the line below. + // I need an $entity? We are a static method. + if ($referenceable = $manager->getSelectionHandler($field_definition, $this->getentity())->getReferenceableEntities()) { $group = array_rand($referenceable); $values['target_id'] = array_rand($referenceable[$group]); return $values; diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/FloatItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/FloatItem.php index a693a17..dccc70d 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/FloatItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/FloatItem.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Field\Plugin\Field\FieldType; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\TypedData\DataDefinition; @@ -50,8 +51,8 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) /** * {@inheritdoc} */ - public function generateSampleValue() { - $settings = $this->getSettings(); + public static function generateSampleValue(FieldDefinitionInterface $field_definition) { + $settings = $field_definition->getSettings(); $precision = rand(10, 32); $scale = rand(0, 2); $max = is_numeric($settings['max']) ?: pow(10, ($precision - $scale)) - 1; @@ -59,7 +60,7 @@ public function generateSampleValue() { // @see "Example #1 Calculate a random floating-point number" in // http://php.net/manual/en/function.mt-getrandmax.php $random_decimal = $min + mt_rand() / mt_getrandmax() * ($max - $min); - $values['value'] = $this->truncateDecimal($random_decimal, $scale); + $values['value'] = self::truncateDecimal($random_decimal, $scale); return $values; } diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php index 4de09c9..b48bc2f 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Field\Plugin\Field\FieldType; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\TypedData\DataDefinition; @@ -105,8 +106,10 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) /** * {@inheritdoc} */ - public function generateSampleValue() { - $values['value'] = mt_rand($this->getSetting('min'), $this->getSetting('max') ?: 10000); + public static function generateSampleValue(FieldDefinitionInterface $field_definition) { + $min = $field_definition->getSetting('min') ?: 0; + $max = $field_definition->getSetting('max') ?: 999; + $values['value'] = mt_rand($min, $max); return $values; } diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/NumericItemBase.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/NumericItemBase.php index 899b8e2..b0a7b28 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/NumericItemBase.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/NumericItemBase.php @@ -124,7 +124,7 @@ public function getConstraints() { * @return float * Decimal number truncated. */ - function truncateDecimal($decimal, $num) { + protected static function truncateDecimal($decimal, $num) { return floor($decimal * pow(10, $num)) / pow(10, $num); } diff --git a/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php b/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php index 06a2762..4b37a3c 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php +++ b/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php @@ -7,6 +7,7 @@ namespace Drupal\datetime\Plugin\Field\FieldType; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\TypedData\DataDefinition; @@ -104,8 +105,8 @@ public function settingsForm(array &$form, FormStateInterface $form_state, $has_ /** * {@inheritdoc} */ - public function generateSampleValue() { - $type = $this->getSetting('datetime_type'); + public static function generateSampleValue(FieldDefinitionInterface $field_definition) { + $type = $field_definition->getSetting('datetime_type'); // Just pick a date in the past year. No guidance is provided by this Field // type. diff --git a/core/modules/datetime/src/Tests/DateTimeItemTest.php b/core/modules/datetime/src/Tests/DateTimeItemTest.php index bdab4be..e6e06ea 100644 --- a/core/modules/datetime/src/Tests/DateTimeItemTest.php +++ b/core/modules/datetime/src/Tests/DateTimeItemTest.php @@ -77,8 +77,7 @@ public function testDateTimeItem() { // Test the generateSampleValue() method. $entity = entity_create('entity_test'); - $values = $entity->field_datetime->first()->generateSampleValue(); - $entity->field_datetime->first()->setValue($values); + $entity->field_datetime->generateSampleItems(); $this->entityValidateAndSave($entity); } diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceItemTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceItemTest.php index 9399ccf8..f0cf0d6 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceItemTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceItemTest.php @@ -115,10 +115,8 @@ public function testContentEntityReferenceItem() { // Test the generateSampleValue() method. $entity = entity_create('entity_test'); - $values = $entity->field_test_taxonomy_term->first()->generateSampleValue(); - $entity->field_test_taxonomy_term->first()->setValue($values); - $values = $entity->field_test_taxonomy_vocabulary->first()->generateSampleValue(); - $entity->field_test_taxonomy_vocabulary->first()->setValue($values); + $entity->field_test_taxonomy_term->generateSampleItems(); + $entity->field_test_taxonomy_vocabulary->generateSampleItems(); $this->entityValidateAndSave($entity); } diff --git a/core/modules/field/src/Tests/Boolean/BooleanItemTest.php b/core/modules/field/src/Tests/Boolean/BooleanItemTest.php index f9198b1..8bdbf78 100644 --- a/core/modules/field/src/Tests/Boolean/BooleanItemTest.php +++ b/core/modules/field/src/Tests/Boolean/BooleanItemTest.php @@ -72,6 +72,11 @@ public function testBooleanItem() { $entity->save(); $entity = entity_load('entity_test', $id); $this->assertEqual($entity->field_boolean->value, $new_value); + + // Test sample item generation. + $entity = entity_create('entity_test'); + $entity->field_boolean->generateSampleItems(); + $this->entityValidateSave($entity); } } diff --git a/core/modules/field/src/Tests/Email/EmailItemTest.php b/core/modules/field/src/Tests/Email/EmailItemTest.php index 18f0916..e476000 100644 --- a/core/modules/field/src/Tests/Email/EmailItemTest.php +++ b/core/modules/field/src/Tests/Email/EmailItemTest.php @@ -70,11 +70,10 @@ public function testEmailItem() { $entity = entity_load('entity_test', $id); $this->assertEqual($entity->field_email->value, $new_value); - // Test the generateSampleValue() method. + // Test sample item generation. $entity = entity_create('entity_test'); - $values = $entity->field_email->first()->generateSampleValue(); - $entity->field_email->first()->setValue($values); - $this->entityValidateAndSave($entity); + $entity->field_email->generateSampleItems(); + $this->entityValidateSave($entity); } } diff --git a/core/modules/field/src/Tests/Number/NumberItemTest.php b/core/modules/field/src/Tests/Number/NumberItemTest.php index fc57dab..8daf131 100644 --- a/core/modules/field/src/Tests/Number/NumberItemTest.php +++ b/core/modules/field/src/Tests/Number/NumberItemTest.php @@ -92,14 +92,11 @@ public function testNumberItem() { $this->assertEqual($entity->field_float->value, $new_float); $this->assertEqual($entity->field_decimal->value, $new_decimal); - // Test the generateSampleValue() method. + /// Test sample item generation. $entity = entity_create('entity_test'); - $values = $entity->field_integer->first()->generateSampleValue(); - $entity->field_integer->first()->setValue($values); - $values = $entity->field_float->first()->generateSampleValue(); - $entity->field_float->first()->setValue($values); - $values = $entity->field_decimal->first()->generateSampleValue(); - $entity->field_decimal->first()->setValue($values); + $entity->field_integer->generateSampleItems(); + $entity->field_float->generateSampleItems(); + $entity->field_decimal->generateSampleItems(); $this->entityValidateAndSave($entity); } diff --git a/core/modules/file/src/Plugin/Field/FieldType/FileItem.php b/core/modules/file/src/Plugin/Field/FieldType/FileItem.php index d5dcaca..4da530b 100644 --- a/core/modules/file/src/Plugin/Field/FieldType/FileItem.php +++ b/core/modules/file/src/Plugin/Field/FieldType/FileItem.php @@ -9,6 +9,7 @@ use Drupal\Component\Utility\Bytes; use Drupal\Component\Utility\Random; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; use Drupal\Core\Form\FormStateInterface; @@ -307,18 +308,18 @@ public function getUploadValidators() { /** * {@inheritdoc} */ - public function generateSampleValue() { + public static function generateSampleValue(FieldDefinitionInterface $field_definition) { $random = new Random(); - $settings = $this->getSettings(); + $settings = $field_definition->getSettings(); // Generate a file entity and reuse it for speed. - $destination = $settings['uri_scheme'] . '://' . $settings['file_directory'] . $random->generateWord(10) . '.txt'; - $data = $random->generateParagraphs(3); + $destination = $settings['uri_scheme'] . '://' . $settings['file_directory'] . $random->word(10) . '.txt'; + $data = $random->paragraphs(3); $file = file_save_data($data, $destination, FILE_EXISTS_ERROR); $values = array( 'target_id' => $file->id(), 'display' => (int)$settings['display_default'], - 'description' => $random->createSentences(10), + 'description' => $random->sentences(10), ); return $values; } diff --git a/core/modules/file/src/Tests/FileItemTest.php b/core/modules/file/src/Tests/FileItemTest.php index 09c5f13..0c55585 100644 --- a/core/modules/file/src/Tests/FileItemTest.php +++ b/core/modules/file/src/Tests/FileItemTest.php @@ -98,8 +98,7 @@ public function testFileItem() { // Test the generateSampleValue() method. $entity = entity_create('entity_test'); - $values = $entity->file_test->first()->generateSampleValue(); - $entity->file_test->first()->setValue($values); + $entity->file_test->generateSampleItems(); $this->entityValidateAndSave($entity); } diff --git a/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php b/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php index dd0ae48..1a14cc2 100644 --- a/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php +++ b/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php @@ -8,6 +8,7 @@ namespace Drupal\image\Plugin\Field\FieldType; use Drupal\Component\Utility\Random; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\TypedData\DataDefinition; @@ -316,9 +317,9 @@ public function preSave() { /** * {@inheritdoc} */ - public function generateSampleValue() { + public static function generateSampleValue(FieldDefinitionInterface $field_definition) { $random = new Random(); - $settings = $this->getSettings(); + $settings = $field_definition->getSettings(); static $images = array(); $min_resolution = empty($settings['min_resolution']) ? '100x100' : $settings['min_resolution']; @@ -330,7 +331,7 @@ public function generateSampleValue() { $tmp_file = drupal_tempnam('temporary://', 'generateImage_'); $destination = $tmp_file . '.' . $extension; file_unmanaged_move($tmp_file, $destination, FILE_CREATE_DIRECTORY); - if ($path = $random->generateImage(drupal_realpath($destination), $min_resolution, $max_resolution)) { + if ($path = $random->image(drupal_realpath($destination), $min_resolution, $max_resolution)) { $image = File::create(); $image->setFileUri($path); // $image->setOwner($account); @@ -343,7 +344,7 @@ public function generateSampleValue() { $images[$extension][$min_resolution][$max_resolution][$file->id()] = $file; } else { - return FALSE; + return array(); } } else { @@ -355,12 +356,12 @@ public function generateSampleValue() { list($width, $height) = getimagesize($file->getFileUri()); $values = array( 'target_id' => $file->id(), - 'alt' => $random->createSentences(4), - 'title' => $random->createSentences(4), + 'alt' => $random->sentences(4), + 'title' => $random->sentences(4), 'width' =>$width, 'height' => $height, ); - $this->setValue($values); + return $values; } /** diff --git a/core/modules/image/src/Tests/ImageItemTest.php b/core/modules/image/src/Tests/ImageItemTest.php index 2b7780b..1333711 100644 --- a/core/modules/image/src/Tests/ImageItemTest.php +++ b/core/modules/image/src/Tests/ImageItemTest.php @@ -117,8 +117,7 @@ public function testImageItem() { // Test the generateSampleValue() method. $entity = entity_create('entity_test'); - $values = $entity->image_test->first()->generateSampleValue(); - $entity->image_test->first()->setValue($values); + $entity->image_test->generateSampleItems(); $this->entityValidateAndSave($entity); } diff --git a/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php b/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php index 3956d9b..e9d1b4a 100644 --- a/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php +++ b/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php @@ -8,6 +8,7 @@ namespace Drupal\link\Plugin\Field\FieldType; use Drupal\Component\Utility\Random; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemBase; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Form\FormStateInterface; @@ -137,26 +138,26 @@ public function instanceSettingsForm(array $form, FormStateInterface $form_state /** * {@inheritdoc} */ - public function generateSampleValue() { + public static function generateSampleValue(FieldDefinitionInterface $field_definition) { // Set of possible top-level domains. $tlds = array('com', 'net', 'gov', 'org', 'edu', 'biz', 'info'); // Set random length for the domain name. $domain_length = mt_rand(7, 15); $random = new Random(); - switch ($this->getSetting('title')) { + switch ($field_definition->getSetting('title')) { case DRUPAL_DISABLED: $values['title'] = ''; break; case DRUPAL_REQUIRED: - $values['title'] = $random->createSentences(4); + $values['title'] = $random->sentences(4); break; case DRUPAL_OPTIONAL: // In case of optional title, randomize its generation. - $values['title'] = mt_rand(0,1) ? $random->createSentences(4) : ''; + $values['title'] = mt_rand(0,1) ? $random->sentences(4) : ''; break; } - $values['url'] = 'http://www.' . $random->generateWord($domain_length) . '.' . $tlds[mt_rand(0, (sizeof($tlds)-1))]; + $values['url'] = 'http://www.' . $random->word($domain_length) . '.' . $tlds[mt_rand(0, (sizeof($tlds)-1))]; return $values; } diff --git a/core/modules/link/src/Tests/LinkItemTest.php b/core/modules/link/src/Tests/LinkItemTest.php index 2badbbd..4b7864a 100644 --- a/core/modules/link/src/Tests/LinkItemTest.php +++ b/core/modules/link/src/Tests/LinkItemTest.php @@ -103,8 +103,7 @@ public function testLinkItem() { // Test the generateSampleValue() method. $entity = entity_create('entity_test'); - $values = $entity->field_test->first()->generateSampleValue(); - $entity->field_test->first()->setValue($values); + $entity->field_test->generateSampleItems(); $this->entityValidateAndSave($entity); } diff --git a/core/modules/path/src/Plugin/Field/FieldType/PathItem.php b/core/modules/path/src/Plugin/Field/FieldType/PathItem.php index 14c33c0..cff3fa6 100644 --- a/core/modules/path/src/Plugin/Field/FieldType/PathItem.php +++ b/core/modules/path/src/Plugin/Field/FieldType/PathItem.php @@ -8,6 +8,7 @@ namespace Drupal\path\Plugin\Field\FieldType; use Drupal\Component\Utility\Random; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldItemBase; use Drupal\Core\TypedData\DataDefinition; @@ -92,9 +93,9 @@ public function delete() { /** * {@inheritdoc} */ - public function generateSampleValue() { + public static function generateSampleValue(FieldDefinitionInterface $field_definition) { $random = new Random(); - $values['alias'] = str_replace(' ', '-', $random->createSentences(3)); + $values['alias'] = str_replace(' ', '-', strtolower($random->sentences(3))); return $values; } diff --git a/core/modules/taxonomy/src/Tests/TaxonomyTermReferenceItemTest.php b/core/modules/taxonomy/src/Tests/TaxonomyTermReferenceItemTest.php index 96ede60..1472991 100644 --- a/core/modules/taxonomy/src/Tests/TaxonomyTermReferenceItemTest.php +++ b/core/modules/taxonomy/src/Tests/TaxonomyTermReferenceItemTest.php @@ -111,10 +111,9 @@ public function testTaxonomyTermReferenceItem() { $this->assertEqual($entity->field_test_taxonomy->entity->id(), $term2->id(), 'Field item entity contains the new TID.'); $this->assertEqual($entity->field_test_taxonomy->entity->getName(), $term2->getName(), 'Field item entity contains the new name.'); - // Test the generateSampleValue() method. + // Test sample item generation. $entity = entity_create('entity_test'); - $values = $entity->field_test_taxonomy->first()->generateSampleValue(); - $entity->field_test_taxonomy->first()->setValue($values); + $entity->field_test_taxonomy->generateSampleItems(); $this->entityValidateAndSave($entity); } diff --git a/core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php b/core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php index 3270806..8b70203 100644 --- a/core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php +++ b/core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php @@ -8,6 +8,7 @@ namespace Drupal\text\Plugin\Field\FieldType; use Drupal\Component\Utility\Random; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemBase; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\TypedData\DataDefinition; @@ -86,17 +87,17 @@ public function onChange($property_name) { /** * {@inheritdoc} */ - public function generateSampleValue() { + public static function generateSampleValue(FieldDefinitionInterface $field_definition) { $random = new Random(); - $settings = $this->getSettings(); + $settings = $field_definition->getSettings(); if (empty($settings['max_length'])) { // Textarea handling - $value = $random->generateParagraphs(); + $value = $random->paragraphs(); } else { // Textfield handling. - $value = substr($random->createSentences(mt_rand(1, $settings['max_length'] / 3), FALSE), 0, $settings['max_length']); + $value = substr($random->sentences(mt_rand(1, $settings['max_length'] / 3), FALSE), 0, $settings['max_length']); } $values = array( diff --git a/core/modules/text/src/Tests/TextWithSummaryItemTest.php b/core/modules/text/src/Tests/TextWithSummaryItemTest.php index af0cdb0..04f813a 100644 --- a/core/modules/text/src/Tests/TextWithSummaryItemTest.php +++ b/core/modules/text/src/Tests/TextWithSummaryItemTest.php @@ -96,8 +96,7 @@ public function testCrudAndUpdate() { // Test the generateSampleValue() method. $entity = entity_create($entity_type); - $values = $entity->summary_field->first()->generateSampleValue(); - $entity->summary_field->first()->setValue($values); + $entity->summary_field->generateSampleItems(); $this->entityValidateAndSave($entity); }