1

I would like to add a dynamic select field where options change from time to time. New options will be added along with old options in future. I know i can add form element using hook_form_alter and save values using node api in custom table created using install file of a module.

Is there any inbuilt mechanism where i can create a field and avoid dealing with schema and schema related queries ? My only concern is field should support dynamic options in future... Something like select field values will be pulled using a query.

PS: I hope above questions is not primarily opinion based. I believe many Drupalers might have faced above situation :-) Please do add a comment if form alter approach is the right one !

  • 1
    Can you explain more how the options will change? Could you perhaps do this with a hierarchical taxonomy? – Pontus Nilsson May 20 '14 at 6:54
  • Options will be queried from a table.. More like dynamic options.. Let's say at a point of time A1, A2, A3 are the options.. Some other point of time it will be A1, A2, A3, A4 ... – Anil Sagar May 20 '14 at 6:56
  • What is A1,A2,A3,A4? Are they taxonomy terms, nodes or just values? If they are not entities in Drupal it sounds like you need a settings page that holds available values and then through a hook_form_alter you could set the available options in your form element. – Pontus Nilsson May 20 '14 at 7:08
  • They are just text values :-) Form alter is already in my list as i mentioned in my question.. I am looking for Out of the box solution where i don't need to deal with schema related operations like create/update/delete.. – Anil Sagar May 20 '14 at 7:08
  • Have a look at api.drupal.org/comment/23028#comment-23028 there a settings page is built where the values are saved in a variable_set(). These values can then be retrieved in variable_get() in your form_alter and set as #options for your form element. – Pontus Nilsson May 20 '14 at 7:11
2

You can probably do that using the field API and setting an allowed_values_function for the list field.

e.g:

/**
 * Implements hook_form_alter
 */
function MYMODULE_form_alter(&$form, &$form_state, $form_id){

  if ($form_id == 'field_ui_field_settings_form') {
    // Make the allowed_values_function editable as a textfield
    // Possible to inspect $form or $form_state to vary by field
    if(isset($form['field']['settings']['allowed_values_function'])) {
      $form['field']['settings']['allowed_values_function'] = array(
        '#type' => 'textfield',
        '#title' => t('Allowed value function'),
        '#description' => t('Returns an array of allowed values'),
        '#default_value' => $form['field']['settings']['allowed_values']['#field']['allowed_values_function']
      );
    }
  }

}

The allowed_values_function then returns an array of values for the field.

e.g:

/**
 * Allowed values callback
 */
function MYMODULE_custom_field_allowed_values($field){

  if(CONDITION){
    return drupal_map_assoc(array('A1','B1','C1'));
  }elseif(OTHER_CONDITION){
    return drupal_map_assoc(array('A2','B2','C2'));
  }

}
  • Great !! Seems like allowed_values_function property should solve the issue.. Also found a relevant tutorial here – Anil Sagar May 20 '14 at 9:35

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.