Index: apachesolr_search.module =================================================================== --- apachesolr_search.module (revision 569) +++ apachesolr_search.module (revision 570) @@ -499,6 +499,7 @@ $facets['is_book_bid'] = array( 'info' => t('Apache Solr Search: Filter by Book'), 'facet_field' => 'is_book_bid', + 'content types' => variable_get('book_allowed_types', array('book')), ); } @@ -511,6 +512,7 @@ $facets[$delta] = array( 'info' => t('Apache Solr Search: Filter by taxonomy @name', array('@name' => $vocab->name)), 'facet_field' => $delta, + 'content types' => $vocab->nodes, ); } } @@ -524,6 +526,7 @@ $facets[$field['field_name']] = array( 'info' => t('Apache Solr Search: Filter by @field', array('@field' => $field['label'])), 'facet_field' => apachesolr_index_key($field), + 'content types' => $field['content types'], ); } } @@ -566,6 +569,10 @@ return; } + if (!apachesolr_block_visibility($query, 'apachesolr_search', $delta)) { + return; + } + // Get information needed by the taxonomy blocks about limits. $initial_limits = variable_get('apachesolr_facet_query_initial_limits', array()); $limit_default = variable_get('apachesolr_facet_query_initial_limit_default', 10); Index: apachesolr.module =================================================================== --- apachesolr.module (revision 569) +++ apachesolr.module (revision 570) @@ -651,6 +651,101 @@ } /** + * This code makes a decision whether to show a block or not. + * @param $query + * The current query object. + * @param string $module + * The module's name to whom this block belongs. + * @param string $delta + * The delta string the identifies the block within $module. + * @return boolean + * Whether the block should be visible. Other factors, like + * the block system's visibility settings, apply as well. + */ +function apachesolr_block_visibility($query, $module, $delta) { + // TYPE HIERARCHY. + // If the block is configured to heed type hierarcy then it looks + // to see if a suitable type filter has been chosen. If not, + // the function returns. + // This variable is not static cached because variable_get() already does that. + $type_filters = variable_get('apachesolr_type_filter', array()); + if (isset($type_filters[$module][$delta]) && $type_filters[$module][$delta] == TRUE) { + $facet_info = module_invoke($module, 'apachesolr_facets'); + if (isset($facet_info[$delta]['content types'])) { + $has_filter = $query->get_filters($facet_info[$delta]['facet_field']); + $show = count($has_filter); + foreach ($facet_info[$delta]['content types'] as $content_type) { + if ($query->has_filter('type', $content_type)) { + $show = TRUE; + } + } + if (!$show) { + return FALSE; + } + } + } + return TRUE; +} + +/** + * Implementation of hook_form_[form_id]_alter(). + * + * Hide the core 'title' field in favor of our 'name' field. + * + * Add a checkbox to enable type-specific visibility. + */ +function apachesolr_form_block_admin_configure_alter(&$form, $form_state) { + // Hide the core title field. + if ($form['module']['#value'] == 'apachesolr' && $form['delta']['#value'] != 'sort') { + $form['block_settings']['title']['#access'] = FALSE; + } + + // Add a type-specific visibility checkbox. + $module = $form['module']['#value']; + $delta = $form['delta']['#value']; + $enabled_facets = apachesolr_get_enabled_facets(); + + // If this block isn't enabled as a facet, get out of here. + if (!isset($enabled_facets[$module][$delta])) { + return; + } + + $facet_info = module_invoke($module, 'apachesolr_facets'); + + // If this facet doesn't define any content type array, get out of here. + if (!isset($facet_info[$delta]['content types'])) { + return; + } + + $settings = variable_get('apachesolr_type_filter', array()); + // Set up some variables for the verbiage of the form element. + $count = count($facet_info[$delta]['content types']); + $types = format_plural($count, t('type'), t('types')); + $are = format_plural($count, t('is'), t('are')); + $content_types = implode(', ', $facet_info[$delta]['content types']) . '.'; + $form['block_settings']['apachesolr_type_filter'] = array( + '#type' => 'checkbox', + '#title' => t('Show this block only when the type filter is selected for: %content_types', array('%content_types' => $content_types)), + '#default_value' => isset($settings[$module][$delta]) ? $settings[$module][$delta] : FALSE, + '#description' => t('This filter is relevant only for specific content types. Check this to display the block only when the type filter has been selected for one of the relevant content types.'), + '#weight' => 11, + ); + // Add a submit handler to save the value. + $form['#validate'][] = 'apachesolr_block_admin_configure_submit'; +} + +function apachesolr_block_admin_configure_submit($form, &$form_state) { + if (isset($form_state['values']['apachesolr_type_filter'])) { + $settings = variable_get('apachesolr_type_filter', array()); + $module = $form_state['values']['module']; + $delta = $form_state['values']['delta']; + unset($settings[$module][$delta]); + $settings[$module][$delta] = $form_state['values']['apachesolr_type_filter']; + variable_set('apachesolr_type_filter', $settings); + } +} + +/** * Helper function for displaying a facet block. */ function apachesolr_facet_block($response, $query, $module, $delta, $facet_field, $filter_by, $facet_callback = FALSE) { @@ -1329,17 +1424,6 @@ } /** - * Implementation of hook_form_[form_id]_alter(). - * - * Hide the core 'title' field in favor of our 'name' field.. - */ -function apachesolr_form_block_admin_configure_alter(&$form, $form_state) { - if ($form['module']['#value'] == 'apachesolr' && $form['delta']['#value'] != 'sort') { - $form['block_settings']['title']['#access'] = FALSE; - } -} - -/** * Returns a list of blocks. Used by hook_block */ function apachesolr_mlt_list_blocks() {