Hi,

I want to add a section to the standard node editing form that will allow a user to add a category (taxonomy term) on the fly.

I will add the additional fields and submit button via form_alter. I would like the "Add category" button to submit to a new function (not the standard edit node _submit function) which will just add the category and go back to the node editing form with the message "Category added".

So after adding the category the user would be back at the node editing form with any previously entered values persisted though not submitted.

Is this possible in the forms api? How?

Is there already functionality in Drupal 6 to achieve adding categories on the fly?

Thanks

Comments

drawk’s picture

Sorry, this doesn't address your main question, but I'm curious: why not use free tagging with multiple values enabled? Then the user can add as many terms as they like right on the node add/edit form without needing to even click any buttons ..

barful’s picture

Because free tagging does not allow assignment of a parent term.

On my web site the taxonomy hierarchy is an important part of the primary and secondary navigation and is visible to the user. The root level terms form the primary navigation and the child terms form the secondary navigation once you have drilled down to a parent term through the primary navigation links

See www.shootingseeds.co.uk to see what I mean. I made this in Wordpress and I'm now porting it to Drupal. Garden/Pond/Seeds are the root level terms and also the primary navigation links

barful’s picture

There seems to be a fundamental problem with what I want to do here. I have defined an additional submit button to handle adding a category in the edit node form. The button looks like this :

      $form['addcategory']['addcategory_submit'] = array(
        '#type' => 'submit',
        '#value' => $yes ? $yes : t('Add category TEST'),
        '#validate' => array('mytest_addcategory_validateform'),
        '#submit' => array('mytest_addcategory_elmstest'));

Unfortunately the '#required' => TRUE fields in the main edit node form prevent my submit handler being called even though it wants nothing to do with the main form's fields. For example the "Title field is required" validation error is created - adding a category has nothing to do with the title

Do I have to create a validate handler that sets '#required' => FALSE, to all fields I'm not interested in?

Am I missing something?

barful’s picture

Sod it I'll have to do it a different way. In plain php code it would be easy to do this but of course with a framework come advantages and disadvantages

drawk’s picture

Could you perhaps put the term creation logic into a custom validation hanlder? If you can ensure that your validation handler gets called before the regular validation handler, you could add the term and return false without setting any form errors. That should return the user to the form with all their form fields still populated ..

barful’s picture

Hi Drawk,

I array_merge()d my validation function so it appeared at [0] and made my validation function do this :

  $_SESSION['messages'] = null;
  drupal_set_message('Your category has been added. NOT!');
  return false;

Despite returning false the subsequent validate functions ran, eg; node_validate(), and looking at form_execute_handlers() it looks like returning false in a validate function doesn't do anything in Drupal 6 - it would be nice if returning false prevented subsequent handlers from being called. I also found that other values were not persisted such as the title, body etc.

I can feel a strange sensation; "giving up"!

In June 2008 I ditched my own ignorant attempts at writing a cms to concentrate on Drupal and it's tough when I can't make it do what I want. The obvious answer is "make it do something that it can do". It would be easy to do this in an ajax call but it wouldn't work without javascript which is a must

All I want is for a submit button that has its own #submit and #validate functions to bypass the rest of the form's #validate and #submit functions and for all form values to persist.

I will have to rethink my approach and may have to reduce the role that categories play in my IA, though it pains me right to my desparate core to say it.

I'm screaming inside - right now. Like a child

Unless I change Drupal core, in which case I'm screaming inside like several children rather than one

Anonymous’s picture

What about a non-submitting button from hook_form_alter sending the field input to a custom script. This example, http://blue.live4all.co.il/~mooffie/cms/node/15, is something like I want to do but I am stuck since I'm translating it all for D6.
I need to work out how to make the non-submitting button validate = FALSE, so it can refresh nicely. If this would work for you, I would appreciate a look at your end product.

steve

barful’s picture

That would rely on javascript which I don't want to force on the user base. I have abandoned this for now and I've put it on the "bad list" which I will return to at the end of the project - it gets it out of my head

I read somewhere that the Drupal usability group are working on exactly the same thing so I might hold out until then, unless it's Drupal 7

EDIT: It does work without javascript I see - but it still requires a post so we're back where we started

computer_jin’s picture

I have same issue that i want to add category using fieldset but when i submit any sub category it shows other require fields of Main form ..... plzz tell me how can i do this .....i have spent on it more time and it is very urgent to complete this .........

Thanks

--
Azhar uddin
Technical Lead
email : engr.azharuddin@gmail.com
skype : computer_jin