Drupal 8 - adding body class based on taxonomy term or other - drupal

Building a site in Drupal 8, using classy subtheme. Run into a puzzling theming issue - adding body class to html.html.twig based on a taxonomy term on that node.
Themers use this to customize page display, in my case using it to define a few sections of my site so I can change color and format.
I have tried some preprocess functions I saw on google but to no result.
Has anyone else run into and solved this issue?

Use this to get all fields of the node and check for whatever you need:
\Drupal::service('current_route_match')->getParameter('node')->toArray();
In your .theme file you can use the html preprocess hook:
function your_theme_preprocess_html(&$variables) {
$body_classes = [];
$nodeFields = \Drupal::service('current_route_match')->getParameter('node')->toArray();
// if something, then set $body_classes to something.
$variables['attributes']['class'] += $body_classes;
}
And then in your html twig template add the attributes to the body element:
<body{{ attributes }}>
Hope this helps.

With the answer of Frank Drebin I get a PHP fatal error (Unsupported operand types) with the "+=" operand. If you want to add the node ID and node type to your body class, you can use this code for example:
// Add the node ID and node type to the body class
$body_classes = [];
$nodeFields = \Drupal::service('current_route_match')->getParameter('node')->toArray();
if (is_array($nodeFields) && count($nodeFields) > 0) {
if (isset($nodeFields['nid'])) {
$body_classes[] = 'node-' . $nodeFields['nid'][0]['value'];
}
if (isset($nodeFields['type'])) {
$body_classes[] = $nodeFields['type'][0]['target_id'];
}
}
$variables['attributes']['class'] = $body_classes;

Related

how to add fields from referenced node drupal 8

I have a content type that contains a field that is a reference to another node. I'm trying to include a field from the referenced node within the page for the main node, but I can't figure out how to add that. Here's how I'm getting the value within my theme:
function mytheme_preprocess_node(array &$variables) {
$node = $variables['node'];
if ( $node->get('field_testimonial') ) {
$referenced_nodes = $node->get('field_testimonial')->referencedEntities();
if ( count($referenced_nodes) > 0 ){
$referenced_node = $referenced_nodes[0];
//this is providing the value I want. how can I add that back to my page?
error_log($referenced_node->body->value);
}
}
}
Please help me add that value back to my variables so I can use it in my theme! Thank you for your help.
Just do the following
$variables['referenced_body'] = $referenced_node->body->value;
In your Twig-Template you can do this:
{{ referenced_body }}
Why are you doing with code ? it can configure form admin
Go to admin/structure/types/manage/article/display and manage your desire format to display the reference node.
Thanks

Add class to an image field for a specific content type in Drupal 7

Currently I'm using the code as suggested in this answer. Which is the following:
function simalr_preprocess_image(&$variables) {
if ($variables['style_name'] == 'request-background') {
$variables['attributes']['class'][] = 'pixastic';
$variables['attributes']['class'][] = 'pixastic-blurfast(amount=1)';
}
}
This works fine except for the fact that I get the following error message on a page which doesn't have an Image with the 'request-background' style:
Notice: Undefined index: style_name in simalr_preprocess_image() (line 46 of /var/www/vhosts/simalr.com/httpdocs/sites/all/themes/simalr/template.php).
I only want this piece of code used on a specific content type (namely 'request'). In which way do I have to adjust the code in my template.php file in order to just use it on a page which is only of a certain content type?
You can still work with your code but use isset function. This will remove the warning.
If you want to do it just for a specific content type use menu_get_object function in drupal. This function will return the node for you if it's a node page.
Example:
$node = menu_get_object();
if ($node->type == 'story') {
// TODO
}
Hope this helps.

Template drupal in full HTML

Is there a way to build a drupal template for a content type which not import the tag and the begin of the tag ?
in exemple, i would create a "newsletter" content type which is based on a "page--newsletter.tpl.php" as it tell here : http://drupal.org/node/1089656
But all the xxx.tpl.php i ever seen not include the top of the HTML code
Is there a way for it ?
*EDIT : * I'm trying to change the html.tpl.php to another when the $node->type is equal to "newsletter"
(look easier like that)
function themename_preprocess_html(&$vars, $hook) {
if ($node = menu_get_object()) {
array_splice($vars['theme_hook_suggestions'], -1, 0, 'html__' . str_replace('-', '_', $node->type));
}
#print_r($vars['theme_hook_suggestions']); // print out suggestions
}

How can I customize a specific node in Drupal 6 WHEN a custom template has already been applied to the node's content type?

[For Drupal 6] Let's say I've created a content type called "my_content_type". I can override the default template for that entire content-type by creating "page-node-my_content_type.tpl.php". But, what would be the best way to then further customize a single node of that content type (e.g., node 5555)?
I tried the following, but none worked:
page-node-5555.tpl.php
page-node-my_content_theme-5555.tpl.php
node-5555.tpl.php
None of these work. They all continue to use my original content-type template.
Drupal's page templates work on a suggestion system. Based on the current URL, an array of possible template files is created. It loops through the array (in reverse order) looking for template files that exists. The first one it finds, it will use.
drupal's theme system provides a hook for you to modify the template suggestions.. open up your template.php and find
function phptemplate_preprocess_page(&$vars) {
the $vars variable is what contains the suggestions, specifically $vars['template_files']
By default the only page suggestions that are available are
page.tpl.php
page-node.tpl.php
page-node-[node_id].tpl.php
As far as im aware, page-node-[node_type].tpl.php does not work by default, so its likely you have already modified the preprocess_page template to added in this functionality.
However if you want to add more specific templates you could do something like this...
function phptemplate_preprocess_page(&$variables) {
if ($variables['node']->type != "") {
$variables['template_files'][] = "page-node-" . $variables['node']->type;
$variables['template_files'][] = "page-node-" . $variables['node']->type . "-" . $variables['node']->nid;
}
}
this will allow the following hierarchy of template suggestions
page.tpl.php
page-node.tpl.php
page-node-[node_id].tpl.php
page-node-[node_type].tpl.php
page-node-[node_type]-[node_id].tpl.php
In Drupal 7 just copy the page.tpl.php template and rename it as
page--node--[node:id].tpl.php
Clear cache and start tweaking..
function phptemplate_preprocess_page(&$variables) {
if ($variables['node']->type != "") {
$variables['template_files'][] = "page-node-" . $variables['node']->type;
$variables['template_files'][] = "page-node-" . $variables['node']->type . "-" . $variables['node']->nid;
}
}
This code should not work because hook_preprocess_page() does not get passed any node information. hook_preprocess_node() does. So you can easily create a custom node.tpl, but you cannot easily create a custom page.tpl for a specific node. Not that I've been able to figure out anyway :)
Later...
In default Drupal, page-node-NID.tpl.php will work with no special coding. On a site of mine, it wasn't working, however, and I used the following code to make it work:
/**
* Implementation of hook_preprocess_page().
*/
function MYMODULE_preprocess_page(&$variables) {
// Allow per-node theming of page.tpl
if (arg(0) == 'node' && is_numeric(arg(1))) {
$variables['template_files'][] = "page-node-" . arg(1);
}
}

Get $node variable in html.tpl.php - Drupal 7

I'm trying to allow users to update head titles and meta descriptions for each page. I thought that an easy way to achieve this would be to add a field to the 'Basic page' content type for the page title, then check if that field is not empty in html.tpl.php and if it is not, override $head_title with this user-defined value.
However, it appears that the $node variable is not available in html.tpl.php. Can anyone suggest a way for me to make this data available in this template file, or alternatively, alter $head_title before it is sent to html.tpl.php? Thanks for reading.
Taken in part from this thread that I found: http://drupal.org/node/1041768...
In your template.php, you can do the following:
function yourtheme_preprocess_html(&$variables) {
// If on an individual node page, add the node type to body classes.
if ($node = menu_get_object()) {
$variables['head_title'] = $node-> // find your cck field here
}
}
Bit messy, but would work:
if(arg(0) == 'node' && !empty(arg(1))) {
$node = node_load(arg(1));
}
However, you might prefer http://drupal.org/project/metatags_quick (an interrim module until the full http://drupal.org/project/metatags is finished).

Resources