Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Depends on #1969728: Implement Field API "field types" as TypedData Plugins
Instructions are on #2014671: [META] Convert all field types to plugins
Comment | File | Size | Author |
---|---|---|---|
#18 | 2015691-followup-18.patch | 1.83 KB | amateescu |
#14 | field_type_number-2015691-14.patch | 20.41 KB | plopesc |
#14 | interdiff.txt | 3.07 KB | plopesc |
#11 | field_type_number-2015691-11.patch | 20.33 KB | plopesc |
#11 | interdiff.txt | 1.46 KB | plopesc |
Comments
Comment #1
swentel CreditAttribution: swentel commentedComment #2
swentel CreditAttribution: swentel commentedCommit see http://drupalcode.org/sandbox/yched/1736366.git/commit/9e0092439003b70d1...
(but not sure whether all works fine)
Comment #3
swentel CreditAttribution: swentel commentedComment #4
plopescTaking this one...
Comment #5
yched CreditAttribution: yched commented@plopesc. mind you that swentel already did the job in a sandbox for a couple field types, including number.
For those field types, it's just about extracting his code and posting a patch.
Comment #6
plopescHello
@yched: I know, but based on text module implementation I've made some minor changes in sandbox code.
Attaching first approach patch.
It works fine and number field tests passes but, preSave() function does not work at the moment, given that it depends on #1868004: Improve the TypedData API usage of EntityNG.
Regards
Comment #7
plopescChanging status
Comment #8
yched CreditAttribution: yched commentedThanks (again) @plopesc !
While we're here, let's use \Drupal\Component\Utility\MapArray::copyValuesToKeys() instead of drupal_map_assoc() (see the @deprecated doc in the phpdoc for drupal_map_assoc())
Same a couple lines below.
Let's extract $field->hasData() in a variable to avoid computing it several times.
Too long, this should be split in several lines.
+ this looks seriously wrong :-)
Should be something like
$this->value = round($this->value, $field->settings['scale'])
Don't forget that in a FieldItem class, $this is the value object itself. No need to go through getInstance() / getField() to access it.
Nitpick: I know this is just moving some existing code around, but let's remove the empty line at the beginning of the function. (also applies to the other classes)
Not needed, should be removed.
This looks wrong, 'min' and 'max' are instance-level settings, so this should be $this->getInstance()->settings['min'];
(same applies for max)
Also, better extract $this->getInstance()->settings to its own variable at the beginning of the function, like you have done in other places.
copy/paste - should be 'maxMessage' :-)
Seeing number.module empty aside from hook_help() : +++++++ :-)
Comment #9
plopescHi!
Attaching patch following your advices.
Thanks for your feedback!
Regards.
Comment #10
yched CreditAttribution: yched commentedLength / Range : right, missed that.
I really vote for the short syntax :-)
$this->value = round($this->value, $this->getInstance()->getField()->settings['scale'])
Comment #11
plopescNo problem, using the short way ;)
Given that this is the preferred approach, I changed it in
isEmpty()
method too.Regards.
Comment #12
yched CreditAttribution: yched commentedThanks!
RTBC if green.
Comment #13
yched CreditAttribution: yched commentedEr - sorry, I should have realized that earlier, my bad.
As I just wrote in #2015693: Convert field type to typed data plugin for link module :
"The goal is to make our FieldItem classes (like LinkItem here) be usable for configurable fields (that can be added through the UI) as well as base entity fields (that are hardcoded in the Entity definition).
This means relying as little as possible on the Field / FieldInstance definition structures that are specific to configurable fields, and instead use the FieldDefinitionInterface."
This means that everywhere we access settings (hm, except probably the schema() method for now, this one is pretty tied to configurable fields), this should be done through FieldDefinitionInterface::getFieldSettings() / getFieldSetting() :
$setting = $this->getFieldDefinition()->getFieldSetting('foo_bar');
or, if you're going to use several settings :
$settings = $this->getFieldDefinition()->getFieldSettings();
And just don't bother whether the setting is field-level or instance-level (this will get both indistinctly)
For the field label : $this->getFieldDefinition()->getFieldLabel();
Comment #14
plopescHello
I did it without any problem for field settings or field label. However, the hasData() property necessary to enable/disable the settings form elements. I think it's not accessible through getFieldDefinition.
I don't know if we could add it to FieldDefinitionInterface, or if you have planned that this property will be accessed in other way in the future.
Regards.
Comment #15
yched CreditAttribution: yched commentedYeah, hasData() is not in the interface. Same as for schema(), we need to polish that a bit.
In practice, it's not too important since hasData() is typically called in settingsForm(). When this method is called, this necessarily implies a configurable field.
I guess the best solution would be to let the caller pass the result of hasData() as a param to settingsForm(). That's how it worked in D7.
I'll open an issue for that. This patch is good to go.
Thanks !
Comment #16
yched CreditAttribution: yched commentedOpened #2051157: pass $has_data as a param to ConfigFieldItem::settingsForm()
Comment #17
alexpottCommitted e30e80c and pushed to 8.x. Thanks!
Comment #18
amateescu CreditAttribution: amateescu commentedThe patch needed a small update after #2041423: Rely on 'provider' instead of 'module' for Field plugin types, here it is :)
Comment #19
yched CreditAttribution: yched commentedYup.
Comment #20
alexpottI should have caught that :(
Committed 3454b34 and pushed to 8.x. Thanks!