Creating a custom twig layout for views content in Drupal 8

You are here

If site builder needs to create a custom view twig file. In Drupal 8 you have to register a suggestion with the theme array. Adding the function below to your template.theme will give the developer the ability to add a twig file called views-view-viewname--.html.twig.

/**
 * Implements hook_theme_suggestions_HOOK_alter() for form templates.
 * @param array $suggestions
 * @param array $variables
 * @param string $hook hook name
 */
function theme_name_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
  
  if($hook === 'views_view') {
       $suggestions[] = "views_view__".$variables['view']->current_display;
  }

  if($hook === 'views_view_unformatted') {
    $suggestions[] = "views_view__unformatted_".$variables['view']->current_display;
  }

 }