You are here

protected function alpha_pagination_handler_pagination::getEntityPrefixes in Views Alpha Pagination 7

Retrieve the distinct first character prefix from the field tables.

Mark them as TRUE so their pagination item is represented properly.

Note that the node title is a special case that we have to take from the node table as opposed to the body or any custom fields.

@todo This should be cleaned up more and fixed "properly".

Return value

array An indexed array containing a unique array of entity prefixes.

1 call to alpha_pagination_handler_pagination::getEntityPrefixes()
alpha_pagination_handler_pagination::getItems in views/alpha_pagination_handler_pagination.inc
Retrieves the items used to populate the pagination item list.

File

views/alpha_pagination_handler_pagination.inc, line 562
Definition of alpha_pagination_handler_pagination.

Class

alpha_pagination_handler_pagination
Views area handler to display an alphabetic pagination representive of the entire result set for this view and not just the limited number being displayed by the view's configuration

Code

protected function getEntityPrefixes() {
  $prefixes = array();
  if ($entity_ids = $this
    ->getEntityIds()) {
    switch ($this->options['paginate_view_field']) {
      case 'name':
        $table = $this->view->base_table;
        $where = $this->view->base_field;

        // Extract the "name" field from the entity property info.
        $table_data = views_fetch_data($table);
        $entity_info = entity_get_property_info($table_data['table']['entity type']);
        $field = isset($entity_info['properties']['name']['schema field']) ? $entity_info['properties']['name']['schema field'] : 'name';
        break;
      case 'title':
        $table = $this->view->base_table;
        $where = $this->view->base_field;

        // Extract the "title" field from the entity property info.
        $table_data = views_fetch_data($table);
        $entity_info = entity_get_property_info($table_data['table']['entity type']);
        $field = isset($entity_info['properties']['title']['schema field']) ? $entity_info['properties']['title']['schema field'] : 'title';
        break;
      default:

        // If, somehow, the paginate_view_field value is not present, then we need to make sure
        // we have a default in place as a fallback position. This will default to name for
        // taxonomies and titles for everything else.
        if (empty($this->options['paginate_view_field'])) {
          $table = $this->view->base_table;
          $where = $this->view->base_field;
          if ($this->view->base_table == 'taxonomy_term_data') {

            // Get an array list of all non-image, non-entity or other assorted reference fields.
            $field = 'name';
          }
          else {
            $field = 'title';
          }
          break;
        }
        if (strpos($this->options['paginate_view_field'], ':') === FALSE) {

          // Format field name and table for single value fields
          $field = $this->options['paginate_view_field'] . '_value';
          $table = 'field_data_' . $this->options['paginate_view_field'];
        }
        else {

          // Format field name and table for compound value fields
          $field = str_replace(':', '_', $this->options['paginate_view_field']);
          $field_name_components = explode(':', $this->options['paginate_view_field']);
          $table = 'field_data_' . $field_name_components[0];
        }
        $where = 'entity_id';
        break;
    }
    $result = db_query('SELECT DISTINCT(SUBSTR(' . $field . ', 1, 1)) AS prefix
                          FROM {' . $table . '}
                          WHERE ' . $where . ' IN ( :nids )', array(
      ':nids' => $entity_ids,
    ));
    while ($data = $result
      ->fetchObject()) {
      $prefixes[] = is_numeric($data->prefix) ? $data->prefix : drupal_strtoupper($data->prefix);
    }
  }
  return array_unique(array_filter($prefixes));
}