Problem/Motivation

Default EntityViewBuilder tries to render shortcut entities based on assumptions that don't match with shortcut entities.

By default EntityViewBuilder creates an render array'#theme' => 'shortcut', which tries to render the entity using non existend theme hook implementation.

Proposed resolution

Create view builder for Shortcut entity type.

Remaining tasks

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

lauriii created an issue. See original summary.

jibran’s picture

@lauriii can you explain the problem a little?

lauriii’s picture

In #674108: ThemeManager::theme() does not trigger an error when a theme hook is not found we want to trigger an error every time theme hook is not found. EntityCacheTagsTestBase tests by default referencing entities and printing them as rendered entity. Without view builder it tries to render the entity using non-existing theme hook shortcut. There's probably bunch of other ways that could be solved in case this issue doesn't make any sense.

tim.plunkett’s picture

Ahh, I was very confused at first, because entity types shouldn't be given a view_builder unless they specify.
Except \Drupal\Core\Entity\ContentEntityType forcibly adds \Drupal\Core\Entity\EntityViewBuilder as one!

Unfortunately, removing that line is out of the question. It would be the correct fix, since it blindly adds EntityViewBuilder without ensuring there is a corresponding theme hook.

Since AFAIK we don't actually view shortcuts via that code, I think we should add an alter to unset the view builder...

dawehner’s picture

Could we check somehow in the view builder whether the theme hook exists and otherwise throw an exception?

andypost’s picture

Status: Active » Needs review
FileSize
538 bytes

Let's see how many thing could be broken

Status: Needs review » Needs work

The last submitted patch, 7: 2698909-7.patch, failed testing.

andypost’s picture

andypost’s picture

Status: Needs work » Postponed
Issue tags: +Needs tests
FileSize
450 bytes

The same way "menu_link_content" missing view builder

lauriii’s picture

Status: Postponed » Needs review

That is not necessary to be fixed before this. That only adds the theme suggestions but this should work without any theme suggestions.

andypost’s picture

I see no reason to implement view builder because root of problem in missing theme functions, see #2186653: $build['#attributes'] added in hook_entity_view_alter() are not output for entity types that provide neither a #theme nor #theme_wrappers (breaks Quick Edit module for those entity types)

Ways to solve that
- add missing theme functions (wrappers or real ones) #2270883: Automatically add theme hook suggestions for all entity types
- add preprocessing into \Drupal\Core\Entity\EntityViewBuilder #2023571: Support preprocessing in EntityViewBuilder

Status: Needs review » Needs work

The last submitted patch, 10: 2698909-10.patch, failed testing.

andypost’s picture

Missing builders after applying #7

    [block_content_type] => config
    [comment_type] => config
    [commerce_line_item_type] => config
    [commerce_order_type] => config
    [commerce_order] => content
    [commerce_line_item] => content
    [commerce_currency] => config
    [commerce_product_variation_type] => config
    [commerce_product_attribute] => config
    [commerce_product_type] => config
    [commerce_store_type] => config
    [contact_form] => config
    [editor] => config
    [entity_queue] => config
    [entity_subqueue] => content
    [field_storage_config] => config
    [field_config] => config
    [file] => content
    [filter_format] => config
    [image_style] => config
    [node_type] => config
    [profile_type] => config
    [rdf_mapping] => config
    [search_page] => config
    [shortcut] => content
    [shortcut_set] => config
    [action] => config
    [menu] => config
    [taxonomy_vocabulary] => config
    [user] => content
    [user_role] => config
    [menu_link_content] => content
    [view] => config
    [base_field_override] => config
    [entity_view_display] => config
    [entity_form_display] => config
    [entity_view_mode] => config
    [entity_form_mode] => config
    [date_format] => config

drush @d8 scr theme-entity.php

<?php
  $types = [];
  /** @var \Drupal\Core\Utility\ThemeRegistry $registry */
  $registry = \Drupal::service('theme.registry')->getRuntime();
  foreach (\Drupal::entityTypeManager()->getDefinitions() as $definition) {
    if (!$definition->hasViewBuilderClass()) {
      $types[$definition->id()] = $definition->isSubclassOf('Drupal\Core\Config\Entity\ConfigEntityInterface') ? 'config' : 'content';
    }
  }
  print_r($types);
andypost’s picture

The core content entities without theme function are

    [block_content] => content
    [contact_message] => content
    [file] => content
    [shortcut] => content
    [menu_link_content] => content

script is

<?php
  $types = [];
  /** @var \Drupal\Core\Utility\ThemeRegistry $registry */
  $registry = \Drupal::service('theme.registry')->getRuntime();
  foreach (\Drupal::entityTypeManager()->getDefinitions() as $definition) {
    if (!$registry->has($definition->id()) && !$definition->isSubclassOf('Drupal\Core\Config\Entity\ConfigEntityInterface')) {
      $types[$definition->id()] = 'content';
    }
  }
  print_r($types);

dawehner’s picture

None of those really have any meaning, you cannot render out a file entity, nor a contact message etc. I would argue that removing the view builder manually is the right solution still.

andypost’s picture

Status: Needs work » Needs review
FileSize
802 bytes
1.29 KB

@dawehner did you mean that way?
Only alter allows to unset that.

dawehner’s picture

@andypost
Yeah something like that ...

The last submitted patch, 17: 2698909-17-test.patch, failed testing.

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.

tstoeckler’s picture

Title: Create missing view builder for Shortcut entity type » Remove view builder for shortcut entity
Status: Needs review » Reviewed & tested by the community

Looks fine and has tests. Thanks!

tim.plunkett’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: -Needs tests
FileSize
2.66 KB
2.03 KB

Sorry to mess with such a simple and RTBC patch, but that alter was bothering me.
Specifying NULL in the annotation makes more sense to me, so that we keep the values with the rest of the annotation. Additionally, a custom module can now specify a view builder if they so desire, without needing to worry about hook invocation order.

Also since I was worried about the treatment of NULL, I expanded the tests.

Berdir’s picture

We also have this problem with other entity types, we just have no tests for them. menu_link_content, file and probably more.

The default entity view builder was added in #2446483: Viewing fields requires a view builder., so with this change, it is now longer possible to add shortcut fields to a view. So, I don't think we can just remove it.

Berdir’s picture

The last submitted patch, 17: 2698909-17.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 24: entity-view-builder-2698909-24.patch, failed testing.

Berdir’s picture

Status: Needs work » Needs review
FileSize
1.18 KB

This should be better.

lauriii’s picture

Issue tags: +Needs tests

The approach looks good. It would be good to add some test coverage for this (even in this issue).

Berdir’s picture

Updated the previous test. It's no longer shortcut specific, so we could make it generic as well and use entity_test or so.

Berdir’s picture

Title: Remove view builder for shortcut entity » EntityViewBuilder uses non-existing #theme hooks
Version: 8.3.x-dev » 8.2.x-dev
Component: shortcut.module » entity system
Berdir’s picture

The last submitted patch, 29: entity-view-builder-2698909-29-test-only.patch, failed testing.

tstoeckler’s picture

Issue tags: +Dublin2016
jibran’s picture

+++ b/core/modules/shortcut/tests/src/Kernel/ShortcutEntityTest.php
@@ -0,0 +1,38 @@
+class ShortcutEntityTest extends KernelTestBase {

Should we make this a generic entity test instead?

jibran’s picture

I meant something like this.

Status: Needs review » Needs work

The last submitted patch, 35: entityviewbuilder_uses-2698909-35.patch, failed testing.

Berdir’s picture

@jibran: I would prefer to keep those overrides for forward compatibility with #2808481: Introduce generic entity template with standard preprocess and theme suggestions.

I was thinking about making the hook generic as well but actually, I'm not sure if we need to test every single entity type for a generic implementation.

jibran’s picture

Status: Needs work » Needs review

Ok then NR for patch in #29 again.

Berdir’s picture

Re-uploading the patch from #29 to make sure it still passes.

I think this is ready unless someone feels strongly about converting this test to use entity_test or so. Note that entity_test actually gets a template in #2808481: Introduce generic entity template with standard preprocess and theme suggestions, so it would need to be updated there again.

jibran’s picture

Status: Needs review » Reviewed & tested by the community

I think this is ready unless someone feels strongly about converting this test to use entity_test or so.

I think it is fine.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/lib/Drupal/Core/Entity/EntityViewBuilder.php
@@ -159,6 +158,11 @@ protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
+    if (\Drupal::service('theme.registry')->getRuntime()->has($this->entityTypeId)) {

We should inject the service - no?

I'm not too keen on using shortcut for this. It is easy to imagine someone adding a shortcut theme and removing the test and then we lose coverage of this change.

Berdir’s picture

Ok. This moves the test to EntityViewBuilderTest and uses test entities. I also injected the theme registry but didn't update all the subclasses, core has 3 that inject additional dependencies.

I also removed the #theme removal in EntityTestViewBuilder. This is different from the others, as that will get to use the generic entity template in #2808481: Introduce generic entity template with standard preprocess and theme suggestions and then I will update the test for the new behavior.

Status: Needs review » Needs work

The last submitted patch, 42: entity-view-builder-2698909-42.patch, failed testing.

Berdir’s picture

Removed left-over empty test class.

tstoeckler’s picture

Status: Needs review » Reviewed & tested by the community

Thank you, that looks good. I agree with not updating the other view builders for now, that would probably double that patch size (or worse...).

+++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityViewBuilderTest.php
@@ -2,9 +2,11 @@
+use Drupal\shortcut\Entity\Shortcut;

That's not used.

Can be fixed on commit.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 44: entity-view-builder-2698909-44.patch, failed testing.

The last submitted patch, 44: entity-view-builder-2698909-44.patch, failed testing.

tstoeckler’s picture

Status: Needs work » Reviewed & tested by the community

Not sure what happened here...

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 44: entity-view-builder-2698909-44.patch, failed testing.

Berdir’s picture

Status: Needs work » Reviewed & tested by the community

Testbot, be nice again, please :)

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed 408f00c to 8.3.x and 0290c9c to 8.2.x. Thanks!

Fixed unused use on commit.

+++ b/core/lib/Drupal/Core/Entity/EntityViewBuilder.php
@@ -159,6 +170,11 @@ protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
+      $build['#theme'] = $this->entityTypeId;

This kind of magic naming concerns me. But not changed here...

  • alexpott committed 408f00c on 8.3.x
    Issue #2698909 by Berdir, andypost, jibran, tim.plunkett, lauriii,...

  • alexpott committed 0290c9c on 8.2.x
    Issue #2698909 by Berdir, andypost, jibran, tim.plunkett, lauriii,...

Status: Fixed » Closed (fixed)

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