Follow-up to #2320157: Generate placeholder content for Field types
Problem/Motivation
Now that #2238085: [regression] options_allowed_values() signature doesn't allow for Views filter configuration has landed, we should be able to implement the ListItemBase::generateSampleValue() method.
Steps to reproduce
- Install Drupal standard
- Download and install Devel Generate (part of Devel)
- Add a field on standard pages
- Name: whatever
- Type: any type that starts with "List" eg "List (text)"
- Allowed values list: at least one value
- Allowed number of values: whatever
- Generate standard pages using Devel Generate
- Edit one of those pages and see that your field has not been filled
Proposed resolution
Adapt the code from Devel Generate 7.x
function list_devel_generate($object, $field, $instance, $bundle) {
if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
return devel_generate_multiple('_list_devel_generate', $object, $field, $instance, $bundle);
}
else {
return _list_devel_generate($object, $field, $instance, $bundle);
}
}
function _list_devel_generate($object, $field, $instance, $bundle) {
$object_field = array();
if ($allowed_values = list_allowed_values($field)) {
$keys = array_keys($allowed_values);
$object_field['value'] = $keys[mt_rand(0, count($allowed_values) - 1)];
}
return $object_field;
}
Remaining tasks
Contributor tasks needed
| Task |
Novice task? |
Contributor instructions |
Complete? |
| Create a patch |
|
Instructions |
Done |
| Update the issue summary noting if allowed during the beta |
|
Instructions |
|
| Manually test the patch |
Novice |
Instructions |
|
| Review patch to ensure that it fixes the issue, stays within scope, is properly documented, and follows coding standards |
|
Instructions |
|
User interface changes
None.
API changes
None.
Why this should be an RC target
That code is never directly used in Core except for tests. It's not disruptive at all. It's going to be used by Devel and enlight #2590993: Create stub entities with proper default values (see #7)
Comments
Comment #1
duaelfrComment #2
duaelfrAdded steps to reproduce.
Comment #3
duaelfrI tried to find out how to test it.Looking inOptionsFieldTest::testUpdateAllowedValues()I found a call toListItemBase::generateSampleItems()followed by a call toFieldUnitTestBase::entityValidateAndSave()that's supposed to test if generated values are OK. That function seems to work well to validate if a given value is valid but it does not seem to check if there is a value in the field or not, even if the FieldConfig entity has the required setting.The attached patch is supposed to fail because it makes thetest_optionsfield onentity_testrequired and theListItemBase::generateSampleItems()cannot generate values. Unless the testbot has different results than my local tests, that path is not going to fail.Forget that comment. I didn't saw the FieldConfig settings were replaced just before the call to
FieldUnitTestBase::entityValidateAndSave().Comment #4
duaelfrI finally found out how to improve tests for that tiny feature.
Comment #7
mikeryanLooks good to me (and will enable me to remove a kludge from #2590993: Create stub entities with proper default values).
Comment #8
duaelfrLet's try to get this one in the RC :)
Comment #9
alexpottThis is rc eligible as it only changes test code.
Comment #10
alexpottCommitted cbef10d and pushed to 8.0.x. Thanks!