1

I had a similar question recently, related to changing the path on a Views Display. Following this reasoning (as well as other examples I've seen), I assumed I could follow a similar approach. Unfortunately, that doesn't seem to be the case.

Related question for reference: Change Views Path Programatically

What I currently have:

$view = \Drupal\views\Entity\View::load('custom_view');
$display =& $view->getDisplay('page_1');
$display['display_options']['title'] = 'My Custom Title';
$view->save();

However, this does not seem to alter the title of the Display. I've attempted a method of changing the Display title in a Views hook, at that works correctly, but I'd like to be able to change the title without relying on actually viewing the View to utilize its hooks. In addition, I believe this would set the title for all Displays, which I don't want. Working example:

function my_moudule_views_post_render(ViewExecutable $view) {
    $view->setTitle('My Custom Title');
}

Following this approach, I attempted to utilize the \Drupal\views\Views::getView() method, but was unsuccessful.

1

Your code is working I believe, but it could be that "page_1" is not the correct page variant name...I had to use 'default', that was what I ran into when creating a test View and attempting to try your code out... in my case to verify, I enable kint, do a ksm($view) and check the 'display' array to verify the variants. You may need to alter 'default' to see the intended results. Also make sure to cache rebuild just in case drush cr afterwards, you may want to programmatically empty the cache bin for this View depending on the use case.

Also, you mentioned the hook affecting all the Views. That is true, but you could add some more granular logic to the hook to make it only affect specific View, by specifically drilling down to the ID (the 'machine name" in the Views GUI) and performing logic only if the machine name matches, e.g. if my View machine name is "foobar_view_custom", then you can do:

/**
 * Implements hook_views_post_render().
 */
function mymodule_views_post_render(ViewExecutable $view, &$output, CachePluginBase $cache)
{
  $display = $view->getDisplay();
  $id = $display->display['id'];
  if ($id === "foobar_view_custom") {
    $view->setTitle("PROGRAMMATICALLY CHANGED");
  }

}

The difference here is you are altering the actual data in the database for the title of the View entity in your first implementation, versus in the hook, it would only change it during rendering, so really depends on use case (if the title was dynamic based on date or something, the hook might be better. Long term alteration, like in an update hook or something, probably would want to alter the actual data with the entity method) Both implementations should cache just fine.

  • Interesting. You are correct, switching it to the "default" display does indeed make my first chunk of code work correctly. I know for sure I have the display machine name correct, though, as changing the path with "page_1" display is working correctly. Odd, but glad to know I was down the right path! – Nuuou Feb 7 '18 at 19:12
  • You could also implement the post_render logic by specifically drilling down to the ID (the 'machine name" in the Views GUI) and performing logic only if the machine name matches, I'll update my answer – Tyler Fahey Feb 7 '18 at 19:13
  • 1
    Alright, I figured this out finally. You mentioning "default" display got me thinking. Since I only had a single display for now (multiple in the future), the "Title" field was relying on the "default" display for its value, not being set to be overridden by the current display. Changing the Title via Views UI to "This page (override)" allows be to modify the View via the "page_1" display. Thank you, very helpful answer! – Nuuou Feb 7 '18 at 19:25

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.