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

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.

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

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

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;

Drupal: force regions to always render?

I'd like to always render all regions. Even if they don't contain any blocks.
I render my regions like this (in page.tpl.php):
<?php print render($page['region_name']); ?>
Here's the code I'm using, but this has no effect.
function theme_name_page_alter(&$page) {
$regions = system_region_list($GLOBALS['theme'], REGIONS_ALL);
foreach ($regions as $region => $name) {
if(empty($page[$region])) {
$page[$region] = array();
}
}
}
You can make sure Drupal renders every region with the following code (in a custom module):
function hook_page_alter(&$page) {
foreach($page as $region => $blocks) {
if(is_array($blocks) && in_array($region, array('region_1', 'region_2', 'region_3'))) {
if(count($blocks)==1) {
$page[$region]['phantom_content']['#markup'] = ' ';
}
}
}
}
You will need to replace region_1, region_2, region_3 with the names of your regions that you want to make sure are always rendered.
To explain the code a little, if the count of the blocks array is 1 then it means it is empty as it will always contain the #sorted attribute.
It's not just that you don't have any blocks assigned to that region, but almost certainly because the render array is empty. Keep in mind what the render function does-- it calls drupal_render on any/all elements & children in the render array, which converts them to an html string for output. If there are no renderable elements, it doesn't return any html.
The correct (programmatic) way of rendering these regions would be to define a render array for each region, setting the #markup element to whatever html you want Drupal to output there. This would have to be done in your custom module.
If you need to do this from the gui only, I don't see any way other than defining a phantom block. In which case you should probably re-consider what it is you're trying to accomplish.
I think the answer lies in the region.tpl.php file and associated template_preprocess_region() function.
The template file checks that there is a valid variable called $content available, which is loaded up from the preprocess function. If that $content array is empty, the condition will fail and no markup will be rendered (which will happen if the region contains no blocks).
Try adding a copy of region.tpl.php to your theme, removing the if ($content): condition, and then flushing Drupal's cache.

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).

Why isn't $vars['node'] available in preprocess_page for some content types?

I am currently using drupal 6 for a site I'm working on. I have a MYTHEME_preprocess_page() function that adds a few variables to the page.tpl.php template from the taxonomy and from a cck field. It was working correctly for a bit, and then the $vars['node'] is empty, but only for 2 content types. The 'node' variable is available to the preprocess_page function in other content types.
I thought it was a problem with using the following code, but when I remove all of this, the 'node' variable is still empty.
function mytheme_preprocess_node(&$vars, $hook) {
$function = 'mytheme_preprocess_node'.'_'. $vars['node']->type;
if (function_exists($function)) {
$function(&$vars);
}
}
Does anyone know of any gotchas or bugs that might be removing the 'node' variable? I can't seem to figure out where I'm going wrong. I'm at a loss.
Here is my complete mytheme_preprocess_page() function.
function mytheme_preprocess_page(&$vars, $hook) {
if ($hook == 'node' || $hook == 'page') {
if (is_object($vars['node'])) {
// grab the header image if it exists to make it avaialble to the content header
$vars['header_image'] = _mytheme_get_header_image($vars);
// get the taxonomy term to put in the content header
if (count($vars['node']->taxonomy) > 0) {
$vars['tax_term'] = "<div class=\"terms\">" . _mytheme_get_first_taxonomy_term($vars['node']->taxonomy) . "</div>";
}
// add the teacher's credentials to the content header
if ($vars['node']->field_credentials[0]['view'] != '') {
$vars['teacher_credentials'] = '<span class="teacher-creds">' . $vars['node']->field_credentials[0]['view'] . '</span>';
}
}
}
}
After going through and disabling modules one-by-one, I determined that the problem is related to the module, node_breadcrumb. A similar issue was filed here: http://drupal.org/node/616100#comment-2199374
In the 3rd comment, you'll see a link to another issue with a resolution
For others that run into this, I had the same issue as a result of using the jQuery UI module. Disabling and re-enabling fixed it, and I could not track down the specific issue, but it appeared to be related to $static variables in some path check functions.
To others that stumble their way into here, I suggest you pull some of the more obvious modules right out of the module folder on your dev setup, see if things change, and then put them back in there until you figure it out.
Another option is to search for instances of _preprocess_page(, $variables['node'] and $vars['node'] to see if some contributed code is unwittingly unsetting a node when it shouldn't be.

Resources