Problem/Motivation

There's no "Rendered entity" field handler in views. We used to have it in D7 (provided by the Entity module).

Proposed resolution

In D8 there's a "Rendered entity" area handler already, so it should not be hard to do the same as a field handler.

Remaining tasks

Commit.

User interface changes

Have a "Rendered entity" field in views UI.

API changes

-

Data model changes

-

CommentFileSizeAuthor
#54 interdiff.txt1.75 KBdawehner
#54 2632068-54.patch17.99 KBdawehner
#50 2632068-50.patch18.24 KBbojanz
#47 2632068-46.patch18.24 KBbojanz
#47 2632068-46.patch18.24 KBbojanz
#45 2632068-45.patch31.5 KBbojanz
#42 2632068-41.png59.8 KBmohit_aghera
#41 interdiff.txt529 bytesdawehner
#41 2632068-41.patch18.24 KBdawehner
#38 interdiff.txt5.21 KBdawehner
#38 2632068-38.patch17.72 KBdawehner
#29 2632068-29-rendered-entity.patch15.99 KBczigor
#24 interdiff.txt725 bytesbojanz
#24 2632068-24.patch15.94 KBbojanz
#20 interdiff.txt1.91 KBbojanz
#20 2632068-20.patch15.94 KBbojanz
#19 interdiff.txt889 bytesbojanz
#19 2632068-19.patch15.39 KBbojanz
#18 interdiff.txt2.3 KBbojanz
#18 2632068-18.patch15.54 KBbojanz
#17 2632068-17.patch15.53 KBdawehner
#17 interdiff.txt11.32 KBdawehner
#15 rendered_entity_views-2632068-15.patch14.27 KBmglaman
#15 interdiff-2632068-10-15.txt4.49 KBmglaman
#10 2632068-10-rendered-entity.patch13.99 KBczigor
#7 2632068-7-rendered-entity.patch14.98 KBczigor
#2 2632068-2-rendered-entity.patch5.71 KBczigor
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

czigor created an issue. See original summary.

czigor’s picture

Status: Active » Needs review
FileSize
5.71 KB

This kind of works. Might need some cleanup.

czigor’s picture

Assigned: Unassigned » czigor

Writing test is in progress.

dawehner’s picture

Version: 8.0.x-dev » 8.1.x-dev
Issue tags: +Needs tests

Given semantic versioning this won't land in 8.0.x but just in 8.1.x

  1. +++ b/core/modules/views/src/Plugin/views/field/RenderedEntity.php
    @@ -0,0 +1,158 @@
    +/**
    + * Provides an area handler which renders an entity in a certain view mode.
    

    ... field handler

  2. +++ b/core/modules/views/src/Plugin/views/field/RenderedEntity.php
    @@ -0,0 +1,158 @@
    +  protected function getEntityManager() {
    +    return $this->entityManager;
    

    There should be a more specific service available now.

  3. +++ b/core/modules/views/src/Plugin/views/field/RenderedEntity.php
    @@ -0,0 +1,158 @@
    +
    +  /**
    +   * {@inheritdoc}
    +   */
    +  protected function getLanguageManager() {
    +    return $this->languageManager;
    

    Unused

  4. +++ b/core/modules/views/src/Plugin/views/field/RenderedEntity.php
    @@ -0,0 +1,158 @@
    +  /**
    +   * {@inheritdoc}
    +   */
    +  protected function getView() {
    +    return $this->view;
    +  }
    

    That method is not used

czigor’s picture

Thanks for the review! Tests are still under construction.

I'll fix 1.
2. I'm implementing here \Drupal\views\Entity\Render\EntityTranslationRenderTrait::getEntityManager() which should return a \Drupal\Core\Entity\EntityManagerInterface. Until it's not fixed there I can't really do anything about it.
3-4: I need to implement these too because they are abstract methods of \Drupal\views\Entity\Render\EntityTranslationRenderTrait.

dawehner’s picture

2. I'm implementing here \Drupal\views\Entity\Render\EntityTranslationRenderTrait::getEntityManager() which should return a \Drupal\Core\Entity\EntityManagerInterface. Until it's not fixed there I can't really do anything about it.
3-4: I need to implement these too because they are abstract methods of \Drupal\views\Entity\Render\EntityTranslationRenderTrait.

Oh yeah you are right,

czigor’s picture

Credits go to @eager_hun for providing a connection to the world wide web.

dawehner’s picture

+++ b/core/modules/views/src/Tests/Handler/FieldRenderedEntityTest.php
@@ -0,0 +1,156 @@
+    $edit = array(
+      'id' => 'foobar',
+      'label' => t('My view mode'),
+    );
+    $this->drupalPostForm('admin/structure/display-modes/view/add/node', $edit, t('Save'));
+
...
+    // Foobar view mode should be custom.
+    $edit = [
+      'display_modes_custom[foobar]' => 1,
+    ];
+    $this->drupalPostForm('admin/structure/types/manage/article/display', $edit, t('Save'));
+    $this->drupalPostForm('admin/structure/types/manage/page/display', $edit, t('Save'));
...
+    // Hide the body on the foobar view mode.
+    $edit = [
+      'fields[body][type]' => 'hidden',
+    ];
+    $this->drupalPostForm('admin/structure/types/manage/article/display/foobar', $edit, t('Save'));
+    $this->drupalPostForm('admin/structure/types/manage/page/display/foobar', $edit, t('Save'));
...
+    // Show the body on the foobar view mode.
+    $edit = [
+      'fields[body][type]' => 'text_default',
+    ];
+    $this->drupalPostForm('admin/structure/types/manage/article/display/foobar', $edit, t('Save'));
+    $this->drupalPostForm('admin/structure/types/manage/page/display/foobar', $edit, t('Save'));

IMHO we could use the API directly, so using EntityViewMode::create()->save() and EntityViewDisplay::create()->save().

Its just unnecessary HTTP requests without any benefit, IMHO

dawehner’s picture

Given that people might want to use it in 8.0.x already, we backported it to https://github.com/fago/entity/pull/19

czigor’s picture

Thanks, that makes perfect sense. Number of lines in test has been drastically reduced.

jibran’s picture

I think this is ready other then this minor feedback.

+++ b/core/modules/views/src/Plugin/views/field/RenderedEntity.php
@@ -0,0 +1,159 @@
+  /**
+   * {@inheritdoc}
+   */
+  protected function getView() {
+    return $this->view;
+  }

AFAICS we are not using this method so this can be removed.

czigor’s picture

:) Yes, it is, see comment #5 -> 3-4.

bojanz’s picture

Assigned: czigor » bojanz
Status: Needs review » Needs work

I want to try and convert that test to a kernel one, for speed.

dawehner’s picture

Issue tags: -Needs tests

Thank you @bojanz!!

mglaman’s picture

Assigned: bojanz » Unassigned
Status: Needs work » Needs review
FileSize
4.49 KB
14.27 KB

Here's my attempt at porting it to ViewKernelTestBase

Status: Needs review » Needs work

The last submitted patch, 15: rendered_entity_views-2632068-15.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review
FileSize
11.32 KB
15.53 KB

Converted the test to use entity_test entities, and some more changes.

bojanz’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
15.54 KB
2.3 KB

We're good to go.

Made some final code style tweaks.

bojanz’s picture

Actually the test had unused imports.

bojanz’s picture

The calculateDependencies part was requested by dawehner. Fixed a few comments along the way.

Now really RTBC?

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 20: 2632068-20.patch, failed testing.

jibran’s picture

+++ b/core/modules/views/src/Plugin/views/field/RenderedEntity.php
@@ -0,0 +1,207 @@
+      $view_builder = $this->entityManager->getViewBuilder($this->getEntityTypeId());

Why not use $entity->getEntityTypeId() instead of $this->getEntityTypeId()?

dawehner’s picture

I'm okay with that, but the testbot isn't.

bojanz’s picture

Status: Needs work » Needs review
FileSize
15.94 KB
725 bytes

Bah.

dawehner’s picture

Ha, I'm confused because there is also getEntityType() on there.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Yeah nevermind the trait adds this.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

I think we should add some more test coverage...

  • make sure that the correct translations are displayed
  • Cache tags merge correctly
  • And config dependency added
+++ b/core/modules/views/src/Plugin/views/field/RenderedEntity.php
@@ -0,0 +1,207 @@
+  /**
+   * {@inheritdoc}
+   */
+  public function getCacheMaxAge() {
+    return 0;
+  }

So these views are uncacheable?

digitaldonkey’s picture

I can Confirm the patch working.
"Uncachable" sounds like a very bad Idea. Didn't check that yet.

But I consider this a an essential feature of views as everybody should use viemodes as much as possible (think about your themers ;) ).
It will also enable e.g. Table layout with rendered entities.
So I hope It will be ready in 8.3.

czigor’s picture

Assigned: Unassigned » czigor
FileSize
15.99 KB

Caching changed to tags-based.

Translation and cache-tag tests are also coming soon.

michaellenahan’s picture

I would like to test this manually, but I'm not sure how to manually test the fact that the rendered entity is being cached.

digitaldonkey’s picture

Tested on Drupal VDD with Drupal 8-1.x

Works as expected. Cache entries are created in cache table (tag based). --> Seems to work :)

jcnventura’s picture

Issue tags: +SprintWeekend2016

We looked at this in the Frankfurt sprint.

czigor’s picture

Assigned: czigor » Unassigned
dawehner’s picture

+++ b/core/modules/views/src/Plugin/views/field/RenderedEntity.php
@@ -0,0 +1,208 @@
+    if (isset($entity) && $entity->access('view')) {
+      $view_builder = $this->entityManager->getViewBuilder($this->getEntityTypeId());
+      return $view_builder->view($entity, $this->options['view_mode']);
+    }
+    return [];

IMHO we should set #access to $entity->access('view', NULL, TRUE);

dawehner’s picture

dawehner’s picture

@czigor Did you managed to add some translation / cache-tags tests?

czigor’s picture

Sorry, no. It's high on my list, but I still can't say when I will find the time for it. Feel free to take it.

dawehner’s picture

Status: Needs work » Needs review
FileSize
17.72 KB
5.21 KB

Alright, I'll give it a try. Thanks you for your really quick response.

jibran’s picture

I think we need an empty update hook to clear the views data cache.

dawehner’s picture

True, I guess. On the other hand, nothing is broken without it. The new feature will just appear at some point ;)

dawehner’s picture

FileSize
18.24 KB
529 bytes

Sure, here it is.

mohit_aghera’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
59.8 KB

I've tested patch #41 on locally.
It seems working fine. Content is also rendered in proper display format. Screenshot attached.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 41: 2632068-41.patch, failed testing.

The last submitted patch, 41: 2632068-41.patch, failed testing.

bojanz’s picture

Status: Needs work » Needs review
FileSize
31.5 KB

Weird, it applies with fuzz locally. Here's a reroll anyway.

Status: Needs review » Needs work

The last submitted patch, 45: 2632068-45.patch, failed testing.

bojanz’s picture

Status: Needs work » Needs review
FileSize
18.24 KB
18.24 KB

Now with less trash.

The last submitted patch, 47: 2632068-46.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 47: 2632068-46.patch, failed testing.

bojanz’s picture

Status: Needs work » Needs review
FileSize
18.24 KB

mglaman caught the duplicate update number.

bojanz’s picture

Status: Needs review » Reviewed & tested by the community

Back to RTBC.

dawehner’s picture

Issue summary: View changes
catch’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/views/src/Tests/Handler/FieldRenderedEntityTest.php
    @@ -0,0 +1,209 @@
    +    $role->grantPermission('bypass node access');
    

    Can't imagine this is necessary for entity_test.

  2. +++ b/core/modules/views/src/Tests/Handler/FieldRenderedEntityTest.php
    @@ -0,0 +1,209 @@
    +    // The view should not display the body field.
    

    Copy/paste comments - no nodes or body?

  3. +++ b/core/modules/views/src/Tests/Handler/FieldRenderedEntityTest.php
    @@ -0,0 +1,209 @@
    +  /**
    +   * Tests the rendered entity output with the body field configured to show.
    +   */
    +  public function testRenderedEntityWithField() {
    +    \Drupal::currentUser()->setAccount($this->user);
    +
    +    // Show the body on the node.x.foobar view mode.
    +    EntityViewDisplay::load('entity_test.entity_test.foobar')->setComponent('test_field', ['type' => 'string', 'label' => 'above'])->save();
    +
    +    // The view should display the body field.
    

    Also copy/paste comments?

dawehner’s picture

Status: Needs work » Needs review
FileSize
17.99 KB
1.75 KB

Oh yeah , there we go.

bojanz’s picture

Status: Needs review » Reviewed & tested by the community

Sorry about that, catch.

#54 addresses #53, a search shows no more mentions of "node". Let's give it another shot.

  • catch committed ae2ee03 on 8.1.x
    Issue #2632068 by bojanz, dawehner, czigor, mglaman: Rendered entity...
catch’s picture

Status: Reviewed & tested by the community » Fixed

That's better. Committed/pushed to 8.1.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

yannickoo’s picture

Oh that's funny, I have created a module for that 4 months ago - well done guys :)

xjm’s picture

Issue tags: +8.1.0 release notes