How to add code between <head> and </head> in Drupal but exclude some nodes - drupal

I'm trying to add Google AdSense 'page level ads' to my Drupal website. It requires pasting some code between the two tags <head> and </head>. I want to however exclude some nodes from this. Since they are all the same node type, they will load from the same page.tpl.php file. How do I do this? Thanks.

Like everything in Drupal, there are many ways to do this. You could check for specific node ids in a preprocess function and then only add that code when the page is rendering specific node ids.
function themename_preprocess_page(&$vars) {
//check and see if we're rendering a node and if the current nid is in the a
if (isset($variables['node'] && in_array($variables['node']->nid, array('1','2','3','4','5')) {
drupal_add_js('your adsense code here',
array('type' => 'inline', 'scope' => 'header');
}
}
To make it more manageable for people, what I would do is add a checkbox field for that node type for "No ads" or something. This will give you a process for removing ads from that node from the admin interface and you won't need to constantly tinker with code to exclude certain node ids.
Now make (or add to) your themename_preprocess_page function.
//psuedo code -- don't copy and paste
theme_preprocess_page(&$vars) {
if ((isset($variables['node']->type) && $variables['node']->type == 'your_node_type') && isset($variables['node']->field_checkbox[LANGUAGE_NONE][0]) && $variables['node']->field_checkbox[LANGUAGE_NONE][0][value]) {
drupal_add_js(your javascript ad code);
}
}

Related

Drupal 8, how to disable a view during a installation profile's hook_install?

I have a custom installation profile, that has a dependency on views and other custom modules. One such module is an admin content module that has a custom view meant to replace the "content" view (/admin/content) .
In general, I'm able to disable the content view programatically. (This is on a drush file that I call with drush scr)
$view = \Drupal::service('entity.manager')->getStorage('view')->load('content');
if (!is_null($view)) {
$view->setStatus(FALSE);
$view->save();
}
However, this works if the site was installed before. If I try to run this in the install hook of my custom profile the view object is null.
function my_profile_install() {
// Previous code here
}
The view is null and I cannot disable it. I'm still not too proficient on Drupal 8 inner workings, I'm going trough code and trying to identify what could be the reason but no luck so far.
I've made sure that both the node module (this is where the standard content view is defined) and the views module are loaded before requesting for the view. It still returns empty.
Drupal::moduleHandler()->load('node');
Drupal::moduleHandler()->load('views');
I also did this
\Drupal::configFactory()
->getEditable('views.view.content')
->set('status', TRUE)
->save();
This "works". The problem is that this breaks the views listing page with an exception. So not a good fix at all.
I would appreciate if someone could point me in the right direction.
Thanks in advanced.
So, after thinking of alternatives I've come up with this logic.
Implementing hook_install_tasks and create a task that disables the view.
function my_profile_install_tasks() {
return [
'my_profile_disable_views' => [
'display_name' => 'Disabling unused views',
'display' => TRUE,
'type' => 'normal',
'run' => INSTALL_TASK_RUN_IF_NOT_COMPLETED,
],
];
}
function my_profile_disable_views() {
$view = \Drupal::service('entity.manager')->getStorage('view')->load('content');
if (!is_null($view)) {
$view->setStatus(FALSE);
$view->save();
}
}
This works without problem installing from Drush or from UI.

How to change the number of comments per page for a single node in Drupal 7?

i would like to change the comments per page for only a single node. Let's say that the defaul number of comments per page for the content type "article" is 50 and i want change this to 10 only for the article with nid=171.
$node = menu_get_object('node',1); if($node && $node->nid = 171) {......}
Any hint? Thanks.
If you want to control the number of comments on individual pages, I would suggest installing Views module which allow for easy creation of any lists of things, including comments. With Views, you can create a block with comments for any node and use that instead of regular comments block.
Also, you can have several different blocks with different number of comments, and attach whatever block you want to whatever node. Using Context module would help too, it allows to assign blocks and other elements to pages with much more sophisticated controls than Drupal core block management.
Check out can this be helpful to you:
https://drupal.stackexchange.com/questions/7450/comment-per-page-settings
Since it's a form you have to alter it and change comments number from code.
i found a solution even if a bit rude. To disable the node's default comments i used a preprocess function in template.php file of my subtheme and disbled the $content['comments'] variable used in the comment-wrapper.tpl.php template:
function MYSUBTHEMENAME_preprocess_comment_wrapper(&$vars){
$node = menu_get_object();
if($node):
if($node->type === 'forum' && $node->nid == 171):
unset($vars['content']['comments']);
endif;
endif;
}
Note that i did it for comment wrapper variables/template and not for the node vfariables/template in order to unset the comments but to keep the comment form.
Then i created a view block of comments with a filter for the nid of the comment's node.
This works well for my purposes but if you find a more elegant way let me know. :)
I spent a time looking comment.module and found this in comment_form_node_type_form_alter
$form['comment']['comment_default_per_page'] = array(
'#type' => 'select',
'#title' => t('Comments per page'),
'#default_value' => variable_get('comment_default_per_page_' . $form['#node_type']->type, 50),
'#options' => _comment_per_page(),
);
So I put that in my template.php
function THEMENAME_preprocess_page(&$variables){
if (isset($variables['node']->type)) {
if ($variables['node']->type == 'forum') {
variable_set('comment_default_per_page_' . 'forum', 4);
}
}
}
It worked for me, I hope it helps.

Use custom module to insert tags before page content on Drupal

I'm trying to create a Drupal custom module that would enclose any page or article content with specific microdata tags (among other things). I've successfully inserted code in both the header and before the closing '' tag, but when I try to enclose the page content it just jams everything together at the end of the content.
For example, here's the module code:
function my_module_page_build(&$page) {
// add manifest value to header
$manifest_code = "..."
$page['header']['manifest'] = array(
"#weight" => 0,
"#markup" => t($manifest_code)
);
// add opening microdata tags
$opening_microdata = "...";
$page['content']['my_module_open_microdata'] = array(
"#weight" => 0,
"#markup" => t($opening_microdata)
);
$closing_microdata = "...";
$page['content']['my_module_close_microdata'] = array(
"#weight" => 25,
"#markup" => t($closing_microdata)
);
// add javascript to footer
$javascript = "...";
$page['page_bottom']['my_module_javascript'] = array(
"#weight" => 0,
"#markup" => t($javascript)
);
}
I don't want to insert the closing microdata into the $page['page-bottom'] slot because I don't want some of these links and other code inside the microdata tags. I've also tried using a negative weight to move it up, but no luck.
I've tried searching for more information on Node or Blog or Article structure to see if there are other slots for the code, but I can't find anything.
I'm sure this is very easy -- are there resources I'm missing for understanding page structure? Some simple solution I'm missing?
Two potential solutions:
Try the tip from the first comment on hook_page_build, which should make your weight declarations actually take effect. (Not sure if resetting the #sorted flag might have adverse effects, though.)
Switch from using hook_page_build() to your_module_preprocess_page(&$variables), adding your microdata tags and other stuff as custom entries to the ´$variables´ array there. Then adjust your page template to place these additional variables as you please. This gives you pretty decent control over the placement of your additions, at the price of being tied to theme customizations (i.e. it works well for custom site building, but not so well if your module is intended for general usage with arbitrary themes).

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

Drupal module for webmaster block management?

The person managing a site I'm working on wants to be able to decide what blocks go where. There is already a nice interface for this in Drupal (selecting the region from a drop down) but I'd like to hide certain blocks from this user. These are blocks he should not be able to move around.
Afaik this is not possible via the Permissions. Is there a module that allows fine grained control of what blocks can be managed by whom? I'd rather not write a custom interface ...
Thanks,
Stef
Well, you can create a simple custom module like this (replace my_module with your custom module's name, obviously):
function my_module_perm()
{
return array('view special blocks');
}
function my_module_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'block_admin_display_form') {
if(!user_access('view special blocks')) {
$special_blocks = array( ); // Specially hidden blocks go here
foreach($special_blocks as $block) {
unset($form[$block]);
}
}
}
}
And then:
Add the blocks you want to hide into the $special_blocks array (it's basically the id of the block's div minus block_ )
Create a new account, and possibly a new role for this guy
Permission-wise, the new user's role should have access administration pages and administer blocks on, but shouldn't have view special blocks
Tested on Drupal 6.6, should work on other 6.x versions (and maybe 5.x with a few modifications)
Take those blocks out of regions and embed them into your template manually using module_invoke().
$block = module_invoke('module_name', 'block', 'view', 'block name or ID');
print '<h2>' . $block['subject'] . '</h2>';
print $block['content'];
Maybe give Blockqueue a try? I've never used it, but it appears to cover your use case.

Resources