-
Show Menu Description Field in Node Add/Edit Form
Many components in Drupal have an optional title or description field. This text usually displays when a user hovers over an item. We had an interesting request: a client wanted to have a box that displayed text which would change every time you roll over a menu item. This can be accomplished using just a small amount of jQuery, and Drupal’s built-in menu handling. The menu system in Drupal can handle descriptions on each menu item, however you have to do this through the menu admin. The problem is that the client needed to be able to edit these descriptions at will.
-
Views Taxonomy: Get Terms with Associated Nodes
This example serves as both an example of how to alter a Views2 query, as well as how to use the get_terms_by_count() function I’ve written.
Unfortunately there is not (at present) a Views2 taxonomy filter that lets you “Get only terms with at least X associated nodes.” We had a client request that terms without associated nodes be hidden. This was actually more complex than it sounds, but the solution led me to a whole new level of Views2 understanding. Views2 has a hook called hook_views_query_alter() that lets you alter a Views2 query before it is executed. This is exactly what we needed to do in order to only pull terms with associated nodes. Specifically, we needed to add an additional WHERE clause to the query.
-
Rebuilding the Drupal Navigation Menu (Automatically)
The following steps will get you a completely rebuilt Navigation menu.
- Clear your cache
- Backup your database
- In the database, remove any records in {menu_links} where menu_name = navigation
- Create a file somewhere in your site’s directory structure
- Add this code to that file and then save it
123456789<?php//uncomment the next two lines to automatically use the admin user (not advised)//global $user;//$user-?-->uid = 1;require_once './includes/bootstrap.inc';drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);menu_rebuild();?> - Tell Drupal to ignore this file using this method.
- Browse to the file in your web browser (it will run quickly, with no indication of completion). Wait for the page to stop loading, then check your navigation menu settings in the admin.
- When finished, delete or make-inaccessible the PHP file you just made!
-
$user Conditionals
This is one example of performing an action based on whether or not the current user belongs to a certain role or roles. Note that the admin user (UID #1) matches as well.
12345678global $user;$roles = array_values($user->roles);if (in_array('admin', $roles)) || in_array('developer', $roles)) || $user->uid == 1) {print $content;}else{header("location:/not-allowed");}Note: You may want to try using drupal_goto() (or drupal_access_denied() in this case) instead of a header redirect.
-
Passing Arguments to Drupal Blocks
You can pass any number of arguments to a Drupal block by providing default arguments. The screenshot below shows what an argument might look like in Views2.
-
Open All External Links in a New Window/Tab using jQuery
123$(document).ready(function() {$("a[href^='http']").not("[href*='mysite.com']").attr('target','_blank');});
123$(document).ready(function() {$("a[href^='http']").not("[href*='mysite.com'],[href*='mysite2.com']").attr('target','_blank');});