1

I am attempting to modify the output of view programmatically using hook_views_post_execute.

I created the usual module structure (in this case using drupal generate:module.

I am able to use other hooks (such as hook_entity_view)successfully.

However, hook_views_post_execute causes blows up the website with the message

The website encountered an unexpected error. Please try again later.

The code block is right out of api.drupal.org.

/**
 * @param ViewExecutable $view
 */
function idm_views_post_execute(ViewExecutable $view) {

  // If there are more than 100 results, show a message that encourages the user
  // to change the filter settings.
  // (This action could be performed later in the execution process, but not
  // earlier.)
  $temp = $view;

  if ($view->total_rows > 100) {
    \Drupal::messenger()
      ->addStatus(t('You have more than 100 hits. Use the filter settings to narrow down your list.'));
  }
}
  • 2
    You need to provide the actual error so people can help. Otherwise all we can do is guess. Did you include the use statement for ViewExecutable? – Kevin Jan 9 at 17:45
  • 1
    No, it exists: api.drupal.org/api/drupal/… – Kevin Jan 9 at 17:48
  • Also note the comment on that page: Please note that this hook is not being called on the second request if (page) caching is active, even if view cache is disabled! – Kevin Jan 9 at 17:49
  • @kevin. Thanks for the quick reply. The actual error was as indicated. White screen on the web browser with the words. "The website encountered an unexpected error. Please try again later." I have no idea what you mean by "include the use statement" I suspect it's something simple like that though! – sea26.2 Jan 9 at 17:52
  • That isn't an error... its just an output saying an error happened. An error trace would tell you what the problem is. – Kevin Jan 9 at 17:52
1

My guess is you did not include the use statement at the top of your file that tells the application how to locate the class ViewExecutable. This is a PHP construct and not Drupal specific, which is why it is not always mentioned explicitly in the documentation.

At the top of your file, add:

use Drupal\views\ViewExecutable;

The incoming argument to the hook is of type ViewExecutable, but PHP does not know how to find that class, so it errors out unless you either use the use statement, or change the function signature to:

function idm_views_post_execute(Drupal\views\ViewExecutable $view)

Clear cache and reload. If the error persists, you'll need to provide log(s) to indicate what the actual error that occurred was.

If you copied and pasted the code here for hook_entity_view that would explain why that worked for you:

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_entity_view/8.6.x

The example here has the full path in the argument list.

hook_entity_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode)
  • You're awesome. I'm far to used to the old ways of Drupal 6 and 7. Added the "use" statement per your suggestion and problem is solved – sea26.2 Jan 9 at 18:08
  • The docs should be consistent in this manner, I would agree. Consider grabbing an editor like PHPStorm to help you wade into new territory - it would have caught this for you. – Kevin Jan 9 at 18:12
  • Actually, I just made the switch from Komodo IDE to PhpStorm... But, it either didn't catch it, or I didn't see it screaming at me – sea26.2 Jan 9 at 18:25
  • Did you set up the Drupal/Symfony plug-ins and set the language level to 7.0? Spend some time with it, these things will open up. – Kevin Jan 9 at 18:31

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.