Drupal Articles

Drupal: Add current user's role to body classes

If you'd like to change your site's appearance based on the current user's role, you can easily add the current user's role to the $body_classes array.

Just add this snippet to the yourTheme_preprocess_page function in your template.php file.

Drupal 6:

Escape Views' replacement patterns

Views replacement patterns are great, but they take up valuable variable real-estate. If you need to literally output "[nid]" rather than the value of its corresponding replacement pattern, you might start hitting your head against the wall.

I found this issue to be particularly annoying when using the Prepopulate module, which required CCK field names (with bracketed arrays) to a URL.

Never fear! Rather than using "[nid]" in your views text, try using percent encoding rather than literal brackets. The result would be:

%5Bnid%5D

Using percent encoding with stop views from replacing the replacement pattern with a dynamic value.

Add a Drupal template suggestion for Panels: page-panel.tpl.php

Drupal has a standard array of template suggestions that let you specify which TPL file should be used according to node type, node id, etc. However, there is no default template suggestion for pages generated by Panels.

You can easily fix that by adding a little snippet to your template.php files' preprocess_page() function.

Link to search page with advanced search fieldset expanded!

Create the link:

<a href="/search#advanced-search" class="advanced-search">Advanced Search</a>

Or you could even add it to the search box with jQuery (I'm adding it to a Custom Search block here):

/* jQuery to expand advanced search field is contained in js_injector rule (in db) */
$('Advanced Search').appendTo('.block-custom_search_blocks .form-item');

Then, add a jQuery script. I used JS Injector to add this only to the 'search*' pages.

//wait until the rest of the jQuery has been loaded
$(document).ready(function() {
//break apart url to get anchor
var url = document.location.toString();
if (url.match('#')) {
var anchor = '#' + url.split('#')[1];
}

Downgrading from Views 6.x-3.x-dev to 6.x-2.x

You may notice that after downgrading, you receive and error like:

Unknown column 'view_php' in 'field list' query:..sites/all/modules/views/includes/view.inc on line 1722.

If so, backup your database and then run this mySQL query:

ALTER TABLE views_view ADD is_cacheable tinyint;
ALTER TABLE views_view ADD view_php blob;

Worked for me!

Apart from that, be sure that you've clear all of the Header, Footer, and Empty Text fields from all of your views.

Creating a dynamic menu link in Drupal

Sometimes you'll need to create a menu link that is different for each user that views it. The simplest way to do this is to create a small custom module that will add the menu link and generate a dynamic URL.

In my case, I wanted a link title 'My Company' that would redirect the user to their own Organic Group.

What to do

  • Create a menu entry
    <?php
    function grasmash_menu() {
     
    //create menu item for My Company
     
    $items = array();

Disable all drupal non-core modules for extreme troubleshooting.

drush pm-list --type=module --status=enabled | sed '/\((.*)\)/!d;/[Cc]ore.*-.*[Rr]equired/d;s/.*(\(.*\)).*/\1/' | sort > noncoremod.txt
drush pm-disable $(cat noncoremod.txt)

Also, drush has an shell alias located in drushrc.example.php that will do this for you:

# $options['shell-aliases']['dis-all'] = '!drush -y dis $(drush pml --status=enabled --type=module --no-core --pipe)';

Drupal View: shows teasers of all menu children for current node

I tossed around a lot of strategies for accomplishing this, and in the end I managed to find a module that does most of the hard work for me.

Goal

To create a node which displays the teasers of all its menu children (other nodes nested under current node's menu entry).

Overwrite Drupal jQuery behaviors on specific pages

I recently ran into a situation that required me to disable a jQuery function on one specific Drupal page. I searched for keywords like "detach drupal behavior" and "disable jQuery function in drupal," but to no avail. So I came up with a fairly simple workaround that uses the JS Injector module.

I've used this to Disable Beauty Tips, Compact Forms, and even Drupal's core password checker on specific pages.

First, I found the function that I wanted to overwrite. In my case, I wanted to disable the jQuery generated by the Compact Forms module on a specific webform. So I cracked open the compact_forms js file and found the jQuery function that actually does the magic: "Drupal.behaviors.compactForms()"

Pages