public function TripalField::viewsData

3.x TripalField.inc public TripalField::viewsData($view_base_id)

Describes this field to Views.

The child class need not implement this function has all of the details provided for elements by the elementInfo() function are used to generate the details needed for Views.

Parameters

$view_base_id: Views was originally designed to integrate with SQL tables. And each field is associated with a table. Because these are TripalFields and views is not directly querying the tables it doesn't make sense to associate fields with a table, but we must associate the fields with the bundle. Each bundle is uniquely identified with the $view_base_id that is passed here.

Return value

An associative array describing the data structure. Primary key is the name used internally by Views for the bundle that is provided by the $view_base_id. The returned array should be compatible with the instructions provided by the hook_views_data() function.

File

tripal/includes/TripalFields/TripalField.inc, line 326

Class

TripalField

Code

public function viewsData($view_base_id) {
  $data = array();
  $field_name = $this->field['field_name'];
  $field_term = $this->getFieldTermID();

  $elements = $this->elementInfo();
  $field_details = $elements[$field_term];

  // Get any titles or help text that is overriden.
  $title = ucfirst($this->instance['label']);
  if (array_key_exists('label', $field_details)) {
    $title = $field_details['label'];
  }
  $help = $this->instance['description'];
  if (array_key_exists('help', $field_details)) {
    $help = $field_details['help'];
  }

  // Build the entry for the field.
  $data[$view_base_id][$field_name] = array(
    'title' => $title,
    'help' => $help,
    'field' => array(
      'handler' => 'tripal_views_handler_field',
      'click sortable' => TRUE,
    ),
  );
  // Is the field sortable?
  if (array_key_exists('sortable', $field_details) and $field_details['sortable']) {
    $data[$view_base_id][$field_name]['sort']['handler'] = 'tripal_views_handler_sort';
  }

  // Is the field searchable?
  if (array_key_exists('searchable', $field_details) and $field_details['searchable']) {
    $filter_handler = 'tripal_views_handler_filter_string';
    if (array_key_exists('type', $field_details) and $field_details['type'] == 'numeric') {
      $filter_handler = 'tripal_views_handler_filter_numeric';
    }
    $data[$view_base_id][$field_name]['filter'] = array(
      'handler' => $filter_handler,
    );
  }

  // Now add any entries for child elements.
  if (array_key_exists('elements', $field_details)) {
    $elements = $field_details['elements'];
    foreach ($elements as $element_name => $element_details) {
      $this->_addViewsDataElement($data, $view_base_id, $field_name, $element_name, $element_details);
    }
  }

  return $data;
}