Problem/Motivation
While playing with a view, i may have clicked multiple times on the remove button for a "representative node" fields. The view modification page was not working anymore and i reloaded the page.
At reload, the page showed multiple error, and i can not edit the view anymore.
First warning:
Warning : Undefined array key "tid_representative" dans Drupal\views\Plugin\views\HandlerBase->getEntityType() (ligne 709 de web/core/modules/views/src/Plugin/views/HandlerBase.php).
Second warning:
Warning : Trying to access array offset on value of type null dans Drupal\views\Plugin\views\HandlerBase->getEntityType() (ligne 710 de /app/web/core/modules/views/src/Plugin/views/HandlerBase.php).
Error:
InvalidArgumentException: A valid cache entry key is required. Use getAll() to get all table data. in Drupal\views\ViewsData->get() (line 140 of core/modules/views/src/ViewsData.php).
Steps to reproduce
1. In a view, add a "Taxonomy term: Representative node" relationship (like described by View Taxonomy Term and hide empty terms.
2. In the same view, add filters on the representative node content.
3. Remove the representative node relationship without removing the filters.
Proposed resolution
In HandlerBase.php, rewrite the getEntityType() method like this:
/** * {@inheritdoc} */ public function getEntityType() { // If the user has configured a relationship on the handler take that into // account. $relationship_key = $this->options['relationship'] ?? ''; if ($relationship = $this->displayHandler->getOption('relationships')[$relationship_key] ?? NULL) { $table_data = $this->getViewsData()->get($relationship['table']); $views_data = $this->getViewsData()->get($table_data[$relationship['field']]['relationship']['base']); } else { $views_data = $this->getViewsData()->get($this->view->storage->get('base_table')); } if (isset($views_data['table']['entity type'])) { return $views_data['table']['entity type']; } else { throw new \Exception("No entity type for field {$this->options['id']} on view {$this->view->storage->id()}"); } }
Remaining tasks
Make a better rewrite of getEntityType(), to have only one $views_data failsafe instead of the two introduce by the given solution.
User interface changes
None.
API changes
None.
Data model changes
None.
Release notes snippet
Fix error when editing view with filters from a missing representative node relationship.
| Comment | File | Size | Author |
|---|---|---|---|
| #18 | 3275276-18.patch | 1.02 KB | merlin06 |
Issue fork drupal-3275276
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3275276-main
changes, plain diff MR !17639
- 3275276-removing-a-relationship
changes, plain diff MR !17638
Comments
Comment #2
larowlanThanks for reporting
Anything that lets you cause a fatal error from the UI we class as major
Comment #3
lendudeI tried to reproduce this, but can't.
Steps I took:
* Clean Umami install
* Create taxonomy View
* Add 'Representative node' relationship with default settings
* Add Filter on node content title using the relationship, set to contains 'choc'
* Remove relationship
* Error shown in UI: The filter criterion Content: Title uses a relationship that has been removed.
So nothing break for me, are there any specific steps I'm missing here that would cause this to break?
Comment #4
landure commentedThe break was a edge case I think, and short of using hacks, it is difficult to reproduce.
The issue stays valid, since if the break occurs, there is no way to recover. The fix seems painless and has no impact on functionnality.
Comment #7
guypaddock commentedRepro:
At this point, the Views UI stopped responding -- I couldn't even get the dialog that I was using to edit/delete the relationship to close. Upon refreshing the edit form for the View, I got a WSOD with these stack traces:
Comment #8
alain dg commentedHad the same on a user view with reationship to "content authored" whose (a) title is displayed as field + (b) node values are used as filter.
Deleted the relationship by mistake and now cannot use the view Ui anymore.
Note that I didn't save the view, but nevertheless the view UI for that particular view throws the error message below, even after clearing cache or changing browser.
Error message:
Comment #9
merlin06 commentedPreserving the original code
Comment #10
merlin06 commentedComment #11
merlin06 commentedCredit for this snippet goes to https://www.drupal.org/u/bradtreloar from https://www.drupal.org/australian-competition-and-consumer-commission-accc and I modified the issue description with it.
Comment #12
merlin06 commentedRolled a patch.
Comment #13
merlin06 commentedPatch 12 is incorrect.
Rolled patch 13 against original file.
Comment #14
prabuela commentedComment #15
prabuela commentedComment #16
merlin06 commentedComment #17
_pratik_#15 patch contains error, #16 is corrupted.
Try this.
Thanks
Comment #18
merlin06 commentedThis patch restores the original else branch that is still required.
Comment #19
lendudeTried the steps in #8 on a clean Drupal Umami install, couldn't reproduce. Might miss some steps to break?
Didn't try the steps in #7 since those require a contrib module. Can we get some steps to reproduce this with just core? Or an automated test that shows where this breaks? Without clear steps its really hard to see if the proposed fix is the right way to go.
Comment #21
smustgrave commentedSince there hasn't been a follow up in 2 years going to close out. If still a valid bug please re-open.
Comment #22
eduardo morales albertiIt is possible that the error is related to the PHP serializer. In our case, we use igbinary. We will try to change it to PHP, and see if the problem is gone
Comment #23
todea commentedWe encountered this issue in Drupal 10.6.9. Patch #18 worked, allowing us to cancel the view edits (which hadn't been saved yet) and restore previous version.
Comment #24
duozerskThe issue is still present with Drupal 11.4.11, the patch in #18 applies cleanly through cweagans/composer-patches and then I was able to edit the broken view with incorrectly removed relationship.
Comment #25
smustgrave commentedIssue tags would still be relevant then so putting in NW
Comment #26
mortona2k commentedThe problem here is that HandlerBase->getEntityType() is getting stuck trying to figure out the entity type of a removed relationship.
The trigger is the content type/bundle filter. Set up a view that uses it with a relationship, save, then delete the relationship.
#18 is an appropriate fix, falling back to the view's entity type when the relationship type is missing.
Another way would be to update the relationship selection on the filter when the relationship is removed.
#2988983: Removing a relationship to an entity reference field can break a View was created after this one, and mixes this up with another problem that appears similar. That one breaks when the relationship is to a table that doesn't have an entity type. Similar problem, but the handling is a little different.
Comment #29
mortona2k commented!MR17639 includes the fix from #18, an extra check in HandlerBase to load the entity type from the current table, and a test.
This may fix most if not all issues in #2988983: Removing a relationship to an entity reference field can break a View.
This MR was generated with claude after studying both issues.
Comment #30
chrisdarke commentedOn 11.4.10: After I had built a view with 2 levels of entity reference (Main Content > Sub Content, SubSub Content), and used the deepest level entity in a filter (SubSubContent.address country code external filter), I then went to delete that SubSub Content relationship forgetting I had a filter set, and it threw everything into the error state as described. Clearing the temp bin, and even `drush cim` didn't fix it.
Patched with patch #18 and immediately was able to edit the view and cancel the changes.
RTBC, but I haven't had a chance to check the MR, and was just trying to get unstuck so I am going to leave it in 'Needs Review' for someone to review the MR.