Advertisement
Guest User

Apache Solr View Modes Module

a guest
Aug 4th, 2012
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.21 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Implementation of hook_form_alter()
  5.  */
  6. function apachesolr_search_view_modes_form_alter(&$form, &$form_state, $form_id){
  7.  
  8.   /**
  9.    * Override the Apache Solr Search Page configuration form
  10.    */
  11.   if ($form_id == 'apachesolr_search_page_settings_form'){
  12.    
  13.     $view_modes = array('' => 'Use default core search result');
  14.    
  15.     $entity_info = entity_get_info('node');
  16.    
  17.     /**
  18.      * Build the view modes dropdown list
  19.      */
  20.     foreach ($entity_info['view modes'] as $machine_name => $view_mode){
  21.       $view_modes[$machine_name] = $view_mode['label'];
  22.     }
  23.    
  24.     /**
  25.      * Override form to add custom view modes dropdown
  26.      */
  27.     if ($view_modes){
  28.      
  29.       //Grab search page id
  30.       $search_page_id = $form['search_page']['#value']->page_id;
  31.  
  32.       /**
  33.        * Fetch currently save Solr view modes
  34.        */
  35.       $solr_view_modes =  variable_get('apachesolr_search_view_modes', '');
  36.      
  37.       $form['info']['page_title']['#weight'] = -2;
  38.      
  39.       $form['info']['apachesolr_search_view_mode'] = array(
  40.         '#type' => 'select',
  41.         '#title' => t('View Mode'),
  42.         '#description' => t('Select another view mode in favor of the default search view mode.'),
  43.         '#options' => $view_modes,
  44.         '#weight' => -1,
  45.         '#default_value' => isset($solr_view_modes[$search_page_id]) ? $solr_view_modes[$search_page_id] : '',
  46.       );
  47.      
  48.       //Add custom submit handler
  49.       $form['#submit'][] = 'apachesolr_search_view_modes_save_view_mode';
  50.     }
  51.   }
  52. }
  53.  
  54. /**
  55.  * Apache Solr Save View Mode Form Submit Handler
  56.  */
  57. function apachesolr_search_view_modes_save_view_mode($form, &$form_state){
  58.  
  59.   //Get Solr View Modes
  60.   $solr_view_modes =  variable_get('apachesolr_search_view_modes', '');
  61.  
  62.   $values = $form_state['values'];
  63.  
  64.   //Set View Modes
  65.   $solr_view_modes[$values['page_id']] = $values['apachesolr_search_view_mode'];
  66.  
  67.   //Save Apache Solr View Modes
  68.   variable_set('apachesolr_search_view_modes', $solr_view_modes);
  69. }
  70.  
  71. /**
  72.  * Implementation of hook_preprocess_search_results()
  73.  */
  74. function apachesolr_search_view_modes_preprocess_search_results(&$variables) {
  75.  
  76.   /**
  77.    * This will override node results if a node view mode way selected on the apachesolr page    
  78.    * configuration page.
  79.    */
  80.   $solr_view_modes =  variable_get('apachesolr_search_view_modes', '');
  81.  
  82.   /**
  83.    * Check to see if the search results page we are on has a view mode selected .
  84.    */
  85.   if (isset($variables['search_page']->page_id) AND isset($solr_view_modes[$variables['search_page']->page_id])){
  86.    
  87.     $view_mode = $solr_view_modes[$variables['search_page']->page_id];
  88.    
  89.     /**
  90.      * Iterate through results and load the node view mode instead of the default search result
  91.      * snippet.
  92.      */
  93.     if ($view_mode){
  94.       //Reset Search Results, we are going to completely override
  95.       $variables['search_results'] = '';
  96.      
  97.       foreach ($variables['results'] as $result) {
  98.         if ($result['entity_type'] == 'node'){      
  99.           $node = node_load($result['node']->entity_id);
  100.           $variables['search_results'] .= render(node_view($node, $view_mode));
  101.         }
  102.       }
  103.     }
  104.   }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement