array( 'arguments' => array('vid', 'terms'), ), ); } /** * Alter the standard node edit form to remove calais taxonomies so that they are * only editable via the Calais tab. * TODO: Make this configurable so the user can specify how/where they want to edit calais terms. */ function calais_form_alter(&$form, $form_state, $form_id) { // Node edit form if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) { $node = $form['#node']; $vocabs = calais_get_vocabularies($node->type); foreach ($vocabs as $vocabulary) { _calais_disable_taxonomy_field($form['taxonomy']['tags'][$vocabulary->vid]); } } // Display Calais GUI on Taxonomy Term Editor if it exists else if ($form_id == 'taxonomy_form_term'){ $term = $form['#term']; if(!empty($term['guid'])) { $form['identification']['guid'] = array( '#type' => 'markup', '#prefix' => '
', '#value' => l($term['guid'], $term['guid'], array('attributes' => array('target' => '_blank'))), '#suffix' => '
', '#weight' => 10, ); } } } /** * This function implements what is essentially disabling of a taxonomy field, * but leaving it on the form so that values can be recreated by * taxonomy_node_save() upon save. */ function _calais_disable_taxonomy_field(&$field) { $field['#type'] = 'hidden'; $field['#maxlength'] = CALAIS_TERM_MAXLENGTH; } /** * Render the form for the Calais taxonomy & suggestions */ function calais_keywords_form(&$form_state, $node) { drupal_set_title(t('Calais Terms: ') . $node->title); $path = drupal_get_path('module', 'calais'); drupal_add_css("$path/calais.css"); drupal_add_js("$path/calais.js", 'module'); $vocabs = calais_get_vocabularies($node->type); $form = array(); $form['#node'] = $node; $form['nid'] = array('#type' => 'value', '#value' => $node->nid); $form['vid'] = array('#type' => 'value', '#value' => $node->vid); foreach ($vocabs as $vocabulary) { $keywords = calais_get_keywords($node->nid, $node->type, $vocabulary->vid); $suggestions = theme('calais_suggestions', $vocabulary->vid, $keywords[$vocabulary->vid]); $current_tags = _calais_get_current_tags($node, $vocabulary->vid); $has_keywords = sizeof($keywords[$vocabulary->vid]); // If there are keywords available, make these vocabs appear at the top of the long list if ($has_keywords) { $vocabulary->weight -= 100; } // If there are no keywords but existing terms, make these vocabs appear next if (!$has_keywords && !empty($current_tags)) { $vocabulary->weight -= 50; } $form['calais'][$vocabulary->vid] = array( '#type' => 'textfield', '#title' => $vocabulary->name, '#description' => $suggestions, '#required' => $vocabulary->required, '#default_value' => $current_tags, '#autocomplete_path' => 'taxonomy/autocomplete/'. $vocabulary->vid, '#weight' => $vocabulary->weight, '#size' => 75, '#maxlength' => CALAIS_TERM_MAXLENGTH, ); if ($has_keywords) { $form['calais'][$vocabulary->vid]['#attributes'] = array( 'class' => 'keywords_available', ); } } $form['calais']['#tree'] = TRUE; $form['submit'] = array('#type' => 'submit', '#value' => t('Save'), '#weight' => 45); return $form; } /** * Provide validation for the Calais free tagging taxonomy terms. * * The essence of this was clipped from taxonomy_node_validate() */ function calais_keywords_form_validate($form, &$form_state) { $node = (object)$form_state['values']; if (!empty($node->calais)) { $terms = $node->calais; foreach ($terms as $vid => $vid_value) { $vocabulary = taxonomy_vocabulary_load($vid); if (empty($vocabulary->tags)) { // see form_get_error $key = implode('][', $element['#parents']); // on why this is the key form_set_error("calais][$vid", t('The %name vocabulary can not be modified in this way. It is not setup for Free Tagging.', array('%name' => $vocabulary->name))); } } } } /** * Update the node for any Calais keyword modifications. */ function calais_keywords_form_submit($form, &$form_state) { $node = (object)$form_state['values']; foreach ($node->calais as $vid => $vid_value) { calais_remove_terms_for_node($node, $vid); $terms = drupal_explode_tags($vid_value); foreach ($terms as $term_name) { $term = calais_get_node_term($node->nid, $term_name); if(!$term){ $term = new stdClass; $term->value = $term_name; } calais_assign_to_node($vid, $term, $node); } } $form_state['redirect'] = "node/{$node->nid}"; } /** * Remove all terms on a node for a particular vocabulary. * * @param $node The node to remove terms * @param $vid The vocabulary whose terms will be removed from the node */ function calais_remove_terms_for_node($node, $vid) { db_query('DELETE FROM {term_node} WHERE vid = %d AND tid IN (SELECT d.tid FROM {term_data} AS d WHERE d.vid = %d)', $node->vid, $vid); } /** * Process the node and get all specified terms for the current vocabulary */ function _calais_get_current_tags($node, $vid) { if (!isset($node->taxonomy)) return NULL; $terms = $node->taxonomy; $current_tags = taxonomy_implode_tags($terms, $vid); $current_tags .= (array_key_exists('tags', $terms) ? $terms['tags'][$vid] : NULL); return $current_tags; } /** * Theme function to render the suggestions for a particular vocabulary */ function theme_calais_suggestions($vid, $terms) { if (sizeof($terms)) { $suggestions .= "
"; $suggestions .= t('Calais Suggestions: '); foreach ($terms as $term) { $suggestions .= ""; } $suggestions .= "
"; } return $suggestions; }