View source
<?php
class SarniaViewsHandlerField extends views_handler_field {
public $original_value = NULL;
protected $sarnia_type_info;
function init(&$view, &$options) {
parent::init($view, $options);
$this->sarnia_type_info = sarnia_entity_type_load_by_index($this->definition['search_api_index']);
}
function ui_name($short = FALSE) {
if (!empty($this->options['ui_name'])) {
$title = check_plain($this->options['ui_name']);
return $title;
}
$title = $short && isset($this->definition['title short']) ? $this->definition['title short'] : $this->definition['title'];
$title .= ': ' . $this->options['solr_property'];
if (!$short) {
$title = $this->definition['group'] . ': ' . $title;
}
return $title;
}
public function option_definition() {
$options = parent::option_definition();
$options['solr_property'] = array(
'default' => NULL,
);
$options['formatter'] = array(
'default' => NULL,
);
$options['settings'] = array(
'default' => array(),
);
$options['is_multivalue'] = array(
'default' => FALSE,
);
$options['multivalue'] = array(
'contains' => array(
'count' => array(
'default' => 0,
),
'list_type' => array(
'default' => 'ul',
),
'list_separator' => array(
'default' => ', ',
),
),
);
return $options;
}
public function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$form_url = views_ui_build_form_url($form_state);
$form['solr_property'] = array(
'#type' => 'select',
'#title' => t('Solr property'),
'#options' => sarnia_index_get_field_options($this->definition['search_api_index']),
'#default_value' => $this->options['solr_property'],
'#ajax' => array(
'path' => $form_url,
),
'#submit' => array(
'views_ui_config_item_form_submit_temporary',
),
'#executes_submit_callback' => TRUE,
);
sarnia_element_add_combobox($form['solr_property']);
$server = search_api_server_load($this->sarnia_type_info['search_api_server']);
$solr_fields = $server
->getRemoteFields();
if (!empty($solr_fields[$this->options['solr_property']]) && $solr_fields[$this->options['solr_property']]
->isMultivalued()) {
$form['multivalue'] = array(
'#type' => 'fieldset',
'#tree' => TRUE,
'#title' => t('Multivalue property settings'),
);
$form['multivalue']['count'] = array(
'#type' => 'textfield',
'#title' => t('Number of values to display'),
'#description' => t('Maximum number of field values to display. Leave this at 0 to display all values.'),
'#default_value' => $this->options['multivalue']['count'],
'#element_validate' => array(
'views_element_validate_integer',
),
'#size' => 5,
);
$form['multivalue']['list_type'] = array(
'#type' => 'select',
'#title' => t('List type'),
'#options' => array(
'ul' => 'Unordered list',
'ol' => 'Ordered list',
'separator' => 'Separator',
),
'#default_value' => $this->options['multivalue']['list_type'],
);
$form['multivalue']['list_separator'] = array(
'#type' => 'textfield',
'#title' => t('Seperator'),
'#default_value' => $this->options['multivalue']['list_separator'],
'#states' => array(
'visible' => array(
'select[name="options[multivalue][list_type]"]' => array(
'value' => 'separator',
),
),
),
);
}
module_load_include('inc', 'views', 'modules/field/views_handler_field_field');
$field = field_info_field($this->definition['field_name']);
$formatters = array(
t('- None -'),
) + _field_view_formatter_options($field['type']);
$form['formatter'] = array(
'#type' => 'select',
'#title' => t('Formatter'),
'#options' => $formatters,
'#default_value' => $this->options['formatter'],
'#ajax' => array(
'path' => $form_url,
),
'#submit' => array(
'views_ui_config_item_form_submit_temporary',
),
'#executes_submit_callback' => TRUE,
);
$format = $this->options['formatter'];
$formatter = field_info_formatter_types($format);
$settings = $this->options['settings'] + field_info_formatter_settings($format);
$entity_type = $this->sarnia_type_info['machine_name'];
$instance = field_info_instance($entity_type, $this->definition['field_name'], $entity_type);
$instance['display']['_dummy'] = array(
'type' => $formatter,
'settings' => $settings,
);
$instance['display']['_dummy'] = array(
'type' => $format,
'settings' => $settings,
);
$settings_form = array(
'#value' => array(),
);
$function = (isset($formatter['module']) ? $formatter['module'] : '') . '_field_formatter_settings_form';
if (function_exists($function)) {
$settings_form = $function($field, $instance, '_dummy', array(), $form_state);
unset($settings_form['solr_property']);
}
$form['settings'] = $settings_form;
}
function options_submit(&$form, &$form_state) {
parent::options_submit($form, $form_state);
if (!isset($this->options['solr_property']) && $form_state['values']['options']['label'] == $this->definition['title'] || $this->options['solr_property'] == $this->options['label'] && $this->options['label'] == $form_state['values']['options']['label']) {
$form_state['values']['options']['label'] = $form_state['values']['options']['solr_property'];
}
$server = search_api_server_load($this->sarnia_type_info['search_api_server']);
$solr_fields = $server
->getRemoteFields();
$solr_property = $form_state['values']['options']['solr_property'];
$form_state['values']['options']['is_multivalue'] = !empty($solr_fields[$solr_property]) && $solr_fields[$solr_property]
->isMultivalued();
}
public function query() {
$this->field_alias = $this->options['solr_property'];
}
function post_execute(&$results) {
if (!empty($this->options['formatter'])) {
$ids = array();
foreach ($results as $result_id => $result) {
$entity_id =& $result->entity;
if (!is_object($entity_id)) {
$ids[$result_id] = $entity_id;
}
}
if (!empty($ids)) {
$entities = entity_load($this->sarnia_type_info['machine_name'], array_values($ids));
foreach ($ids as $result_id => $entity_id) {
if ($entities[$entity_id]) {
$results[$result_id]->entity = $entities[$entity_id];
}
}
}
}
$this->item_key = 'item__' . $this->options['id'];
foreach ($results as $row_id => $result) {
$result->{$this->item_key} = $this
->set_items($result, $row_id);
}
}
function render($values) {
$output = '';
$value = $this
->get_value($values);
if (is_array($value)) {
$value = array_map(array(
$this,
'sanitize_value',
), $value);
$output = implode(' ', $value);
}
else {
$output = $this
->sanitize_value($value);
}
return $output;
}
public function get_value($values, $field = NULL) {
if (!empty($values->_entity_properties[$this->options['solr_property']])) {
return $values->_entity_properties[$this->options['solr_property']];
}
return NULL;
}
function document_self_tokens(&$tokens) {
foreach ($this->view->display_handler
->get_handlers('field') as $field => $handler) {
if ($handler instanceof SarniaViewsHandlerField) {
$token_name = "[{$field}]";
if (isset($tokens[$token_name]) && isset($handler->options['solr_property'])) {
$tokens[$token_name] = $handler
->admin_summary();
}
}
}
}
function click_sortable() {
$name = $this->options['solr_property'];
$server = search_api_server_load($this->sarnia_type_info['search_api_server']);
$fields = $server
->getSortFields();
return isset($fields[$name]);
}
public function click_sort($order) {
$this->query
->sort($this->field_alias, $order);
}
public function set_items($values, $row_id) {
$items = array();
$entity = $values->entity;
$properties = $values->_entity_properties;
$solr_property = $this->options['solr_property'];
$id_field = $this->sarnia_type_info['id_field'];
if (!empty($this->options['formatter']) && !empty($entity)) {
$display = array(
'type' => $this->options['formatter'],
'settings' => $this->options['settings'] + array(
'solr_property' => $solr_property,
),
'label' => 'hidden',
'views_view' => $this->view,
'views_field' => $this,
'views_row_id' => $entity->id,
);
$render_array = field_view_field($entity->type, $entity, $this->definition['field_name'], $display, LANGUAGE_NONE);
foreach (element_children($render_array) as $i) {
$items[$i]['rendered'] = $render_array[$i];
if (isset($render_array['#access'])) {
$items[$i]['rendered']['#access'] = $render_array['#access'];
}
if ((!isset($items[$i]['rendered']['#access']) || $items[$i]['rendered']['#access']) && !empty($render_array['#items'][$i])) {
$items[$i]['raw'] = $render_array['#items'][$i];
}
}
}
else {
$value = $this
->get_value($values);
if (is_array($value)) {
foreach ($properties[$solr_property] as $value) {
$items[] = array(
'raw' => $value,
'rendered' => $this
->sanitize_value($value),
);
}
}
elseif (!empty($value)) {
$items[] = array(
'raw' => $properties[$solr_property],
'rendered' => $this
->sanitize_value($properties[$solr_property]),
);
}
}
if ($this->options['is_multivalue'] && $this->options['multivalue']['count'] > 0) {
$items = array_slice($items, 0, $this->options['multivalue']['count']);
}
return $items;
}
public function get_items($values) {
return $values->{$this->item_key};
}
public function render_item($key, $item) {
return $item['rendered'];
}
public function render_items($items) {
$output = '';
if (!empty($items)) {
if (!$this->options['is_multivalue']) {
$output = current($items);
}
elseif ($this->options['multivalue']['list_type'] == 'separator') {
$output = implode(check_plain($this->options['multivalue']['list_separator']), $items);
}
else {
$output = theme('item_list', array(
'type' => $this->options['multivalue']['list_type'],
'title' => NULL,
'items' => $items,
));
}
}
return $output;
}
public function sanitize_value($value, $type = NULL) {
return $value;
}
}