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.
Problem/Motivation
When selecting "Views: Filter by an entity reference view" as a reference method on a entity_reference field's settings a fatal error occurs.
Fatal error: Using $this when not in object context in /var/www/html/d8.local/drupal/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php on line 252
Proposed resolution
$form_state->setError($element, $this->t('The views entity selection mode requires a view.'));
settingsFormValidate
is called outside of an object context so we should use t() here instead of $this->t().
Remaining tasks
Write a patch
Write test
User interface changes
None
API changes
None
Beta phase evaluation
Issue category | Bug because fatal error occurs |
---|---|
Issue priority | Major because php error is thrown when doing a simple action through the interface. |
Prioritized changes | The main goal of this issue is fixing a bug. |
Comment | File | Size | Author |
---|---|---|---|
#36 | 2430813-36-complete.patch | 5.98 KB | geertvd |
#31 | 2430813-31-complete.patch | 5.8 KB | geertvd |
#31 | interdiff.txt | 1.71 KB | geertvd |
Comments
Comment #1
geertvd CreditAttribution: geertvd commentedComment #2
geertvd CreditAttribution: geertvd commentedComment #3
geertvd CreditAttribution: geertvd commentedComment #4
geertvd CreditAttribution: geertvd commentedThis should probably be a static method also.
Comment #5
geertvd CreditAttribution: geertvd commentedComment #6
geertvd CreditAttribution: geertvd commentedComment #7
geertvd CreditAttribution: geertvd commentedComment #8
geertvd CreditAttribution: geertvd commentedAdded test and made
settingsFormValidate
a static method.Comment #9
geertvd CreditAttribution: geertvd commentedComment #14
geertvd CreditAttribution: geertvd commentedComment #15
geertvd CreditAttribution: geertvd commentedComment #17
amateescu CreditAttribution: amateescu commentedThe patch and test looks great, just one tiny nitpick:
We don't need to enable the views_ui module :)
Comment #18
geertvd CreditAttribution: geertvd commentedActually, this is an issue I bumped into while writing the test, at the moment views_ui needs to be enabled because we are printing
Url::fromRoute('views_ui.add')
hereHow do you propose we should fix that? It's a bit silly that views_ui needs to be enabled for this.
Comment #19
geertvd CreditAttribution: geertvd commentedForgot to take an interdiff but this is what I added
Comment #20
amateescu CreditAttribution: amateescu commentedIf we're going down this path, the current_user and module_handler services can be injected in the plugin.
Comment #22
geertvd CreditAttribution: geertvd commentedSomething like this then
Comment #23
amateescu CreditAttribution: amateescu commentedPerfect!
Comment #24
alexpottWhy does #element_validate have to be static? I.e can't we do
Comment #25
amateescu CreditAttribution: amateescu commentedBy declaring element validate (also process, etc.) handlers as a static methods we skip a lot of form object serialization. Field API does this consistently all over the place :)
Comment #26
alexpottHuh http://3v4l.org/S0onn?
Comment #27
alexpottBasically I don't get the argument in #25 - using $this will not save any form serialisation and if the form is serialised the object detection in serialisation might well end up being more memory efficient that storing the classname.
Comment #28
amateescu CreditAttribution: amateescu commentedWhen you start adding objects (e.g. entities, services) to $d, all of them will need to go through the serialization process, while the simple
array(array('\some\class\name', 'someMethod'))
will not.Comment #29
alexpottYeah but this is $this
Comment #30
amateescu CreditAttribution: amateescu commentedI don't understand #29.
Comment #31
geertvd CreditAttribution: geertvd commentedJust changed
to
I guess this makes sense, since settingsFormValidate is only called from within
ViewsSelection
I don't see why it should be static.Comment #32
alexpottre #29 - there is no saving when doing get_called_class($this) because $this is what is being serialised.
Comment #33
BerdirIt's not the $this that is serialized. It's $form, containing references to $this. And if we make that static, then we can avoid that being serialized. As @amateescu said, field api widgets use that pattern a lot.
Being able to do this is one of the main reasons I introduced support for '::methodName' , but that only works in the actual form objects.
See http://3v4l.org/ALTTj
Comment #34
Fabianx CreditAttribution: Fabianx commentedI agree that we should serialize as less as possible to avoid deep-object-references.
Comment #35
alexpottOk get_called_class it is - not going to fight - but it makes code less testable and dependencies less obvious.
We have object serialization going on all over the place.
Comment #36
geertvd CreditAttribution: geertvd commentedSame patch as in #22
Comment #37
amateescu CreditAttribution: amateescu commentedComment #38
webchickLooks like Alex has been all over this one.
Comment #39
alexpottThis issue addresses a major bug and is allowed per https://www.drupal.org/core/beta-changes. Committed 9dfa950 and pushed to 8.0.x. Thanks!