You are here

function user_reference_field_views_data in References 7.2

Implements hook_field_views_data().

In addition to the default field information we add the relationship for views to connect back to the users table.

File

user_reference/user_reference.module, line 1165
Defines a field type for referencing a user from a node.

Code

function user_reference_field_views_data($field) {

  // No module_load_include(): this hook is invoked from
  // views/modules/field.views.inc, which is where that function is defined.
  $data = field_views_field_default_views_data($field);
  $storage = $field['storage']['details']['sql'];
  foreach ($storage as $table_data) {
    $table = key($table_data);
    $columns = current($table_data);
    $id_column = $columns['uid'];
    if (isset($data[$table])) {

      // Filter: swap the handler to the 'in' operator. The callback receives
      // the field name instead of the whole $field structure to keep views
      // data to a reasonable size.
      $data[$table][$id_column]['filter']['handler'] = 'views_handler_filter_in_operator';
      $data[$table][$id_column]['filter']['options callback'] = 'user_reference_views_filter_options';
      $data[$table][$id_column]['filter']['options arguments'] = array(
        $field['field_name'],
      );

      // Argument: display users.name in argument titles (handled in our custom
      // handler) and summary lists (handled by the base views_handler_argument
      // handler).
      // Both mechanisms rely on the 'name table' and 'name field' information
      // below, by joining to a separate copy of the base table from the field
      // data table.
      $data[$table][$id_column]['argument']['handler'] = 'references_handler_argument';
      $data[$table][$id_column]['argument']['name table'] = $table . '_reference';
      $data[$table][$id_column]['argument']['name field'] = 'name';
      $data[$table . '_reference']['table']['join'][$table] = array(
        'left_field' => $id_column,
        'table' => 'users',
        'field' => 'uid',
      );

      // Relationship.
      $data[$table][$id_column]['relationship'] = array(
        'handler' => 'references_handler_relationship',
        'base' => 'users',
        'base field' => 'uid',
        'field' => $id_column,
        'label' => $field['field_name'],
        'field_name' => $field['field_name'],
      );
    }
  }
  return $data;
}