Modify dropdown in node edit form

In Drupal I have defined a content type with a Year-Month date field in it. Content of this type has two drop downs on the edit form, one for Year and one for Month.
How can I change the Month drop down to show:

Jan-Feb
Mar-Apl
May-Jun
..

The values for each month range should be the same for the initial month in the range, i.e. 01, 03, 05, etc  (I assume that these are the values for the months concerned in the normal Month drop down).



bogormanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

stephencolsonCommented:
Is there data in any of these fields yet? If so, the web UI won't let you change the definition in D7. I don't specifically recall about D6, but it is likely that won't work too. If there is not content, just go to the manage fields tab for that content type, and edit the field in question to have a different set of allowed values.

When entering allowed values, the format is key|value where key is what gets stored in the DB (the month number) and value is what gets displayed to people through the web UI (month name range). That thing in between is the pipe character.

If you already have content in, then your two options are: 1) to export all the content, delete the field and recreate it how you would like, and then reimport in to the new field or 2) make the changes directly against the database manually (only if you really know what you are doing).
0
stephencolsonCommented:
After re-reading your question, it isn't 100% clear if you created the year and month pickers, or if you were using the date widget and it is providing the year and month pickers. My answer was geared towards you having actually defined them. If you are using a date widget, you would need to use the form api (hook_FORM_ID_form_alter) to intercept that page and change the value for those fields (and unset the ones you don't want).

To alter the output, you would need to create a field formatter for CCK
0
bogormanAuthor Commented:
Hi Stephen,
thanks for your rapid reply.   The dropdowns appear after creating the content type and adding a date field.   Under Manage Fields I configured this field and selected Year-Month granularity, so I suppose I need to use the form api.
I am a relative newcomer to Drupal but have a fairly broad experience of programming though I have never used php.   Where do I use hook_FORM_ID_form_alter? Do I add it to a template.php file in my theme subdirectory? Can you give me a link to any examples of this technique?
Presumably you mean by output the data which is fed to the underlying database date field?
Brian
0
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

bogormanAuthor Commented:
Sorry, have just read your previous post.   I use D6 and there are a lot of nodes of this content type. Suspect I will need to export and then import after making the changes.  
0
bogormanAuthor Commented:
One further point.    The key-value in the database should not change for any of the nodes, e.g. If a node date field contains:

2004-05-01T00:00:00    (corresponding to the month May-June but at present selected as May).

It is only necessary to change the dropdown value to May-June, the key remaining 05.

Does this make it easier, or do I still need to export, edit the form, and then import?


0
stephencolsonCommented:
The best source for API things in Drupal is at api.drupal.org. All of their form api ref material is over here: http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6

It looks like I transposed the name of the hook you wanted. It is hook_form_FORM_ID_alter: http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_form_FORM_ID_alter/6

The hook functions in drupal are really easy to implement. Simply create your own module (yourmod.info and yourmod.module files required) and add in to yourmod.module a function like this:

function yourmod_form_CONTENTTYPE_node_form_alter(&$form, &$form_state){
  //do something

}

Open in new window


To make sure your bases are covered, only drupal core modules should really go in to the /modules directory. Everything else is better suited to go in to sites/all/modules, sites/default/modules, or sites/YOURSITEURL/modules. So put them there :-)

Using this form_alter is only going to change how it appears on the node/add or node/edit forms. You would need to implement the cck formatter for output if you care that it would show only JAN instead of JAN-MAR in the body of the node or in views after it is saved because it only really stores the key to the DB.
0
stephencolsonCommented:
re your key, it would actually make it more difficult to do what you suggest. It is a lot harder to merge the values but not actually merge the keys. You might as well store the proper key in to the DB and make your life easier everywhere else.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bogormanAuthor Commented:
Thanks, Stephen.  Guess I will have to export the data, make the changes and then import the data?
One further point. I know I have seen the allowed values box before but I cannot see it now. To test I have taken an unused content type, added a date field using the date type and select list but on the next page there is no allowed values box. Cannot understand it. Have done this several times. Must be making an obvious mistake. Can you suggest a solution?
Brian
0
bogormanAuthor Commented:
Hi Stephen,
Realised that I had to create a text field, not a date one, to use the allowed values.
Would appreciate, however, your views on whether I should just export the data, delete all the node data, change the content type's date field and then import.
One further point. The date key-values have two forms:

2004-05-01T00:00:00  and
2004-05-00T00:00:00

Think the 01 days were in the original data imported when I set up the (test) site and the 00 ones are created when saving the Year-Month nodes. I find, when I edit and resave a node the day is 00. Presumably it doesn't matter which format exists in the database if I use this format?  What do you think?
Brian
0
bogormanAuthor Commented:
Hi Stephen,
Thanks so much for all your help.   If you could just comment on my last queries, I would be grateful.
Thanks again.
Brian
0
stephencolsonCommented:
Brian,

Since you aren't using day granularity, it technically doesn't matter, but from a data validation standpoint, I would personally recommend having a valid date (eg 2004-05-01). This way you can be sure that any date manipulation you want to do in code at least behaves predictably. I know in some systems having a day that is either less than or greater than the "allowed values" for its month get interpreted as the previous or next month instead (Jan 31 + 1 = Jan 32 = Feb 1).

Hope that helps!
0
bogormanAuthor Commented:
Strange. In my case when I save the node I get a zero day!
Do you think I can just change the Month control on the form of the content type to Text (with the allowed values set) as the key (the value in the database should not change) or do you think I ought to export/import?
Brian
0
stephencolsonCommented:
Export/import is the best thing to do from a data consistency and validation standpoint. Or even better is to create a second field that actually does what you want and write a php script that moves content from one field to the other in the correct format.
0
bogormanAuthor Commented:
Thanks again Stephen. You have provided some really helpful advice.
Brian
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Drupal

From novice to tech pro — start learning today.