Problem/Motivation

  • Create a view with an "Embed" display.
  • Attempt to render the embedded view using the following code:
    $variables['my_embedded_view'] = array(
      '#type' => 'view',
      '#name' => 'example_view',
      '#display_id' => 'embed_display',
      '#arguments' => array(),
    );
    

This leads to a fatal error: Unsupported operand types in core/modules/views/src/Element/View.php (line 63). This error is caused by ViewsExecutable::executeDisplay(), which returns NULL for Embed display plugins.

Now, the View render element already defines an #embed variable, which looks like it is supposed to render the view via preview() instead of execute().

So it looks like this should work:

$variables['my_embedded_view'] = array(
  '#type' => 'view',
  '#name' => 'example_view',
  '#display_id' => 'embed_display',
  '#arguments' => array(),
  '#embed' => TRUE,
);

Unfortunately, this does not work either, because the code which is supposed to detect whether #embed is not empty references embed instead.

The following works, but produces the following user error: "embed" is an invalid render array key.

$variables['my_embedded_view'] = array(
  '#type' => 'view',
  '#name' => 'example_view',
  '#display_id' => 'embed_display',
  '#arguments' => array(),
  'embed' => TRUE,
);

Proposed resolution

  • Make the '#embed' variable work.

Remaining tasks

[todo]

User interface changes

None.

API changes

None.

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes

Comments

bforchhammer’s picture

StatusFileSize
new550 bytes
bforchhammer’s picture

Status: Active » Needs review
dawehner’s picture

+++ b/core/modules/views/src/Element/View.php
@@ -48,7 +48,7 @@ public static function preRenderViewElement($element) {
     if ($view && $view->access($element['#display_id'])) {
-      if (!empty($element['embed'])) {
+      if (!empty($element['#embed'])) {
         $element += $view->preview($element['#display_id'], $element['#arguments']);
       }
       else {
 

OOOH, this fix is absolute right. In general though I think #embed should be TRUE by default.

bforchhammer’s picture

StatusFileSize
new753 bytes

In general though I think #embed should be TRUE by default.

Easily changed. Works for me :)

dawehner’s picture

Well, I guess that we might have to change some of the usecases of the existing #type view?

ViewElementTest ... that one should afaik used once with and once without #embed?
ViewsTestDataElementForm probably needs to switch

bforchhammer’s picture

StatusFileSize
new0 bytes

In core #embed is only used once in DisplayPluginBase at the moment. Tests don't reference it, and apparently we currently don't check for the differences between preview() and executeDisplay(), so tests are still green.

I have duplicated the ViewElementTest for both cases, i.e. the default display and #embed = FALSE, as well as an embed display and $embed = TRUE.

bforchhammer’s picture

StatusFileSize
new6.97 KB

Hm, here we go.

Status: Needs review » Needs work

The last submitted patch, 7: views_render_element-2389275-7.patch, failed testing.

bforchhammer’s picture

Status: Needs work » Needs review
StatusFileSize
new8.92 KB

Let's try this again...

Status: Needs review » Needs work

The last submitted patch, 9: views_render_element-2389275-9.patch, failed testing.

bforchhammer’s picture

Status: Needs work » Needs review
StatusFileSize
new8.93 KB

And again...

Status: Needs review » Needs work

The last submitted patch, 11: views_render_element-2389275-11.patch, failed testing.

bforchhammer’s picture

Status: Needs work » Needs review
StatusFileSize
new69.55 KB
dawehner’s picture

Status: Needs review » Needs work

@bforchhammer
Can you reroll the latest patch against HEAD and not some arbitrary commit :)

bforchhammer’s picture

Status: Needs work » Needs review
StatusFileSize
new8.93 KB

Whoops, my bad. ;-)

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Alright, perfect!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

This issue addresses a major bug and is allowed per https://www.drupal.org/core/beta-changes. Committed 1c61006 and pushed to 8.0.x. Thanks!

  • alexpott committed 1c61006 on 8.0.x
    Issue #2389275 by bforchhammer: Views render element #embed not working
    

Status: Fixed » Closed (fixed)

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