Is it possible to have a block/view render from within my Mega Menu twig template?
I've created a region, created a view/block and added that block to the region. But it's a matter of having that content from the view displayed in my menu.
I started off by considering I may be able to simply have a region specified from within my twig menu loop.
If you want to add something to your template, you need to use preprocess function in your module/theme. For example, you can add view variable using views_embed_view function:
/**
* Implements hook_preprocess_HOOK().
*/
function MYTHEME_preprocess_menu(&$variables) {
switch ($variables['menu_name']) {
case 'mega-menu':
$variables['my_view'] = views_embed_view('my_view');
break;
}
}
After this, $my_view variable will be defined in your menu--mega-menu.html.twig template.
Adding block is a bit more difficult. Please take a look at this answer.
Related
Is there a way to change or add class/id tags to items in Drupal? For instance, I want to change a certain set of images but the only class listed on all images is "img-responsive."
I'm very new to Drupal and trying to teach myself everything in a very limited amount of time. So if you answer, please provide clear directions/provide screenshots that a newbie could understand! I really appreciate any guidance or direction to my possible next steps.
This is an absolutely too broad question. You should invest the time to teach yourself about Drupal's theming system and about its preprocessing hooks first.
Start with Working With Twig Templates which will teach you among other things how to create a custom template and change markup or attributes (class, id etc.) according your needs.
Next checkout Preprocessing and modifying attributes in a .theme file. Which assumes you have created a custom theme or sub-theme. There are some hooks you can use to programmatically change classes of certain elements.
img-responsive tells me that you are using some theme that uses some CSS framework. Hopefully you already created your own sub-theme. Then you can add something like the following to your modules .theme file.
To add a class directly to the <img> tag you can identify it by its image style:
/**
* Implements template_preprocess_image().
*/
function MYTHEME_preprocess_image(&$variables) {
// Check the image style.
if ($variables['style_name'] == 'img_fluid') {
// Add class.
$variables['attributes']['class'][] = 'img-fluid';
}
}
If you want to add a class to a image field (the wrapping container), you can target a field by its name:
/**
* Implements template_preprocess_field().
*/
function MYTHEME_preprocess_field(&$variables) {
// Check for your image field.
if ($variables['element']['#field_name'] == 'field_MYIMAGE') {
// Add class.
$variables['attributes']['class'][] = 'img-fluid';
}
}
Source: How to add a class to the image tag in field.html.twig?
I've created a view that outputs a <ul> with a <time> element in each <li>. I need to convert the HTML list to a select.
So I did 2 things:
Copied item-list.html.twig and changed <ul> to a <select>
Copied time.html.twig and changed <time> to <option>
Although this works I need those 2 templates to be scoped to my view named: 'vcon-selection-table'. I tried many things but nothing works:
views-view-table--vcon-selection-table--item-list.html
views-view-table--vcon-selection-table-item-list.html
views-view-table--vcon-selection-table.html--dataset-item-list
So the question is: What template name should I use to override the templates item-list.html.twig and time.html.twig inside my view?
Is the purpose of that view to only provide that <select> element and <option> elements? Then this really isn't the way to go. Don't build a form or form elements with Views.
You'd better provide a custom form or a custom item_list in a custom block. The block then can be placed where ever you need it. There are plenty of tutorials out there. Maybe take the following as a starting point: https://valuebound.com/resources/blog/creating-a-custom-form-in-a-block-in-two-steps-in-Drupal-8
Otherwise, if you really want to continue your road I think you have to simply preprocess the list and time fields to switch to a different template according your desired conditions. I strongly guess there is no option that a custom template for a field from a node in a view will automatically be taken into account by naming patterns. You have to add that template suggestion manually first.
Snippet to be placed in MYMODULE.module file or MYTHEME.theme file.
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function MYMODULE/MYTHEME_theme_suggestions_item_list_alter(array &$suggestions, array $variables) {
// Use \Drupal::routeMatch() to get the current view ID or some other indicator.
// Sample: Get the current view ID.
$view_id = \Drupal::routeMatch()->getRouteObject()->getDefault('view_id');
// Install Devel, Kint module, then uncomment the following line and flush caches to inspect the outcome.
// ksm($view_id);
// Add the template suggestion.
// Add your template now under /templates/item-list--VIEWID.html.twig
if (!empty($view_id)) {
$suggestions[] = 'item_list__' . $view_id;
}
}
Downside of this approach is that this would trigger all item_lists that live on that exact page/view to take this template. You could fine-grain your templating much better with a custom module and a custom item_list and/or form.
Alternatively you can also move the logic to the date field and a custom view mode and an extra template for that. Then at least it stays where it belongs to.
To enable Twig debugging and getting the built-in template suggestions printed as HTML comments follow this article: https://www.drupal.org/docs/8/theming/twig/debugging-twig-templates
I am adding a page with a node-id and earlier I was using "page--node--node-id.tpl.php" template to customize the node/page. However, I accidentally deleted that node, and now I am unable to create that node with particular nid node. I want to know how to customize the specific page as each page his unique title.
Use page--node.tpl.php for all the nodes instead of each template otherwise use
node--[node-type].tpl.php for specifc node content type template
There is another smart step to create content type template with your own specific name by following below code
function themeName_preprocess_page(&$vars, $hook) {
if (isset($vars['node'])) {
$vars['theme_hook_suggestions'][] = 'page__'. str_replace('_', '--', $vars['node']->type);
}
}
In above case If node content type is "article" then the template suggestion will be "page--article.tpl.php".
Create template file page--nodetype.tpl.php in your theme directory.
I have views that shows profiles of my site within a block.
Now, I am trying to alter the title of that block through hook_block_view_MODULE_DELTA_alter.
here is the code of my custom module:
<?php
function homepagefilter_block_view_views_new_users_alter(&$data, $block) {
$data['subject'] = t('New title of the block');
}
I doen't see any effect on the block title...
Why is that?
try hook_block_view_alter().
function homepagefilter_block_view_alter(&$data, $block){
if($block->delta == 'BLOCKID'){
print_r($block);
print_r($data['subject']);
}
}
The deltas of blocks created by the Views module looks like "[view_name]-[display_name]", and the display name is something like "[type]_[number]" by default. So that means the full delta for your block is probably something like "new_users-block_1". However, you can't implement homepagefilter_block_view_views_new_users-block_1_alter(), because you can't use a hyphen in a function name. The blocks Menu module creates has a similar problem (see this issue). As Behzad says, you'll have to implement the generic hook_block_view_alter() hook for now.
I've just discovered that if you want to alter a specific page (or group of pages) all you need is to add templates file to the core templates. For instance, I need to theme my /helloword page using a page--helloworld.tpl.php and node--helloworld.tpl.php template files.
Now all I get is a blank screen so I tried to write a preprocess function that adds support for custom theme files like:
<?php
/**
* Adding or modifying variables before page render.
*/
function phptemplate_preprocess_page(&$vars) {
// Page change based on node->type
// Add a new page-TYPE template to the list of templates used
if (isset($vars['node'])) {
// Add template naming suggestion. It should alway use doublehyphens in Drupal7.
$vars['template_files'][] = 'page--'. str_replace('_', '-', $vars['node']->type);
}
}
?>
I see no syntax error but I still get a blank screen. Still no luck
Is someone able to figure out what's wrong in the code/routine?
Drupal7 + Omega Sub-Theme
Kind Regards
I think there's a tiny bit of confusion here: a template file named node--type.tpl.php will automatically be called for any node which has the type type...you don't need to add the template suggestions in yourself.
There is one caveat to this, you have to copy the original node.tpl.php to your theme folder and clear your caches otherwise Drupal won't pick it up.
Also you don't want to use the phptemplate_ prefix...rather you want your function to be called MYTHEMENAME_preprocess_page.
Your code to add the page template based on the node type looks spot on, see if you still have the problem after you change your function name and clear the caches.
Hope that helps :)