View source
<?php
function content_taxonomy_autocomplete_field_widget_info_alter(&$info) {
$ct_settings = array(
'content_taxonomy_autocomplete_new_terms' => 'allow',
);
$info['taxonomy_autocomplete']['settings'] += $ct_settings;
if (isset($info['active_tags']['settings'])) {
$info['active_tags']['settings'] += $ct_settings;
}
if (isset($info['autocomplete_deluxe_taxonomy']['settings'])) {
$info['autocomplete_deluxe_taxonomy']['settings'] += $ct_settings;
}
if (isset($info['entityreference_autocomplete']['settings'])) {
$info['entityreference_autocomplete']['settings'] += $ct_settings;
}
if (isset($info['entityreference_autocomplete_tags']['settings'])) {
$info['entityreference_autocomplete_tags']['settings'] += $ct_settings;
}
}
function content_taxonomy_autocomplete_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
$field = $form['#field'];
$instance = $form['#instance'];
if ($instance['widget']['type'] == 'taxonomy_autocomplete' || $instance['widget']['type'] == 'active_tags_taxonomy_autocomplete' || $instance['widget']['type'] == 'autocomplete_deluxe_taxonomy' || $instance['widget']['type'] == 'entityreference_autocomplete' || $instance['widget']['type'] == 'entityreference_autocomplete_tags') {
$options = array(
'allow' => t('Allow and insert new terms'),
'moderate' => t('Allow new terms and insert them into a separate vocabulary'),
'deny' => t('Deny any new terms'),
);
$form['instance']['widget']['settings']['content_taxonomy_autocomplete_new_terms'] = array(
'#type' => 'radios',
'#title' => t('Autocomplete settings'),
'#options' => $options,
'#default_value' => isset($instance['widget']['settings']['content_taxonomy_autocomplete_new_terms']) ? $instance['widget']['settings']['content_taxonomy_autocomplete_new_terms'] : 'allow',
'#description' => t('If option 2 is selected, re-save this settings form and afterwards select a second vocabulary for new terms in the field settings. In case the Autocomplete Deluxe widget is used, new terms can be hidden from the suggestion list.'),
);
}
if (($instance['widget']['type'] == 'taxonomy_autocomplete' || $instance['widget']['type'] == 'active_tags_taxonomy_autocomplete' || $instance['widget']['type'] == 'autocomplete_deluxe_taxonomy' || $instance['widget']['type'] == 'entityreference_autocomplete' || $instance['widget']['type'] == 'entityreference_autocomplete_tags') && isset($form[$instance['field_name']]) && isset($instance['widget']['settings']['content_taxonomy_autocomplete_new_terms'])) {
if (!isset($field['settings']['allowed_values'][1])) {
$field['settings']['allowed_values'][1] = array(
'vocabulary' => $field['settings']['allowed_values'][0]['vocabulary'],
'parent' => 0,
);
}
$vocabularies = taxonomy_get_vocabularies();
$options = array();
$options[''] = '---';
foreach ($vocabularies as $vocabulary) {
$options[$vocabulary->machine_name] = $vocabulary->name;
}
$form['field']['settings']['allowed_values'][1]['vocabulary'] = array(
'#type' => 'select',
'#title' => t('Vocabulary for new terms'),
'#default_value' => isset($field['settings']['allowed_values'][1]['vocabulary']) ? $field['settings']['allowed_values'][1]['vocabulary'] : '',
'#options' => $options,
'#description' => t('New terms form autocompletes will be inserted in this vocabulary.'),
);
$form['field']['settings']['allowed_values'][1]['parent'] = array(
'#type' => 'value',
'#value' => $field['settings']['allowed_values'][1]['parent'],
);
if ($instance['widget']['type'] == 'autocomplete_deluxe_taxonomy') {
$form['field']['settings']['allowed_values'][1]['content_taxonomy_ignore_in_suggestions'] = array(
'#type' => 'checkbox',
'#title' => t('Ignore vocabulary above for suggested terms.'),
'#default_value' => isset($field['settings']['allowed_values'][1]['content_taxonomy_ignore_in_suggestions']) ? $field['settings']['allowed_values'][1]['content_taxonomy_ignore_in_suggestions'] : FALSE,
);
}
}
}
function content_taxonomy_autocomplete_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
$instances = field_info_instances($form['#entity_type'], $form['#bundle']);
foreach ($instances as $instance) {
if (($instance['widget']['type'] == 'taxonomy_autocomplete' || $instance['widget']['type'] == 'autocomplete_deluxe_taxonomy' || $instance['widget']['type'] == 'entityreference_autocomplete' || $instance['widget']['type'] == 'entityreference_autocomplete_tags') && isset($form[$instance['field_name']]) && isset($instance['widget']['settings']['content_taxonomy_autocomplete_new_terms'])) {
$lang_key = $form[$instance['field_name']]['#language'];
if ($instance['widget']['settings']['content_taxonomy_autocomplete_new_terms'] == 'moderate') {
$form[$instance['field_name']][$lang_key]['#element_validate'][] = 'content_taxonomy_autocomplete_validate_moderate_new_terms';
}
else {
if ($instance['widget']['settings']['content_taxonomy_autocomplete_new_terms'] == 'deny') {
$form[$instance['field_name']][$lang_key]['#element_validate'][] = 'content_taxonomy_autocomplete_validate_deny_new_terms';
}
}
}
}
}
function content_taxonomy_autocomplete_validate_deny_new_terms($element, &$form_state, $form) {
$values = $form_state['values'];
if (!empty($element['#array_parents'])) {
foreach ($element['#array_parents'] as $parent) {
$values = $values[$parent];
}
}
foreach ($values as $delta => $value) {
if (isset($value['tid']) && $value['tid'] == 'autocreate') {
form_error($element, t('%name: new terms are not allowed. Please choose from the suggested options.', array(
'%name' => $element['#title'],
)));
}
}
}
function content_taxonomy_autocomplete_validate_moderate_new_terms($element, &$form_state) {
$field = field_widget_field($element, $form_state);
$values =& $form_state['values'];
foreach ($element['#array_parents'] as $parent) {
$values =& $values[$parent];
}
if (isset($field['settings']['allowed_values'][1])) {
if ($voc2 = taxonomy_vocabulary_machine_name_load($field['settings']['allowed_values'][1]['vocabulary'])) {
foreach ($values as $delta => $value) {
if ($value['tid'] == 'autocreate') {
$values[$delta]['vid'] = $voc2->vid;
$values[$delta]['vocabulary_machine_name'] = $voc2->machine_name;
}
}
}
}
}
function content_taxonomy_autocomplete_query_autocomplete_deluxe_taxonomy_autocomplete_alter(QueryAlterableInterface $query) {
$field = field_info_field($query
->getMetaData('field_name'));
if (isset($field['settings']['allowed_values'][1]) && isset($field['settings']['allowed_values'][1]['content_taxonomy_ignore_in_suggestions']) && $field['settings']['allowed_values'][1]['content_taxonomy_ignore_in_suggestions']) {
if ($voc1 = taxonomy_vocabulary_machine_name_load($field['settings']['allowed_values'][0]['vocabulary'])) {
$query
->condition('t.vid', $voc1->vid);
}
}
}
function content_taxonomy_autocomplete_search_api_processor_info() {
$callbacks['content_taxonomy_autocomplete_moderated_terms'] = array(
'name' => t('Content Taxonomy Autocomplete Moderated Terms Filter'),
'description' => t('Filters terms from autocompletes that belong to the moderated vocabulary.'),
'class' => 'ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor',
);
return $callbacks;
}