3

I'm using Drupal 8.0.5, and need to force a title on a View.

I've tried various Views hooks, but will focus on hook_views_pre_render().

There's a ViewExecutable $view taken as an argument, which is not passed by reference.

I've tried:

function mymodule_views_pre_render(ViewExecutable $view) {
  $view->setTitle('foo');
  // $view->save(); // I've also tried this
}

I've gone through step-debugging to ensure it's getting called, but it seems the setTitle method does not affect the output that renders. I've also set my theme back to Seven to ensure there's not a theme issue involved.

I'm also happy to embed the View using a custom block, but I am running into the same issue there.

How does one modify a ViewExecutable object properly?

  • There's a ViewExecutable $view taken as an argument, which is not passed by reference. That's deliberate - object handles are pointers in PHP, so they exhibit the same behaviour as if they had been "passed by reference" to a method. Adding a leading & would be redundant, though not actually invalid – Clive Mar 17 '16 at 16:11
  • Thanks, Clive. I mention that because this is quite different than how the hooks worked in Drupal 7, and anticipate some answers coming in that may assume it's the same. – mikeDOTexe Mar 17 '16 at 16:13
  • It isn't though! Check out Drupal 7's hook_query_alter() - notice the object isn't passed by reference. You'll find the same thing for any other hook, function, method, that takes an object. You're probably thinking of hooks/functions/methods that take a built-in type as a parameter, and need to persist changes back to the caller. Those built-in types are pass-by-value, so need to be referenced (&'d) for that to work – Clive Mar 17 '16 at 16:18
  • I'm talking about the hook in question here: Drupal 6 and Drupal 7: hook_views_pre_render(&$view) – mikeDOTexe Mar 17 '16 at 16:56
  • That's just a bad migration from Views 6 -> 7, someone forgot to remove the ampersand. It would've been revelant for D6 (which supports earlier versions of PHP). It's a small point, really, I don't want to labour it. I only mention it because you've highlighted the fact that there's no ampersand in the question, and I just wanted to let you know that isn't going to be part of whatever problem you're having, because its absence is by design, per the language spec, etc – Clive Mar 17 '16 at 17:47
2

You should be able to override the title by implementing hook_preprocess_views_view:

function MYMODULE_preprocess_views_view(&$variables) {
  $variables['title'] = 'Foo';
}

The View object is available in $variables['view'] if you need it for context.

Incidentally, I think you'd be able to use the setTitle() method on the View object itself if you implement hook_views_post_execute or hook_views_post_build (but I haven't tried either).

  • I'll mark this as the answer, although I'm still wondering how to use the ViewExecutable class and the methods therein. – mikeDOTexe Mar 25 '16 at 21:39
1
function hook_views_post_render(\Drupal\views\ViewExecutable $view) {
   if ($view->id() == 'viewid') {
    // Set the view title.
    $view->setTitle('NEW TITLE');
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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