Skip to main content

Working through the following error

Notice: Only variables should be passed by reference in load_view_block_content() (line 391 of /var/www/vhosts/{domain}/httpdocs/themes/custom/{theme}/{theme}.theme.theme)

If you want to read the full error, scroll to the bottom in Appendix.  The related function to this error is

/**
 * Load the related block view content.
 *
 * @param object $view
 *   View as an object.
 * @param string $display
 *   View display name.
 * @param array $arguments
 *   View arguments used in contextual filtering.
 * @param bool $render_view
 *   View field.
 *
 * @return object
 *   Renedered output.
 */
function load_view_block_content($view, $display, $arguments, $render_view) {
  $view->setDisplay($display);
  $view->setArguments($arguments);
  // $view->preExecute();
  $view->execute();
  if ($render_view) {
    $render = \Drupal::service('renderer')->render($view->render());
  }
  else {
    $render = $view->render();
  }

  return $render;
}

Line 391

$render = \Drupal::service('renderer')->render($view->render());

 

This error message "Notice: Only variables should be passed by reference," typically arises from a PHP requirement that functions which accept references must be passed variables, not the result of an expression or a function call directly. This is because only variables can be passed by reference, as they occupy a memory location that can be referenced, while the result of an expression or function call does not have a fixed memory location in the same way.

In the case of your load_view_block_content() function, the issue occurs at line 391:

$render = \Drupal::service('renderer')->render($view->render());

Note, the \Drupal::service('renderer')->render() method expects a reference, but $view->render() is a function call, not a variable.

To resolve this issue, capture the result of $view->render() in a variable, and then pass that variable to the \Drupal::service('renderer')->render() method.

function load_view_block_content($view, $display, $arguments, $render_view) {
 $view->setDisplay($display);
 $view->setArguments($arguments);
 // $view->preExecute();
 $view->execute();
 if ($render_view) {
   // Capture the output of $view->render() in a variable
   $view_output = $view->render();
   // Now pass the variable by reference
   $render = \Drupal::service('renderer')->render($view_output);
 } else {
   $render = $view->render();
 }
 return $render;
}

This modification ensures that the \Drupal::service('renderer')->render() method is given a variable, thus complying with the PHP requirement for passing by reference, and should resolve the notice you're seeing.

Additionally, make sure that any changes you make to the codebase follow Drupal's coding standards and best practices, especially if you're working in a custom theme or module. It's also a good practice to clear the Drupal cache after making changes to the code to ensure that Drupal picks up the changes.

 

 

Appendix: The trace route of the error

Notice: Only variables should be passed by reference in load_view_block_content() (line 391 of /var/www/vhosts/{domain}/httpdocs/themes/custom/{theme}/{theme}.theme.theme)
#0 /var/www/vhosts/{domain}/httpdocs/core/includes/bootstrap.inc(164): _drupal_error_handler_real()
#1 /var/www/vhosts/{domain}/httpdocs/themes/custom/{theme}/{theme}.theme.theme(391): _drupal_error_handler()
#2 /var/www/vhosts/{domain}/httpdocs/themes/custom/{theme}/{theme}.theme.theme(443): load_view_block_content()
#3 [internal function]: hook_preprocess_node()
#4 /var/www/vhosts/{domain}/httpdocs/core/lib/Drupal/Core/Theme/ThemeManager.php(261): call_user_func_array()
#5 /var/www/vhosts/{domain}/httpdocs/core/lib/Drupal/Core/Render/Renderer.php(480): Drupal\Core\Theme\ThemeManager->render()
#6 /var/www/vhosts/{domain}/httpdocs/core/lib/Drupal/Core/Render/Renderer.php(240): Drupal\Core\Render\Renderer->doRender()
#7 /var/www/vhosts/{domain}/httpdocs/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(238): Drupal\Core\Render\Renderer->render()
#8 /var/www/vhosts/{domain}/httpdocs/core/lib/Drupal/Core/Render/Renderer.php(627): Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}()
#9 /var/www/vhosts/{domain}/httpdocs/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(231): Drupal\Core\Render\Renderer->executeInRenderContext()
#10 /var/www/vhosts/{domain}/httpdocs/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(128): Drupal\Core\Render\MainContent\HtmlRenderer->prepare()
#11 /var/www/vhosts/{domain}/httpdocs/core/lib/Drupal/Core/EventSubscriber/MainContentViewSubscriber.php(90): Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse()
#12 [internal function]: Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray()
#13 /var/www/vhosts/{domain}/httpdocs/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(111): call_user_func()
#14 /var/www/vhosts/{domain}/httpdocs/vendor/symfony/http-kernel/HttpKernel.php(186): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch()
#15 /var/www/vhosts/{domain}/httpdocs/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw()
#16 /var/www/vhosts/{domain}/httpdocs/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle()
#17 /var/www/vhosts/{domain}/httpdocs/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle()
#18 /var/www/vhosts/{domain}/httpdocs/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle()
#19 /var/www/vhosts/{domain}/httpdocs/core/modules/big_pipe/src/StackMiddleware/ContentLength.php(32): Drupal\Core\StackMiddleware\ContentLength->handle()
#20 /var/www/vhosts/{domain}/httpdocs/core/modules/page_cache/src/StackMiddleware/PageCache.php(191): Drupal\big_pipe\StackMiddleware\ContentLength->handle()
#21 /var/www/vhosts/{domain}/httpdocs/core/modules/page_cache/src/StackMiddleware/PageCache.php(128): Drupal\page_cache\StackMiddleware\PageCache->fetch()
#22 /var/www/vhosts/{domain}/httpdocs/core/modules/page_cache/src/StackMiddleware/PageCache.php(82): Drupal\page_cache\StackMiddleware\PageCache->lookup()
#23 /var/www/vhosts/{domain}/httpdocs/core/modules/ban/src/BanMiddleware.php(50): Drupal\page_cache\StackMiddleware\PageCache->handle()
#24 /var/www/vhosts/{domain}/httpdocs/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\ban\BanMiddleware->handle()
#25 /var/www/vhosts/{domain}/httpdocs/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
#26 /var/www/vhosts/{domain}/httpdocs/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
#27 /var/www/vhosts/{domain}/httpdocs/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle()
#28 /var/www/vhosts/{domain}/httpdocs/core/lib/Drupal/Core/DrupalKernel.php(704): Drupal\Core\StackMiddleware\StackedHttpKernel->handle()
#29 /var/www/vhosts/{domain}/httpdocs/index.php(19): Drupal\Core\DrupalKernel->handle()
#30 {main}

 

Related articles