class ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor in Content Taxonomy 7
Search API Processor that filters out terms from moderated vocabularies.
Hierarchy
- class \SearchApiAbstractProcessor implements SearchApiProcessorInterface
Expanded class hierarchy of ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor
1 string reference to 'ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor'
File
- includes/
content_taxonomy_autocomplete_moderated_terms.inc, line 6
View source
class ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor extends SearchApiAbstractProcessor {
/**
* Confiuration callback.
*
* Only allow users to select taxonomy fields.
*/
public function configurationForm() {
$form = parent::configurationForm();
$form['fields']['#options'] = $this
->filterTaxonomyFieldsOptions($form['fields']['#options']);
return $form;
}
/**
* Postprocess items while indexing and filter out the moderated terms.
*/
public function preprocessIndexItems(array &$items) {
$fields = $this
->getTaxonomyFields($this->options['fields']);
foreach ($items as &$item) {
foreach ($fields as $search_api_property_name => $field) {
if (isset($item[$search_api_property_name])) {
$this
->processTaxonomyField($item[$search_api_property_name]['value'], $item[$search_api_property_name]['type'], $field);
}
}
}
}
/**
* Processes a single field on a single item and does the actual filtering.
*
* As such a field can be nested, recursions are used until we reach the top
* level.
*/
protected function processTaxonomyField(&$value, &$type, $field) {
if (!isset($value) || $value === '') {
return;
}
if (substr($type, 0, 10) == 'list<list<') {
$inner_type = $t1 = substr($type, 5, -1);
foreach ($value as &$v) {
$t1 = $inner_type;
$this
->processTaxonomyField($v, $t1, $field);
}
return;
}
if (is_array($value)) {
foreach ($value as $key => $v) {
if ($this
->fieldValueIsModerated($v, $field)) {
unset($value[$key]);
}
}
}
elseif ($this
->fieldValueIsModerated($value, $field)) {
$value = NULL;
}
}
/**
* Returns TRUE if the given term id is from moderated vocabulary.
*
* @param $value
* The term id.
* @param $field
* The according field API field.
*/
private function fieldValueIsModerated($value, $field) {
$allowed_voc = $field['settings']['allowed_values'][0]['vocabulary'];
$term = taxonomy_term_load($value);
if ($term && $term->vocabulary_machine_name == $allowed_voc) {
return FALSE;
}
return TRUE;
}
/**
* Helper function that filters the configuration field options for taxonomy
* fields.
*/
private function filterTaxonomyFieldsOptions($options) {
$taxonomy_fields = array();
foreach ($options as $search_api_property_name => $label) {
if ($this
->getTaxonomyField($search_api_property_name)) {
$taxonomy_fields[$search_api_property_name] = $label;
}
}
return $taxonomy_fields;
}
/**
* Helper function that returns the taxonomy fields for the given search API
* property names.
*/
private function getTaxonomyFields($search_api_properties) {
$taxonomy_fields = array();
foreach ($search_api_properties as $search_api_property_name => $label) {
if ($field = $this
->getTaxonomyField($search_api_property_name)) {
$taxonomy_fields[$search_api_property_name] = $field;
}
}
return $taxonomy_fields;
}
/**
* Helper function that extracts the taxonomy field from a search API property
* name.
*/
private function getTaxonomyField($search_api_property_name) {
$parts = explode(':', $search_api_property_name);
foreach ($parts as $part) {
if (substr($part, 0, 6) == 'field_') {
$field = field_info_field($part);
if ($field && isset($field['type']) && $field['type'] == "taxonomy_term_reference") {
return $field;
}
}
}
return FALSE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor:: |
public | function |
Confiuration callback. Overrides SearchApiAbstractProcessor:: |
|
ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor:: |
private | function | Returns TRUE if the given term id is from moderated vocabulary. | |
ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor:: |
private | function | Helper function that filters the configuration field options for taxonomy fields. | |
ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor:: |
private | function | Helper function that extracts the taxonomy field from a search API property name. | |
ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor:: |
private | function | Helper function that returns the taxonomy fields for the given search API property names. | |
ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor:: |
public | function |
Postprocess items while indexing and filter out the moderated terms. Overrides SearchApiAbstractProcessor:: |
|
ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor:: |
protected | function | Processes a single field on a single item and does the actual filtering. | |
SearchApiAbstractProcessor:: |
protected | property | ||
SearchApiAbstractProcessor:: |
protected | property | ||
SearchApiAbstractProcessor:: |
public | function |
Submit callback for the form returned by configurationForm(). Overrides SearchApiProcessorInterface:: |
|
SearchApiAbstractProcessor:: |
public | function |
Validation callback for the form returned by configurationForm(). Overrides SearchApiProcessorInterface:: |
4 |
SearchApiAbstractProcessor:: |
protected | function | Internal helper function for imploding tokens into a single string. | |
SearchApiAbstractProcessor:: |
protected | function | Internal helper function for normalizing tokens. | |
SearchApiAbstractProcessor:: |
public | function |
Does nothing. Overrides SearchApiProcessorInterface:: |
2 |
SearchApiAbstractProcessor:: |
public | function |
Calls processKeys() for the keys and processFilters() for the filters. Overrides SearchApiProcessorInterface:: |
1 |
SearchApiAbstractProcessor:: |
protected | function | Function that is ultimately called for all text by the standard implementation, and does nothing by default. | 5 |
SearchApiAbstractProcessor:: |
protected | function | Method for preprocessing field data. | |
SearchApiAbstractProcessor:: |
protected | function | Called for processing a single text element in a field. The default implementation just calls process(). | 2 |
SearchApiAbstractProcessor:: |
protected | function | Method for preprocessing query filters. | |
SearchApiAbstractProcessor:: |
protected | function | Called for processing a single filter value. The default implementation just calls process(). | |
SearchApiAbstractProcessor:: |
protected | function | Called for processing a single search keyword. The default implementation just calls process(). | |
SearchApiAbstractProcessor:: |
protected | function | Method for preprocessing search keys. | |
SearchApiAbstractProcessor:: |
public | function |
Check whether this processor is applicable for a certain index. Overrides SearchApiProcessorInterface:: |
|
SearchApiAbstractProcessor:: |
protected | function | Determines whether to process data from the given field. | |
SearchApiAbstractProcessor:: |
protected | function | Determines whether fields of the given type should normally be processed. | |
SearchApiAbstractProcessor:: |
public | function |
Constructor, saving its arguments into properties. Overrides SearchApiProcessorInterface:: |
2 |