I have created a content type and the form looks like this:

Home team: Reds
Away team: Blues
Batted first: Reds
Batted second:
Fielded first:
Fielded second:

If Reds batted first, I know the Blues batted second, fielded first and that the Reds fielded second. How can I get Drupal (7) to populate these fields automatically via some simple logic?

I'm thinking the Computed Field module, but the values are not in the database yet: the form hasn't been submitted. So, I'm not sure I can access the Batted first value. Perhaps only via the &$form_state array? - but can the Computed Field module access this array?

Anyway, how can I acheive this? Any suggestions are welcome. Thanks.

  • r u talking about node/add page? – monymirza Mar 27 '13 at 4:44
  • Yes, sorry, node/add/my-content-type. – 24ma13wg Mar 27 '13 at 13:09
  • u can do with some magic of jquery. – monymirza Mar 27 '13 at 13:53
  • Interesting. So I need jQuery listening for the value of 'Home team', 'Away team' and 'Batted first' not being null, and when all three values are entered populating the other input elements via some simple logic. Is that what you're suggesting? – 24ma13wg Mar 27 '13 at 19:54

Module Conditional Fields does the job.

It lets you assing values, visibility etc. of fields depending on other fields in the same form.



You'll have to resort to javascript (jQuery) for this one like monymirza suggested in the comments. Reason being that if the inserted values are not submitted to the server then Drupal/PHP don't know about their existence which means that the task must be solved client-side.

The general approach could be that you add jQuery's change or blur event handler to the the "Batted first" field. When it's triggered you grab the "Batted first" field value and compare it to "Home team" and "Away team". If a match is found you can fill "Fielded second" with that and "Batted second" + "Fielded first" with the other value.

  • I'm looking at the priciples of the drupal_process_states function (see api.drupal.org/api/drupal/includes%21common.inc/function/…). Most of the examples are about making something visible if a radio button is checked, but this drupal function seems to access DOM elements and then do something - which is the crux of what I want to do. – 24ma13wg Mar 31 '13 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.