home resources search newsjoinmembers: 6958
PHP Flash Java Ruby Windows Linux
Hiveminds | Sun, 2007-08-26 11:20  tags: , ,

This a step by step for overriding a form element that is built by one module and then changed by another.

The module doing the overrride must be loaded after the originating module and its form. You can do this by naming the override module to load after the originator. Modules are loaded in alphabetical order unless set by weight. All modules have a default weight of 0. To change this go into the stystem table of the database, find the rows with the modules to be used and change the weights to a numerical order of 1 and 2.

The code for the override is standard but in order to make anything happen you will have to learn the names of the form elements by looking at the HTML code. This is where the Drupal documentation fails becausse it does not tell you how to properly get the names of the elements.

The easiest way is to look at the source code. When getting the names though you have to be careful to not include the prefixes that are added in by the system. The system usually adds in "edit-" to a form element. You must also remove any "+" characters in names. These are added to satisfy the web browsers need to concatenate names with spaces.

Here is a screenshot of the form I am going to change by activating a module called "profilecloud" what it is add freetagging terms to the description of the textarea.

This the default look after the profile textarea has been created by using the profile module. You may want to experiment with the creation of profile forms before attemping to override them via antoher module. This will give you and idea of what to expect when you look at the source code.

In the above image you will see two sections. The top section the the source code of the profile page that has the form. The second section is the PHP code in your module that uses hook_form_alter(). Here I am going to change the description text of the textarea. You do this by entering the names of the form, the profile category and textarea into the function as an array element. The arrows show where the text should be placed. As mentioned earlier you have to follow the naming rules for the function which are different from those used the make up the form. Here is a snippet of the exact code I used to make the change.

<?php

$tags 
profile_list_list();
  if (
$form_id == 'user_edit') {
    
// Check whether we are at the right page
    
if (isset($form['Medlemskonventet 2006'])) {
      
// Modify
      
$form['Medlemskonventet 2006']['profile_matchmaking_2006']['#description'] = '
      <p>En lista p&aring; dina intressen inf&ouml;r medlemskonventet 2006. Anv&auml;nd nyckelord och undvik stora bokst&auml;ver. Skriv varje intresse p&aring; en egen rad eller separera dem med ett kommatecken. Ingen HTML &auml;r till&aring;ten.</p>
      <p><span style="font-size:10px">Klicka i nedanst&aring;ende lista f&ouml;r att v&auml;lja av befintliga intressen:</span><br>'
.$tags.'</p>';
      
// Or unset, all works
      //unset($form['Personal']['profile_firstname']);
    
}
  }
?>

After adding in function profile_form_alter with the right code to the profileclould module and then activating it it looks like this.

printer-friendly version

Hiveminds's picture
This article brought to you by the Hiveminds Magazine - Staff. Contact us if you want to post an article or announcement anonymously
Thoughtbox - So what did you think?



Hiveminds posted on: Fri, 2007-03-02 09:02.

It seems that the undocumented way of capturing the form elements in the node add form is to use [conten type]_node_form. Example:

<?php

/**
* Implementation of hook_form_alter().
*/
function flashgame_form_alter($form_id, &$form) {
  if (
$form_id == 'flashgame_node_form'){
      unset(
$form['title']);
  }
}
?>

a Visitor posted on: Fri, 2007-08-24 16:28.

Thanks, this is a very useful tip. The name "alter" is misleading in that currently it is mainly used for adding elements rather than altering them.

Another easy way to "learn the names of the form elements" is to add a print_r to view the array:

function modulex_form_alter($form_id, &$form) {
  print_r '<pre>' . ($form) . '</pre>';
  ...
}

or you could narrow it down more:

function modulex_form_alter($form_id, &$form) {
  print_r '<pre>' . ($form['Medlemskonventet 2006']) . '</pre>';
  ...
}

a Visitor posted on: Fri, 2007-08-24 16:46.

Actually it should looklike this...

function modulex_form_alter($form_id, &$form) {
  print '<pre>';
  print_r ($form);
  print '</pre>';
  ...
}

 
 
Content Management Systems Drupal Wordpress Content Management Systems Joomla! Adobe Flex Web Developers
 videos
 articles
 blogs
 comments
 downloads
sitemap

Newsletter

Get updates on Hiveminds services, articles and downloads by signing up for the newsletter.

Editor's choice

Some of the better articles, stories and tutorials found at Hiveminds.

Find more

Find more of Hiveminds articles, stories, tutorials and user comments by searching.




Picked links

Hand picked websites and articles from around the web that provide quality reading.

page top