I'm writing a module that adds a field to the create/edit forms for nodes of certain types - the actual types can be chosen by the user. There are loads of ways to identify the type of the current node from within hook_form_alter(), but is there an accepted standard way to do it? If I use $form['type']['#value'] I get "Notice: Undefined variable" warnings as my local server's error reporting is very verbose. I know I could change the error reporting level, but surely it's better practice to try and kill the bug? And if I use $form_id I have to use nodetype_node_form and convert it to nodetype - and, as an aside, I can't find a PHP function to do this cleanly. So, what's the accepted way to do this? -- Google Talk/Windows Live Messenger/AIM: james.benstead@gmail.com Yahoo! Messenger/Twitter/IRC (Freenode): jim0203 Jabber: jim0203@jabber.org // ICQ: 7088050 Skype: jimbenstead
James Benstead wrote:
And if I use $form_id I have to use nodetype_node_form and convert it to nodetype - and, as an aside,
I can't find a PHP function to do this cleanly.
Look at the example in http://api.drupal.org/api/function/hook_form_alter/6. Nancy E. Wichmann, PMP Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King, Jr.
Check $form[#node]->type. If not empty, use that string. - Ken On Dec 12, 2009, at 2:47 PM, James Benstead <james.benstead@gmail.com> wrote:
I'm writing a module that adds a field to the create/edit forms for nodes of certain types - the actual types can be chosen by the user. There are loads of ways to identify the type of the current node from within hook_form_alter(), but is there an accepted standard way to do it?
If I use $form['type']['#value'] I get "Notice: Undefined variable" warnings as my local server's error reporting is very verbose. I know I could change the error reporting level, but surely it's better practice to try and kill the bug?
And if I use $form_id I have to use nodetype_node_form and convert it to nodetype - and, as an aside, I can't find a PHP function to do this cleanly.
So, what's the accepted way to do this? -- Google Talk/Windows Live Messenger/AIM: james.benstead@gmail.com Yahoo! Messenger/Twitter/IRC (Freenode): jim0203 Jabber: jim0203@jabber.org // ICQ: 7088050 Skype: jimbenstead
Thanks Ken & Nancy - I'm now wondering how I've been writing modules for almost a year without realising the whole $node object is available from $form =). 2009/12/12 Ken Rickard <agentrickard@gmail.com>
Check $form[#node]->type. If not empty, use that string.
- Ken
On Dec 12, 2009, at 2:47 PM, James Benstead <james.benstead@gmail.com> wrote:
I'm writing a module that adds a field to the create/edit forms for nodes of certain types - the actual types can be chosen by the user. There are loads of ways to identify the type of the current node from within hook_form_alter(), but is there an accepted standard way to do it?
If I use $form['type']['#value'] I get "Notice: Undefined variable" warnings as my local server's error reporting is very verbose. I know I could change the error reporting level, but surely it's better practice to try and kill the bug?
And if I use $form_id I have to use nodetype_node_form and convert it to nodetype - and, as an aside, I can't find a PHP function to do this cleanly.
So, what's the accepted way to do this? -- Google Talk/Windows Live Messenger/AIM: <james.benstead@gmail.com> james.benstead@gmail.com Yahoo! Messenger/Twitter/IRC (Freenode): jim0203 Jabber: <jim0203@jabber.org>jim0203@jabber.org // ICQ: 7088050 Skype: jimbenstead
To survive in the medium and long term, Drupal must pay its code debt and accept an object oriented paradigm... we can't always be inventing the wheel over and over and over again at such a high cost... reflection/introspection (everything should cheaply and automatically know what it is) is part and parcel of any modern approach to programming and architecture... to show that it is perfectly possible even in PHP, see: http://www.informit.com/articles/article.aspx?p=346958&seqNum=5 Recently there was a Talmudic discussion about what pattern to label Drupal (MVC, etc., etc.). Look, the bindings of an object with the fields of a form on the presentation level is confused completely here with the persistence of the node object. It's a disaster. And I'm not sure that the "fields in core" breakthrough really solves the problem, despite its performance cost. Victor Kane http://awebfactory.com.ar http://projectflowandtracker.com "Make the capitalists pay the crisis, not the workers" On Sun, Dec 13, 2009 at 10:49 AM, James Benstead <james.benstead@gmail.com>wrote:
Thanks Ken & Nancy - I'm now wondering how I've been writing modules for almost a year without realising the whole $node object is available from $form =).
2009/12/12 Ken Rickard <agentrickard@gmail.com>
Check $form[#node]->type. If not empty, use that string.
- Ken
On Dec 12, 2009, at 2:47 PM, James Benstead <james.benstead@gmail.com> wrote:
I'm writing a module that adds a field to the create/edit forms for nodes of certain types - the actual types can be chosen by the user. There are loads of ways to identify the type of the current node from within hook_form_alter(), but is there an accepted standard way to do it?
If I use $form['type']['#value'] I get "Notice: Undefined variable" warnings as my local server's error reporting is very verbose. I know I could change the error reporting level, but surely it's better practice to try and kill the bug?
And if I use $form_id I have to use nodetype_node_form and convert it to nodetype - and, as an aside, I can't find a PHP function to do this cleanly.
So, what's the accepted way to do this? -- Google Talk/Windows Live Messenger/AIM: <james.benstead@gmail.com> james.benstead@gmail.com Yahoo! Messenger/Twitter/IRC (Freenode): jim0203 Jabber: <jim0203@jabber.org>jim0203@jabber.org // ICQ: 7088050 Skype: jimbenstead
James Benstead wrote:
I'm now wondering how I've been writing modules for almost a year without realising the whole $node object is available from $form
It took me some debugging to discover that as well. The "print_r" statement is a wonderful thing. I have never done it, but hook_form_FORM_ID_alter should also work for node forms. Nancy E. Wichmann, PMP (781) 697-6344 Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King, Jr. From: development-bounces@drupal.org [mailto:development-bounces@drupal.org] On Behalf Of James Benstead Sent: Sunday, December 13, 2009 8:49 AM To: development@drupal.org Subject: Re: [development] Node types in hook_form_alter() Thanks Ken & Nancy - I'm now wondering how I've been writing modules for almost a year without realising the whole $node object is available from $form =). 2009/12/12 Ken Rickard <agentrickard@gmail.com> Check $form[#node]->type. If not empty, use that string. - Ken On Dec 12, 2009, at 2:47 PM, James Benstead <james.benstead@gmail.com> wrote: I'm writing a module that adds a field to the create/edit forms for nodes of certain types - the actual types can be chosen by the user. There are loads of ways to identify the type of the current node from within hook_form_alter(), but is there an accepted standard way to do it? If I use $form['type']['#value'] I get "Notice: Undefined variable" warnings as my local server's error reporting is very verbose. I know I could change the error reporting level, but surely it's better practice to try and kill the bug? And if I use $form_id I have to use nodetype_node_form and convert it to nodetype - and, as an aside, I can't find a PHP function to do this cleanly. So, what's the accepted way to do this? -- Google Talk/Windows Live Messenger/AIM: james.benstead@gmail.com Yahoo! Messenger/Twitter/IRC (Freenode): jim0203 Jabber: jim0203@jabber.org // ICQ: 7088050 Skype: jimbenstead No virus found in this incoming message. Checked by AVG - www.avg.com Version: 9.0.716 / Virus Database: 270.14.105/2561 - Release Date: 12/12/09 14:39:00
participants (4)
-
James Benstead -
Ken Rickard -
Nancy Wichmann -
Victor Kane