Error message

You need to log in or create an account to access this page.

Problem/Motivation

When previewing nodes that use a Views Reference Field with a view that includes a node contextual filter configured to use the default argument using the content id from the URL, the view does not render any results as expected and seen when rendering the node.

The node views argument default expects the node parameter, as seen here.

\Drupal\node\Plugin\views\argument_default\Node::getArgument()

  public function getArgument() {
    if (($node = $this->routeMatch->getParameter('node')) && $node instanceof NodeInterface) {
      return $node->id();
    }
  }

However, the entity.node.preview route provides the node_preview parameter, which is upcasted to the node entity via the Drupal\node\ParamConverter\NodePreviewConverter()

See line 31 of node.routing.yml

entity.node.preview:
  path: '/node/preview/{node_preview}/{view_mode_id}'
  defaults:
    _controller: '\Drupal\node\Controller\NodePreviewController::view'
    _title_callback: '\Drupal\node\Controller\NodePreviewController::title'
  requirements:
    _node_preview_access: '{node_preview}'
  options:
    parameters:
      node_preview:
        type: 'node_preview'

Proposed resolution

Can we simply convert the parameter name in entity.node.preview from node_preview to node?

Remaining tasks

Patch, tests, ?

User interface changes

None

API changes

Unsure

Data model changes

None

Comments

jasonawant created an issue. See original summary.

jasonawant’s picture

Status: Active » Needs review
StatusFileSize
new5.02 KB

Here.s a patch that changes the parameter name and many places where its used for previewing to work.

Status: Needs review » Needs work

The last submitted patch, 2: drupal-node_preview_parameter-2942054-2.patch, failed testing. View results

timmillwood’s picture

This is one of those bugs that if we fix it correctly it's going to introduce a BC break. Would it not be possible to update \Drupal\node\Plugin\views\argument_default\Node::getArgument to expect node and node_preview?

jasonawant’s picture

Status: Needs work » Needs review
StatusFileSize
new896 bytes

Gotcha, yeah, here's a patch for that approach.

timmillwood’s picture

Issue tags: +Needs tests

Works for me!

Also, this was not picked up until now because we don't have test coverage, so I guess the next step is... add a test.

jasonawant’s picture

@timmillwood,

Could you provide some direction on what this test would looks like? A Kernel or Functional test? Jason

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

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should 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.

maxstarkenburg’s picture

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

Also chiming in since I'm seeing this issue still in 9.1.x, and patch 5 is working to fix this for me all these years later, though I know next to nothing about automated tests in order to help get this into core.

maxstarkenburg’s picture

FWIW, also adding a non-patch workaround suggested by my colleague instead of a core patch to address this on our site in the meanwhile (targeted in this case to work on just a specific view):

/**
 * Implements hook_views_pre_build().
 */
function my_module_views_pre_build(ViewExecutable $view) {
  switch ($view->id()) {
    case 'id_of_specific_view':
      if ($node = \Drupal::routeMatch()->getParameter('node_preview')) {
        $view->args[0] = $node->id();
      }
      break;
  }
}
vikashsoni’s picture

@jasonawant patch working for me thanks for the patch

fabianfiorotto’s picture

I modified #5 to make it work with node_revision. I also changed the Tid argument because it wasn't working when the option "Load default filter from node page" is selected.

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

Drupal 9.1.10 (June 4, 2021) and Drupal 9.2.10 (November 24, 2021) were the last bugfix releases of those minor version series. Drupal 9 bug reports should be targeted for the 9.3.x-dev branch from now on, and new development or disruptive changes should 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.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should 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.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should 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.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Was previous tagged for tests in #6 which still needs to happen

Thanks.

Version: 9.5.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. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

riyas_nr’s picture

This patch addresses an additional scenario where adding a block with a View reference field containing a contextual filter in Layout Builder results in the preview displaying 'Placeholder for the name Views block.'

acbramley’s picture

Can we simply convert the parameter name in entity.node.preview from node_preview to node?

I think this should definitely be explored. I have no background on why it's the way it is but it seems like changing the parameter name could be a better way forward and reduce some other code duplication we have (e.g in template_preprocess_page). This would however be quite disruptive for BC purposes.

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.