Created
          January 20, 2016 21:01 
        
      - 
      
- 
        Save labboy0276/e831c5ac32dea7352bb5 to your computer and use it in GitHub Desktop. 
    Convert Contextual Filter to another FORM API Type
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | /** | |
| * Implements hook_form_alter(). | |
| */ | |
| function k2m_profiles_form_alter(&$form, &$form_state, $form_id) { | |
| if ($form_id == 'views_content_views_panes_content_type_edit_form') { | |
| if (isset($form['arguments']['field_k2m_profile_classification_target_id'])) { | |
| // Grab the taxonomy to make the options win. | |
| $vid = taxonomy_vocabulary_machine_name_load('classification')->vid; | |
| $terms = taxonomy_get_tree($vid); | |
| foreach ($terms as $term) { | |
| $options[$term->tid] = $term->name; | |
| } | |
| // If the value is set before, explode the string to make it an array for default. | |
| if (isset($form_state['conf']['arguments']['field_k2m_profile_classification_target_id'])){ | |
| $default = explode(',', $form_state['conf']['arguments']['field_k2m_profile_classification_target_id']); | |
| } | |
| // Change the field. | |
| $form['arguments']['field_k2m_profile_classification_target_id']['#type'] = 'checkboxes'; | |
| $form['arguments']['field_k2m_profile_classification_target_id']['#multiple'] = TRUE; | |
| $form['arguments']['field_k2m_profile_classification_target_id']['#options'] = $options; | |
| $form['arguments']['field_k2m_profile_classification_target_id']['#default_value'] = $default; | |
| array_unshift($form['#submit'], '_k2m_profiles_submit_fix_checkboxes'); | |
| } | |
| } | |
| } | |
| /** | |
| * Submit handler for contextual filter. | |
| * | |
| * Problem is a checboxes passes as an array not a string, we need to format it right. | |
| */ | |
| function _k2m_profiles_submit_fix_checkboxes(&$form, &$form_state) { | |
| $flat = implode(',', $form_state['values']['arguments']['field_k2m_profile_classification_target_id']); | |
| $form_state['values']['arguments']['field_k2m_profile_classification_target_id'] = $flat; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment