search_config.module in Search configuration 7
Same filename and directory in other branches
Main functionality for the Search Configuration module.
Features: a) Content type search are govened by permissions. b) Individual advanced search components are toggled using the main search form settings. These may have role settings applied. c) Filters for content types field d) Grouping options for content types field e) Title label overrides f) Title display options
@todo g) Local menu tab label overrides OR find the native method of doing this h) Categories field i) Search result limits
@todo (maybe, depends on scale) j) Add node level exclude from search options k) Add new fields to the search form
File
search_config.moduleView source
<?php
/**
* @file
* Main functionality for the Search Configuration module.
*
* Features:
* a) Content type search are govened by permissions.
* b) Individual advanced search components are toggled using the main search
* form settings. These may have role settings applied.
* c) Filters for content types field
* d) Grouping options for content types field
* e) Title label overrides
* f) Title display options
*
* @todo
* g) Local menu tab label overrides OR find the native method of doing this
* h) Categories field
* i) Search result limits
*
* @todo (maybe, depends on scale)
* j) Add node level exclude from search options
* k) Add new fields to the search form
*/
/**
* Helper function to get the settings.
*/
function search_config_node_settings() {
$settings = variable_get('search_config', array());
$settings += array(
'forms' => array(),
'fields' => array(),
'restrictions' => array(),
'results' => array(),
);
$settings['forms'] += array(
'toggle_forms' => 0,
// Show adv if both forms are present
'move_keyword_search' => 0,
// Move keyword search into adv form
'advanced_populate' => 0,
// Try and repopulate the adv form
'advanced_expand' => 'default',
// Control the presentation of adv form
// Controls all 3 'Containing...' fields.
'remove_containing_wrapper' => 'default',
);
$settings['fields'] += array(
'containing_any' => array(),
'containing_phrase' => array(),
'containing_none' => array(),
'types' => array(),
'category' => array(),
'language' => array(),
);
$settings['results'] += array(
'limit' => '10',
);
$settings['restrictions'] += array(
'admin_bypass' => 1,
'remove_advanced' => 0,
);
foreach ($settings['fields'] as $field => $info) {
$settings['fields'][$field] += array(
'remove' => 0,
// Hides the field
'roles' => array(),
);
if ($field == 'types') {
$settings['fields'][$field] += array(
'filter' => array(),
// Content type to HIDE
'groupings' => array(),
);
}
// @todo: What features do we need here?
if ($field == 'category') {
$settings['fields'][$field] += array(
'filter' => array(),
// Vocabs to HIDE
'widget' => 'textfield',
);
}
}
return $settings;
}
function search_config_string_overrides($key = NULL) {
$overrides = variable_get('search_config_string_overrides', array());
$overrides += array(
'labels' => array(
'basic' => '',
'basic_with_keys' => '',
'basic_submit' => '',
'advanced_fieldset' => '',
'advanced_fieldset_with_keys' => '',
'advanced_any' => '',
'advanced_phrase' => '',
'advanced_none' => '',
'advanced_type' => '',
'advanced_language' => '',
'advanced_submit' => '',
),
'title_display' => array(
'basic' => 'default',
'advanced_any' => 'default',
'advanced_phrase' => 'default',
'advanced_none' => 'default',
'advanced_type' => 'default',
'advanced_language' => 'default',
),
'menu' => array(
'node' => '',
'user' => '',
),
);
if (isset($key)) {
return array_filter($overrides[$key]);
}
return $overrides;
}
/**
* Implements hook_permission().
*/
function search_config_permission() {
$permissions = array(
'admin node search exclusions' => array(
'title' => t('Exclude nodes from search results'),
'description' => t('This allows users with this permission to flag content as not to be searched.'),
),
'search all content' => array(
'title' => t('Search all content types'),
'description' => t('Allow users to search any content, overriding content type permissions.'),
),
'search all excluded entities' => array(
'title' => t('Search excluded content'),
'description' => t('Allow users to search any content, overriding individual content item exclusion settings.'),
),
);
foreach (search_config_content_types() as $type => $label) {
$permissions["search {$type} content"] = array(
'title' => t('%type_name: Search content of this type', array(
'%type_name' => $label,
)),
);
}
return $permissions;
}
/**
* Implements of hook_query_node_access_alter().
*/
function search_config_query_node_access_alter(QueryAlterableInterface $query) {
global $user;
// Provides a tag option to bypass any changes.
if ($query
->hasTag('bypass_search_configuration')) {
return;
}
$search = FALSE;
$node = FALSE;
foreach ($query
->getTables() as $alias => $table) {
if ($table['table'] == 'search_index') {
$search = $alias;
}
elseif ($table['table'] == 'node') {
$node = $alias;
}
}
if ($node && $search) {
// Only conditionally restrict the query if not admin (user.uid = 1).
if ($user->uid != 1) {
if (!user_access('search all content')) {
$excluded_content_types = array();
foreach (search_config_content_types() as $type => $label) {
if (!user_access("search {$type} content")) {
$excluded_content_types[] = $type;
}
}
// A core bug results in a DB error if we use the following: Ref: #1210072
// $query->condition($node . '.type', array($excluded_content_types), 'NOT IN');
if (!empty($excluded_content_types)) {
$db_and = db_and();
foreach ($excluded_content_types as $type) {
$db_and
->condition($node . '.type', $type, '!=');
}
$query
->condition($db_and);
}
}
if (!user_access('search all excluded entities')) {
// Join into the {} table to see if it is excluded.
$query
->leftJoin('search_config_exclude', 'sc', $node . '.nid = sc.entity_id AND sc.entity_type = :type', array(
':type' => 'node',
));
$query
->isNull('sc.entity_id');
}
}
if (get_class($query) == 'PagerDefault' || is_subclass_of($query, 'PagerDefault')) {
$settings = search_config_node_settings();
if (!empty($settings['results']['limit'])) {
$query
->limit($settings['results']['limit']);
}
}
}
}
/**
* Implement hook_theme().
*/
function search_config_theme() {
return array(
'search_config_admin_label_form' => array(
'render element' => 'form',
'template' => 'templates/search-config-admin-labels-form',
),
);
}
/**
* Implements hook_node_search_results().
*
* The only reason for this hook is to record if there are any active
* search results found.
*/
function search_config_node_search_result($node) {
global $search_config_node_results;
$search_config_node_results[$node->nid] = $node->nid;
}
/**
* Implements of hook_form_FORM_alter().
*/
function search_config_form_search_form_alter(&$form, $form_state) {
// Update the node search form.
if (isset($form['module']['#value']) && $form['module']['#value'] == 'node') {
module_load_include('inc', 'search_config', 'search_config.node');
_search_config_set_string_overrides($form, $form_state);
if (isset($form['advanced'])) {
$settings = search_config_node_settings();
if (!empty($settings['restrictions']['remove_advanced'])) {
$form['advanced']['#access'] = FALSE;
}
else {
_search_config_advanced_form($form, $form_state);
}
}
}
}
/**
* Implements hook_form_FORM_alter().
*
* This hooks into the main administration search form to append the additional
* configuration options.
*/
function search_config_form_search_admin_settings_alter(&$form, $form_state) {
module_load_include('inc', 'search_config', 'search_config.admin');
_search_config_form_search_admin_settings_alter($form, $form_state);
$form['#submit'][] = 'search_config_search_admin_settings_alter';
}
/**
* Additional submit handler to save the additional fields added by the form alter.
*/
function search_config_search_admin_settings_alter(&$form, $form_state) {
variable_set('search_config_string_overrides', $form_state['values']['search_config_string_overrides']);
variable_set('search_config', $form_state['values']['content_node_search_config']);
}
/**
* Helper function to get an array of safe to use content type names.
*/
function search_config_content_types() {
return array_map('check_plain', node_type_get_names());
}
/**
* Helper function to test the users configured access rights to a field.
*
* @param bool $remove
* A flag to block access.
*
* @param array $grant
* An additional array of roles to overrule the main flag
*/
function search_config_get_access($remove, $grant) {
global $user;
if ($remove) {
return (bool) array_intersect_key($user->roles, array_filter($grant));
}
return TRUE;
}
/**
* Helper function for the installation, upgrades and permission overviews.
*/
function search_config_get_roles_by_permission($permission) {
return db_select('role_permission', 'p')
->fields('p', array(
'rid',
))
->condition('permission', $permission)
->execute()
->fetchCol();
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function search_config_form_node_form_alter(&$form, $form_state) {
if (!user_access('admin node search exclusions')) {
return;
}
$node = $form['#node'];
$default_value = 0;
if (!empty($node->nid)) {
$default_value = (int) db_query_range('SELECT 1 FROM {search_config_exclude} WHERE entity_id = :id AND entity_type = :type', 0, 1, array(
':id' => $node->nid,
':type' => 'node',
))
->fetchField();
}
$element = array(
'#type' => 'checkbox',
'#title' => t('Exclude from search'),
'#default_value' => $default_value,
);
// Align with normal publishing options if possible.
if (isset($form['options']) && (!isset($form['options']['#access']) || $form['options']['#access'])) {
$form['options']['search_config_exclude'] = $element;
}
else {
$form['search_config_exclude'] = $element;
// Pushes the element to sit just above the vertical tabs.
if (!empty($form['additional_settings']['#weight'])) {
$form['search_config_exclude']['#weight'] = $form['additional_settings']['#weight'] - 1;
}
}
}
/**
* Implements hook_node_insert().
*
* Inserts the excluded flag for both node insert and update hooks.
*/
function search_config_node_insert($node) {
if (!empty($node->search_config_exclude)) {
$record = array(
'entity_type' => 'node',
'entity_id' => $node->nid,
'exclude' => 1,
);
drupal_write_record('search_config_exclude', $record);
}
}
/**
* Implements hook_node_update().
*
* Deletes any existing records and calls search_config_node_insert() to insert
* any required rows.
*/
function search_config_node_update($node) {
if (isset($node->search_config_exclude)) {
db_delete('search_config_exclude')
->condition('entity_id', $node->nid)
->condition('entity_type', 'node')
->execute();
search_config_node_insert($node);
}
}
Functions
Name![]() |
Description |
---|---|
search_config_content_types | Helper function to get an array of safe to use content type names. |
search_config_form_node_form_alter | Implements hook_form_BASE_FORM_ID_alter(). |
search_config_form_search_admin_settings_alter | Implements hook_form_FORM_alter(). |
search_config_form_search_form_alter | Implements of hook_form_FORM_alter(). |
search_config_get_access | Helper function to test the users configured access rights to a field. |
search_config_get_roles_by_permission | Helper function for the installation, upgrades and permission overviews. |
search_config_node_insert | Implements hook_node_insert(). |
search_config_node_search_result | Implements hook_node_search_results(). |
search_config_node_settings | Helper function to get the settings. |
search_config_node_update | Implements hook_node_update(). |
search_config_permission | Implements hook_permission(). |
search_config_query_node_access_alter | Implements of hook_query_node_access_alter(). |
search_config_search_admin_settings_alter | Additional submit handler to save the additional fields added by the form alter. |
search_config_string_overrides | |
search_config_theme | Implement hook_theme(). |