Hi all,

- In http://drupal.org/node/93960 I read that the form id for node editing screens is always [content type]_node_form.
- I have a content type called e-letter .

I am trying to programmatically add something to the editing form, just to get my hands on this hugely important form_alter() function. But nothing works.

This is the function I would like to implement:

function ?????????????($form_id, &$form) {
$form['warning'] = array(
  '#value' => t('Yep, this is made with form_alter'), 
  '#weight' => -10
);
}

My problem: what name should this function have??? I tried the silliest combinations: function e_letter_node_form_form_alter($form_id, &$form)
e-letter_node_form_form_alter($form_id, &$form)
e_letter_node_form_alter($form_id, &$form)
e-letter-node_form_alter($form_id, &$form) etc. etc.

But either nothing happens, or I get the dreaded blank screen.
1) So, what am I doing wrong? What name should my function have if my content type is called "e-letter"?
2) And does it matter where I put that function? I have several forms happily running from "settings.php", maybe not the proper place, but still, it works. So, I figured I could put this function there too.

Comments

modul’s picture

Sorry to bump this, folks, but I would really like to get this working... Any ideas, anyone?

heine’s picture

I've moved your post to the support forum.

2) And does it matter where I put that function? I have several forms happily running from "settings.php", maybe not the proper place, but still, it works. So, I figured I could put this function there too.

You need to write a module.

From the form_alter function signature, I derive that you are writing against Drupal 5.x

You need to replace the hook in hook_form_alter with the name of your module.

Regarding the name of the form:

The machine-readable name of this content type. This text will be used for constructing the URL of the create content page for this content type. This name must contain only lowercase letters, numbers, and underscores.

function mymodule_form_alter($form_id, &$form) {
  if ($form_id == 'e_letter_node_form') {
    // Do something.
  }
}

--
The Manual | Troubleshooting FAQ | Tips for posting | How to report a security issue.

modul’s picture

Thanks for your reply, Heine! But do I really have to write a whole module if all I want to do is add one line of text to a given editing form, plus install it, have it run through the module admin section, apply access rights etc. etc., just for one measly line of extra text somewhere on an existing form???

Can't this be done with the function I wrote in my first posting? And if so, what name should it have? Because in your reply, you automatically go for the module name, but I would like to tuck it in a nice small function, together with my other functions. Can my simple line of text also be added like that?

TapocoL’s picture

You do not need to do that much if all you want the function to do is form_alter.

Make mymodule.info http://drupal.org/node/101009

Make mymodule.module and all you need in the file is the function mymodule_form_alter($form_id, &$form) http://api.drupal.org/api/function/hook_form_alter/5

Thats all. Then, just go install it in admin/build/modules

Plus, I recommend putting all of your other functions that were talked about in this module.

-Craig Jackson
-Web Developer

modul’s picture

Hi again, Heine. I followed your advice and wrote a teeny-weeny module, just for adding that one line of text. And lo and behold: it worked :-)

Still, it looked like overkill to me, but then I realized that I can use that same module for all kinds of interventions in all kinds of forms. So, it will probably be the best solution after all.

Thanks again!

mr.andrey’s picture

Hi there,

I just thought I would add my two cents into this discussion. Here's what I use to set default values on a form.

in template.php:

function chatroom_form_alter($form_id, &$form) {
  if ($form_id == 'event_node_form') {
  	if (preg_match('/^\/node\/add\/event\/chat/',$form['#action'])) {
  		$form['category']['259']['#default_value'] = 628;
  		$form['category']['65']['#default_value'] = 68;
  	}
  }
}

So when someone navigates to /node/add/event/chat, this gets triggered and default values for location and type get set.

Best,
Andrey.

modul’s picture

Clever bit of code, Mr. Andrey, thanks for the hint!

missym’s picture

subscribe

modul’s picture

I'm afraid I still don't feel it. With some guessing and trying I did manage to get some form changes done with my content types, but there is still a whole area where I feel lost. Finding the form id to look for when you want to change something is still not at all clear to me. So I'm going to ask to walk me through another situation again.

Suppose this (admittedly rather silly) situation. I have the form for the theme settings, http://mysite.com/?q=admin/build/themes . Suppose I want to add this simple name field right before the pictures of the various available themes, to make the visitor write his name. The purpose is nil, but that doesn't matter, I would just like to see how it is done - and understand why it would work.

function XXXXXXXXXX() {
$form = array();
$form['lastnaam'] = array(
     '#type' => 'textfield',
     '#size' => 50,
     '#title' => 'Your last name',
     '#description' => t('Please write your last name.'),
);
return $form;
}

- What would come on the XXXXXXX????
- Where would I put this function???

Sorry to bug you all with this, but I do want to feel comfortable with this form business, and that is not the case At All. So, please...?

heine’s picture

function XXXXXXXXXX() {
$form = array();
$form['lastnaam'] = array(
     '#type' => 'textfield',
     '#size' => 50,
     '#title' => 'Your last name',
     '#description' => t('Please write your last name.'),
);
return $form;
}

XXXXX() would be an implementation of hook_form_alter, so you need a module. Let's call it: mymodule.

The function name would then become mymodule_form_alter with the signature:

function mymodule_form_alter($form_id, &$form) {
 // Do stuff.
}

This function is called for all forms on the site so you need to check $form_id to see if you received the form you want to alter.

To get help finding out you can use http://drupal.org/project/forminspect, or just print_r form_ids in your form_alter implementation which would give you system_themes as the form_id.

The function then becomes:

function mymodule_form_alter($form_id, &$form) {
  if ($form_id == 'system_themes') {
   // This would put the lastnaam element at the bottom of the form.
   // Depending on what you want to do insert it in the right part of the array, var_dump $form to see where.
    $form['lastnaam'] = array(
     '#type' => 'textfield',
     '#size' => 50,
     '#title' => 'Your last name',
     '#description' => t('Please write your last name.'),
    );
  }
}

--
The Manual | Troubleshooting FAQ | Tips for posting | How to report a security issue.

modul’s picture

Thanks so much, Heine, those were the instructions I was craving for!! I have the feeling I can find my own way now in Form World. Drupal's Form API is an admirable piece of work, but it soooo much lacks a set of truly practical examples, the stuff you wrote in your reply. Really, Thanks!

tborrome’s picture

Hi, just a quick follow up question on using form_id's. is there a generic form_id for all content types I can use in the hook_form_alter rather than having to specify cases for each of my content types. I want to apply a change for all content types.

I tried using case 'node_form' but that did not work. It works is I specify each specific content type ... case 'event_node_form', case 'forum_node_form'...

heine’s picture

The form_id is a string; you can use any string processing function PHP has. Alternatively check the top level #id tag ("node-form") of the form.

--
The Manual | Troubleshooting FAQ | Tips for posting | How to report a security issue.

tborrome’s picture

thanks. i was able to use preg_match to do the string check.

quick followup - how do you get the top level #id tag?

inuninu’s picture

Hi,
I've read a lot about customizing forms in D5 now and played around with some modules like formfilter and devel.forminspect.
After all i wrote a module to hide some unwanted elements in the editform of a special content type as shown above, but some elements still shown up, when I visit the node edit/add form and I don't know why. I am able to hide the title, the body (which of course are just the fields, that I don't want to hide. It also works with logs and options.
The elements, that I cannot hide in the form are: comment_settings, menu, attachements, path, pathauto_perform_alias and flag.
I also got problems to hide elements inside e.g. taxonomy. I can hide taxonomy itself, but what do I have to do, if I only want to hide Key7(e.g. "Cars") and Key9("Monkeys") from key=taxonomy. It would be great if somebody can help me.

Here's the module and the drupal version is 5.16:

function mymodule_form_alter($form_id, &$form) {
  if ($form_id == 'story_node_form') {
    $form['flag']['#type'] = 'hidden';
    $form['menu']['#type'] = 'hidden';
    $form['taxonomy']['#type'] = 'hidden';
    $form['log']['#type'] = 'hidden';
    $form['attachements']['#type'] = 'hidden';
    $form['author']['#type'] = 'hidden';
    $form['comment_settings']['#type'] = 'hidden';
    $form['options']['#type'] = 'hidden';    
    $form['path']['#type'] = 'hidden';
	}
}
inuninu’s picture

who can help me?

Panaromic.webdesign.com’s picture

You can also hide it in css file. This is what I use to hide comment_setting field:

.node-form fieldset.collapsible {
display: none;
}

You may want to be a little more 'specific' to avoid 'display: none;' on elements that you don't want to hide.

sheldonkreger’s picture

Using the Devel module, one can implement a generic hook_form_alter() containing a dpm() which displays a list of forms on the page.

function mymoudule_form_alter(&$form, &$form_state, $form_id) {
  dpm($form_id);
}

Now, any page you visit on your site will show a list of all the form_id's used on that page. If you don't see any info, double check your permissions for the Devel module. This is an easy way to find form IDs.

timonweb’s picture

I've just created module which lets you find form id easily http://drupal.org/project/get_form_id