'admin/settings/calais/calais-tagmods', 'title' => t('Calais Tag Modifications'), 'description' => t('Configurations for Calais Tag Modifications'), 'callback' => 'drupal_get_form', 'callback arguments' => array('calais_tagmods_settings'), 'type' => MENU_LOCAL_TASK, ); } return $items; } /** * Build the admin settings form. */ function calais_tagmods_settings() { $form = array(); $form['calais_tag_blacklist'] = array( '#type' => 'textarea', '#title' => t('Calais Blacklist'), '#default_value' => variable_get('calais_tag_blacklist', NULL), '#rows' => 6, '#description' => t('A comma-separated list of terms to be removed from the Calais response. Example: funny, bungee jumping, "Company, Inc.".'), ); $form['calais_tag_substitution'] = array( '#type' => 'textarea', '#title' => t('Calais Substitutions'), '#default_value' => variable_get('calais_tag_substitution', NULL), '#rows' => 6, '#description' => t('A list of substitutions. One option per line. Key-value pairs may be entered separated by an equals. i.e. Calais Term=Some Local Substitution'), ); $form = system_settings_form($form); return $form; } /** * Implementation of hook_calais_preprocess. * * Make sure that a vocabulary exists for all entities returned, if not, create it. */ function calais_tagmods_calais_preprocess($node, &$keywords) { _calais_tagmods_apply_blacklist(&$keywords); _calais_tagmods_apply_substitutions(&$keywords); } /** * Filter out unwanted keywords */ function _calais_tagmods_apply_blacklist(&$keywords) { $bl_string = variable_get('calais_tag_blacklist', NULL); $blacklist = calais_explode_tags($bl_string); foreach ($keywords as $cat => $terms) { $diff = array_diff($terms, $blacklist); $keywords->$cat = array_values($diff); } } /** * Substitute keywords with others. */ function _calais_tagmods_apply_substitutions(&$keywords) { $sub_string = variable_get('calais_tag_substitution', NULL); $rows = explode("\n", $sub_string); $subs = array(); foreach ($rows as $row) { $row = trim($row); if (!empty($row)) { preg_match('/^(.*)=(.*)$/', $row, $matches); $subs[$matches[1]] = $matches[2]; } } foreach ($keywords as $cat => $terms) { foreach ($terms as $key => $term) { if (array_key_exists($term, $subs)) { $terms[$key] = $subs[$term]; $keywords->$cat = $terms; } } } }