Problem/Motivation

Some views are not over entities. The 'Entity' row display for REST views still allows itself to be added to them, it just breaks.

Steps to reproduce:

  1. Enable the REST module
  2. Create a view over 'Log entries', and provide a REST export display. Hit Save. See NewView.png
  3. Notice that the new view uses the 'Entity' row display, and yields an error. See ViewError.png

Without an actual entity, QueryPluginBase::getEntityTableInfo() finds nothing, and no base fields are added. So there's no fields at all, and the query breaks.

Proposed resolution

Solution: use hook_views_plugins_row_alter() and add all entity base tables to data_entity][base. (Per @dawehner, see #5.)

This is not the most elegant solution, but it retains BC. The "proper" solution will have to wait for D9. We should open a new issue for that proper D9 solution and add a @todo to the hook_views_plugins_row_alter() implementation pointing to that issue.
The proper D9 solution:

DataEntityRow should filter by entity type (or rather, by the base table). This probably means making it a derived plugin. Just like \Drupal\views\Plugin\views\row\EntityRow uses deriver = "Drupal\views\Plugin\Derivative\ViewsEntityRow", we'd need pretty much the same for DataEntityRow.
While we're at it, we might want to make DataEntityRow and EntityRow both inherit from a TranslatedRowPluginBase. That would save a bunch of boilerplate (eg: for EntityTranslationRenderTrait), and they could probably use the same deriver too.

Remaining tasks

Contributor tasks needed
Task Novice task? Contributor instructions Complete?
Create a patch Instructions
Add automated tests Instructions

User interface changes

Minimal. Only the default row will change for new REST export views on non-entities, but that's broken anyway. We might decide to change the displayed row plugin name from 'Entity' to eg: 'User', 'Node', etc.

API changes

None.

Data model changes

None.

Comments

vasi created an issue. See original summary.

vasi’s picture

Issue summary: View changes

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

wim leers’s picture

Issue summary: View changes
Issue tags: +VDC

While we're at it, we might want to make DataEntityRow and EntityRow both inherit from a TranslatedRowPluginBase. That would save a bunch of boilerplate (eg: for EntityTranslationRenderTrait), and they could probably use the same deriver too.

If we want this, then this should be blocked on #2664880: DataEntityRow doesn't respect translations (which I just RTBC'd, yay!).

But I think this can continue regardless in the mean time, because the bulk of the work can still be done.

Thanks for the great bug report! Updated the issue summary's proposed resolution with some more details.

dawehner’s picture

To be honest I disagree with the solution in the issue summary as it would require a BC breaking change. It would certainly require an update of existing views, which we better avoid if possible.

An alternative approach is to use hook_views_plugins_row_alter() and add all entity base tables to data_entity][base.

wim leers’s picture

Ohhh, I like that idea! I was also concerned about the BC break, and first wrote a comment explaining how I didn't see how that could work, but then I saw it, and couldn't see a better way.

So, I guess, #5 is the recommended solution because no BC break, but for D9, we'd want to do it in the way described in the IS?

dawehner’s picture

So, I guess, #5 is the recommended solution because no BC break, but for D9, we'd want to do it in the way described in the IS?

Yeah I think so. It provides a better UX (as it has a better label) and it provides more semantic information in the configuration. It also allows specialized plugins per entity type, even we should avoid that.

wim leers’s picture

Issue summary: View changes

Alright, cool. Updated IS accordingly.

wim leers’s picture

Issue summary: View changes

Forgot to update the API changes and Data model changes sections.

dawehner’s picture

Thank you wim!

dawehner’s picture

Status: Active » Needs review
StatusFileSize
new1.81 KB

Here is a quick try.

wim leers’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Manually tested.

Before
Perform the STR in the IS: create a new view: display log entries, check the checkbox for REST export.
Observe that it is showing entities, which makes no sense
Confirmed by looking at the config:
      row:
        type: data_entity
        options: {  }
After
Same as above
Observe … that it is unfortunately the same. So #11 did not fix it.
Confirmed by looking at the config:
      row:
        type: data_entity
        options: {  }

Finally, this probably needs an integration test? That would:

  1. Enable Views + REST
  2. Go to /admin/structure/views/add
  3. Do the same as the STR
  4. View::load(), inspect the rest_export_1 display's [display_options][row][type]
dawehner’s picture

Issue tags: +Novice

Thank you for the manual test

Oh I see, so this is about the wizard part as well.

geertvd’s picture

Status: Needs work » Needs review
StatusFileSize
new2.17 KB
new1.33 KB
new2.83 KB

Added tests and made sure the default row type was not set to data_entity for non-entities.

Status: Needs review » Needs work

The last submitted patch, 14: 2666226-14.patch, failed testing.

The last submitted patch, 14: 2666226-14-tests.patch, failed testing.

geertvd’s picture

Status: Needs work » Needs review
StatusFileSize
new3.98 KB

This one should be better

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Nice test!

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

Needs a reroll.

geertvd’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests, -Needs reroll
StatusFileSize
new3.9 KB
dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Thank you @geertvd!

catch’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/modules/rest/rest.module
@@ -27,3 +28,10 @@ function rest_help($route_name, RouteMatchInterface $route_match) {
+function rest_views_plugins_row_alter(array &$plugins) {
+  (new RestViewsPluginsRowAlter(\Drupal\views\Views::viewsData()))->viewsPluginsRowAlter($plugins);
+}

Instead of three lines of logic in a hook implementation, we have one line, then 15 lines of boiler plate in the class, so we can call a method from the hook implementation that contains the three lines. Really?

Also, can we not do the original solution in 8.x? We could add new plugins, deprecate the old ones, hide them in the UI, keep them around for bc for exported Views, and potentially even do the upgrade path for non-exported views. And then even later add deprecation notices for the old plugins to inform people they need to update their Views in preparation for 9.x

wim leers’s picture

Assigned: Unassigned » dawehner
Issue tags: -Novice

Interesting. Assigning to @dawehner, this is more Views than it is REST. I think this makes it no longer novice.

catch’s picture

Two clarifications on #22:

1. If this was complex and/or needed unit testing, then I could see the case for taking the logic out of the hook implementation into a class, neither is the case here though.

2. We don't necessarily have to do that fix here, but it seems worth exploring whether it's possible since it could apply to more things later on, and worth trying in a follow-up.

dawehner’s picture

clarifications on #22:
1. If this was complex and/or needed unit testing, then I could see the case for taking the logic out of the hook implementation into a class, neither is the case here though.

Well yeah I'm following this pattern on custom module development, as it enables you to actually do TDD, which well, at the end of the day, doesn't require you to keep the tests around.

2. We don't necessarily have to do that fix here, but it seems worth exploring whether it's possible since it could apply to more things later on, and worth trying in a follow-up.

Well we could try that fix together with an update path. The more you talk about it, the more I'm convinced of the other solution not really being a BC break.

vasi’s picture

Version: 8.1.x-dev » 8.2.x-dev
StatusFileSize
new6.36 KB

Here's an attempt at the original deriver-based solution. We keep around data_entity for BC, but mark it no_ui.

Notes:
* Our deriver is a lot of boilerplate, any way to shrink that?
* No update hook yet
* It will look a bit weird if you try to edit the row style on an existing view, because the currently selected row style will not be available for selection
* Probably more tests are needed

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dawehner’s picture

+++ b/core/modules/rest/src/Plugin/Deriver/ViewsEntityRow.php
@@ -0,0 +1,112 @@
+    $this->derivatives[''] = [
+      'provider' => 'rest',
+      'no_ui' => TRUE,
+    ] + $base_plugin_definition;

What happens if you go to the UI, don't the radio than not has this value and you get a form validation error?

wim leers’s picture

Status: Needs review » Needs work
Issue tags: +Needs upgrade path, +Needs upgrade path tests

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

wim leers’s picture

Title: DataEntityRow allows itself to be added to non-entity views » REST views: DataEntityRow allows itself to be added to non-entity views
Issue tags: -Needs upgrade path, -Needs upgrade path tests +Needs update path, +Needs update path tests
wim leers’s picture

wim leers’s picture

Apparently I already said that in #32.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jacobsanford’s picture

StatusFileSize
new6.4 KB
new3.82 KB

@vasi's patch in #26 no longer applied to 8.6.x. A reroll with no further modifications is attached - comments in #28 still not addressed.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

rafmagsou’s picture

StatusFileSize
new2.89 KB

Just adaptation to the patch works on drupal 9 update

rafmagsou’s picture

StatusFileSize
new6.41 KB

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

larowlan’s picture

Issue tags: +Needs issue summary update, +Bug Smash Initiative
Related issues:

Marked #2858814: REST views: fatal error when using row plugin and Views is aware of non-Entity-based tables as a dupe and transferred credit

Can we get an issue summary update of what's left here - looks like #28?

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.