0

I'm looking to change the input field widget on my form, but still accept the same value at the end of it.

I have an integer field, and I only want them to be able to select certain integers which are relative to another field in the system. Normally I would just make an integer list field, and change the safe value to the words I want them to pick. eg:

1 | Red
2 | Blue
3 | Green

However the list is constantly growing so I couldn't manually set it. I've also tried using an Entity Reference field with an Entity Reference view for the values, but I've found that it creates an error when used in conjunction with a Field Collection field in the same node form, so I'm running out of ideas.

I've written some code to give me an array of the values, keyed by the integer number I want on a per node basis which is working, but what I want is to change the input widget from a textfield to a dropdown of my allowed values, but still accept the integer key value when it's submitted.

0

Try this in a custom module...

/**
 * Implements hook_form_FORM_ID_alter().
 */
function MODULE_NAME_form_YOUR_FORM_ID_alter(&$form, &$form_state) {    

  // Replace the next 5 lines with your logic to set the array of values
  $options = array(
    1 => 'Red',
    2 => 'Blue',
    3 => 'Green',
  );    

  $form['FORM_ELEMENT_ID']['#type'] = 'select';
  $form['FORM_ELEMENT_ID']['#options'] = $options;
}

Replace MODULE_NAME, YOUR_FORM_ID and FORM_ELEMENT_ID with your module name, form id and form element key respectively.

  • Thanks, but I've tried this and it doesn't actually record the value at the end, I think there's a difference between the way select values store their information before it's added to the database to the way integer fields do – Andrew Morris Apr 29 '15 at 7:53
  • I don't think it should be different unless the select field has #multiple = TRUE, in which case it's value will be an array. One thing you can do is add a submit handler to the form ($form['#submit'][] = 'NAME_OF_FUNCTION'; and then create the function NAME_OF_FUNCTION($form, &$form_state) {dpm($form_state['values']);}, (asdsuming Devel is installed, otherwise use var_dump or similar). Run this with and without the modifications above and compare the outputs. Paste the results here if you need further help. – Paul Querol Apr 29 '15 at 8:34
  • Thanks, that's what I'm messing around with at the moment, I've got a regular integer field, and another that I'm trying to modify to see at each step how they differ. – Andrew Morris Apr 29 '15 at 8:40
  • Yea as I thought, I've tried changing one field and leaving another, the original saves the value in an array like [field_name][LANGUAGE_NONE][0]['value], and the edited one is saved as a string directly into the field key, which is why I don't think it's being saved – Andrew Morris Apr 29 '15 at 8:51
  • Then possibly adding $form['FORM_ELEMENT_ID']['#array_parents'] = array('value', 0, LANGUAGE_NONE, 'field_name'); could work. Feels a bit hacky though and if your site is multilingual or if you might reuse this code on a multilingual site then you'd definitely need to change LANGUAGE_NONE to the appropriate language code for your situation – Paul Querol Apr 29 '15 at 9:27

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.