Drupal Alters and Overrides: Drupal.theme.prototype


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

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.

Add new comment