I've create views as block. Then i tried to theme my views block by altering block--views-block-html.twig
file. I've struggled to get views machine name in this file. I need views machine name for some reason. So any other solutions to get views machine name?
3 Answers
Views machine name would be: /admin/structure/views/view/frontpage.
You can edit/change the view name on right corner: Edit view name / description.
You can override the template name using below.
[base template name]--[view machine name]--[view display id].html.twig
[base template name]--[view machine name]--[view display type].html.twig
So this would be:
views-view--frontpage--page.html.twig
or:
views-view--frontpage--block.html.twig
There is no theme information is displayed with D8 views as D7 views was having the those information.
Please refer the reference page.
-
Yes i know this. But i want to get views machine name inside the block--views-block-html.twig file– GuruCommented Jan 21, 2016 at 5:34
-
-
You lead me to choose theme file. Please read question carefully. I want to know how to get views machine name inside block--views-block-html.twig file.– GuruCommented Jan 21, 2016 at 6:58
You should be able to access the ID with:
$variables['view']->id();
...but to do that, you need to preprocess the block/view; in my case, I needed to grab the machine name of a view that I was preprocessing using template_chn_preprocess_views_view()
.
See the other options/methods you can access inside ViewExecutable
.
This would help someone still looking for adding a block template suggestion with View's machine name.
You should be able to get the View ID with $variables['elements']['content']['#view_id]
if exists.
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function my_theme_name_theme_suggestions_block_alter(array &$suggestions, array $variables) {
$content = $variables['elements']['content'];
// Add a common template suggestion for `View ID` view.
if (isset($content['#view_id']) && $content['#view_id'] === 'view_id') {
$suggestions[] = 'block__' . $content['#view_id'];
}
}