Does theme_suggestions_views_view_alter work with views in Drupal 8.x.3? I have cleared the cache(drush cr) multiple times to try and get it to work. After a search on the web, I can't seem to find any information on this subject and wondering is this a bug or it can't be done.

  1. Image Selection_004.jpg shows that suggestions exist in debugging.
  2. Image Selection_008.jpg is the twig file that I have created in my theme.

This is the function I have been trying to get to work.

/**
 * Implements hook_theme_suggestions_HOOK_alter() for form templates.
 *
 * @param array $suggestions
 * @param array $variables
 */
function purencool_vision_theme_suggestions_views_view_alter(array &$suggestions, array $variables) {

  if (isset($variables['view'])) {
    $view_id = $variables['view']->id();
    $view_block = $variables['view']->getDisplay()->display['id'];
    array_splice($suggestions, 1, 0, 'view__' . $view_id . '__' . $view_block);
  }
}
CommentFileSizeAuthor
Selection_008.jpg144.84 KBPurencool
Selection_004.jpg23.36 KBPurencool
Members fund testing for the Drupal project. Drupal Association Learn more

Comments

Purencool created an issue. See original summary.

Purencool’s picture

Issue summary: View changes
Lendude’s picture

Project: Views » Drupal core
Version: 8.x-3.x-dev » 8.3.x-dev
Component: Miscellaneous » views.module

Views is in Drupal Core now, so moving to the right queue.

tetranz’s picture

I think the problem here is that your suggestion needs to start with views__view__ and template start with views--view--.

I'm not sure of all the rules but it seems like you need to start with the base name as the template you're overriding.

Having said that, I don't think you need this code at all. Core already implements the pattern that I think you're trying to achieve.

http://redcrackle.com/blog/drupal-8/theme-views-templates

I created a view called test with a display called page_1.
That uses my template views-view--test--page-1.html.twig with no additional suggestions needed.

Unfortunately the theme debugging comments doesn't show view's default suggestions.

Just as side note. I don't really follow your use of array_splice. Isn't that the same as this?
$suggestions[] = 'view__' . $view_id . '__' . $view_block;

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

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.