Drupal Alters and Overrides: Drupal.theme.prototype

Tags: 

Drupal.theme.prototype was introduced in Drupal 6, meant for modules producing HTML content within JavaScript, allowing that this content to be overridden.

As a real world example, the Node relationships module produces a link via jQuery that, on mouse over, pops up the words Search and reference.... Each dynamically generated link has it's alt tags created in this way.

To change this link without modifying the module directly, you can override the text in your own theme's template.php file.

The original code in Node relationship's node_form.js file contains:

// Install the "Search and reference" button.
    if (fieldOptions.searchUrl) {
      var $searchButton = $(Drupal.theme('nodeRelationshipsReferenceButton', 'search', Drupal.t('Search and reference...')));
      $buttonsWrapper.append($searchButton);
...

Notice Drupal.theme namespace above. What Drupal.theme is doing is passing a few parameters; the first, nodeRelationshipsReferenceButton, is the name of our actual theme function which contains:

/**
* Theme the specified button for an autocomplete widget.
*/
Drupal.theme.prototype.nodeRelationshipsReferenceButton = function(type, title) {
  return '';
};

The other parameters being passed are type and title.

In your theme's template.php file, you could use the code below to change the text to Search.

Drupal.theme.nodeRelationshipsReferenceButton = function(type, title) {
  if (type == 'search') {
    title = 'Search';
  }
  return '';
};

The only real difference between these functions is the removal of the prototype namespace.

Drupal Alters and Overrides: hook_form_alter

Tags: 

One of the first rules most people coming into the world of Drupal learn, hopefully, is to not hack core. I certainly didn't know about this when I first started out and hacked to pieces a lovely Drupal 4.6 site which, amusingly enough, still exists today. Since then, though, I've become more accustomed to the various ways of altering and overriding things in both Drupal core and contrib "the Drupal way".

Because Drupal is built modular with a strong hook API system, it gives us a lot of power in terms of changing things through our own custom modules. You can read more about creating a custom module at http://drupal.org/developing/modules -- there is also a useful module with some good examples you can download from http://drupal.org/project/examples

I'm going to go through a few different ways you can alter and override content in my next series of blog posts. This first one will cover one of the most popular.

hook_form_alter

One of the most common hooks used to override or alter things in Drupal is hook_form_alter. Drupal is driven by forms-- administration settings forms, content creation forms, contact forms, menu forms, user registration forms, etc., that I don't think I've ever worked on a Drupal site where this hook wasn't used.

Let's say, for example, you've got yourself a View with some exposed filters. By default the submit button says "Apply", but you've been asked to change that to "Submit".

My custom module is called demo. If I take a look at the link above from api.drupal.org, it tells me the parameters my function should have:

hook_form_alter(&$form, &$form_state, $form_id)

This is how it would appear in my module:

function demo_form_alter(&$form, &$form_state, $form_id) {

}

I need to find the $form_id in order to make sure I'm altering only that specific form and not the others. I usually do this just by printing out $form_id. For Views exposed filters, they all have the same $form_id which is 'views_exposed_form'. In order to make sure I'm only targetting this specific exposed form, I also use the $form['#id'] value. You can get these values by doing a print_r($form) or dpm($form) in your module. My alter now looks like this:

/**
 * Implementation of hook_form_alter().
 */
function demo_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'views_exposed_form' && $form['#id'] == 'views-exposed-form-content-search-page-1') {
    $form['submit']['#value'] = t('Search');
  }
}

I can do all sorts of things with forms here. I can remove elements, rename them, add new ones. I can also add or override submit and validation functions. Here are a few examples:

/**
 * Implementation of hook_form_alter().
 */
function demo_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'views_exposed_form' && $form['#id'] == 'views-exposed-form-content-search-page-1') {
    // Rename the submit button
    $form['submit']['#value'] = t('Search');
  }
  if ($form_id == 'search_block_form') {
    // Add default "Search" text in the searchbox
    $form['search_block_form']['#default_value'] = t('Search');
  }
  if ($form_id == 'page_node_form') {
    // Remove the "Preview" option
    unset($form['buttons']['preview']);
    // Add my extra validation function
    $form['#validate'][] = 'demo_extra_validate';
  }
}

Importing Old Blogs

I've spent a bit of time today importing my old blogs here-- if cutting and pasting could be considered "importing", anyway.

I was very surprised and happy today to find that I actually did not lose all the content from my old blog. Older than my last blog, which content I have not re-posted yet (as there was little), but of my last last blog when I actually blogged. Apparently this was back in 2008, as the dates show. Nonetheless, I decided to repost them for prosperity's sake, plus I still get lots of hits for the links. Just if you do read them, take note they are older, possibly deprecated, and that I'm a bit older and hopefully a bit wiser these days.

So I apologize for the formatting of the older posts. I wonder if Drupal 7 / Gardens has something to make the code show up properly and colored... hmmm.

Live on Drupal Gardens

My site is finally live on Drupal Gardens, by following the directions at http://www.drupalgardens.com/documentation/custom-domains

Pretty straight forward and propagated for me within an hour. Now, I shall watch more Netflix and imagine the great blogs to come, because I don't have to worry about keeping my site updated or doing any maintenance!

Pages

Subscribe to alligatorsneeze.com RSS