View source
<?php
function profanity_views_data_alter(&$data) {
$entity_info = entity_get_info();
foreach ($entity_info as $entity_type => $info) {
if (empty($info['entity keys']['label'])) {
continue;
}
$title_property = $info['entity keys']['label'];
if (empty($data[$entity_type][$title_property])) {
continue;
}
$data[$entity_type]["profanity_{$title_property}"] = $data[$entity_type][$title_property];
$data[$entity_type]["profanity_{$title_property}"]['title'] = t('@property - Profanity filtered', array(
'@property' => $data[$entity_type][$title_property]['title'],
));
$data[$entity_type]["profanity_{$title_property}"]['help'] = t('Run the entity title through profanity filters, allows you to select which filters.');
$data[$entity_type]["profanity_{$title_property}"]['field']['handler'] = 'profanity_views_handler_field';
$data[$entity_type]["profanity_{$title_property}"]['field']['type'] = $entity_type;
$data[$entity_type]["profanity_{$title_property}"]['field']['base_table'] = $info['base table'];
$data[$entity_type]["profanity_{$title_property}"]['field']['id'] = $info['entity keys']['id'];
}
if (module_exists('search_api_views')) {
try {
$entity_types = entity_get_info();
foreach (search_api_index_load_multiple(FALSE) as $index) {
$key = 'search_api_index_' . $index->machine_name;
$data[$key]['profanity_search_api_excerpt'] = array(
'group' => 'Search',
'title' => t('Excerpt - Profanity filtered'),
'help' => t('The search result excerpted to show found search terms filtered by the Profanity word lists.'),
'field' => array(
'type' => 'text',
'handler' => 'profanity_views_handler_search_excerpt',
),
);
}
} catch (Exception $e) {
watchdog_exception('search_api_views', $e);
}
}
}
class profanity_views_handler_field extends views_handler_field {
public $entity_type;
public $entity_id_key;
function init(&$view, &$options) {
parent::init($view, $options);
if (!empty($this->options['link_to_entity'])) {
$this->additional_fields['entity_info'] = array(
'table' => $this->definition['base_table'],
'field' => $this->definition['id'],
);
}
module_load_include('inc', 'entity', 'includes/entity.property');
$this->entity_type = entity_property_extract_innermost_type($this->definition['type']);
$this->entity_id_key = $this->definition['id'];
}
function option_definition() {
$options = parent::option_definition();
$options['link_to_entity'] = array(
'default' => FALSE,
'bool' => TRUE,
);
$options['profanity_lists'] = array(
'default' => array(),
);
return $options;
}
function options_form(&$form, &$form_state) {
$form['link_to_entity'] = array(
'#title' => t('Link this field to the original piece of content'),
'#description' => t("Enable to override this field's links."),
'#type' => 'checkbox',
'#default_value' => !empty($this->options['link_to_entity']),
);
$form['profanity_lists'] = array(
'#title' => t('Profanity lists to run'),
'#type' => 'checkboxes',
'#options' => profanity_get_lists_flat(),
'#default_value' => !empty($this->options['profanity_lists']) ? $this->options['profanity_lists'] : array(),
'#description' => t('Select which lists will be applied to the entity title.'),
);
parent::options_form($form, $form_state);
}
function render_link($data, $values) {
if (!empty($this->options['link_to_entity']) && !empty($this->additional_fields['entity_info'])) {
if ($data !== NULL && $data !== '') {
if (in_array($this->entity_type, array(
'node',
'taxonomy_term',
'user',
'harmony_thread',
'harmony_post',
))) {
switch ($this->entity_type) {
case 'node':
$path = 'node';
break;
case 'taxonomy_term':
$path = 'taxonomy/term';
break;
case 'user':
$path = 'user';
break;
case 'harmony_thread':
$path = 'thread';
break;
case 'harmony_post':
$path = 'post';
break;
}
$this->options['alter']['path'] = $path . '/' . $values->{$this->entity_id_key};
$this->options['alter']['make_link'] = TRUE;
}
else {
$entity = entity_load_single($this->entity_type, $this
->get_value($values, $this->entity_id_key));
if ($entity) {
$url = entity_uri($this->entity_type, $entity);
$this->options['alter']['path'] = $url['path'];
$this->options['alter']['make_link'] = TRUE;
}
}
}
else {
$this->options['alter']['make_link'] = FALSE;
}
}
return $data;
}
function render($values) {
$value = $this
->get_value($values);
$sanitised = $this
->sanitize_value($value);
if (!empty($this->options['profanity_lists'])) {
foreach ($this->options['profanity_lists'] as $list_name) {
$sanitised = profanity_list_execute($list_name, $sanitised);
}
}
return $this
->render_link($sanitised, $values);
}
}
class profanity_views_handler_search_excerpt extends profanity_views_handler_field {
function construct() {
parent::construct();
$this->real_field = 'search_api_excerpt';
}
function options_form(&$form, &$form_state) {
$form['profanity_lists'] = array(
'#title' => t('Profanity lists to run'),
'#type' => 'checkboxes',
'#options' => profanity_get_lists_flat(),
'#default_value' => !empty($this->options['profanity_lists']) ? $this->options['profanity_lists'] : array(),
'#description' => t('Select which lists will be applied to the entity title.'),
);
parent::options_form($form, $form_state);
}
}