0

I'm making a custom module that needs Views (API 3) integration. I have a table gentle_teams_season that can join to a table gentle_teams_season_users which then joins to the users table.

This is my gentle_teams_season table:

id      int
begin   int
end     int

This is my gentle_teams_season_users table:

uid    int    (foreign key to users table)
sid    int    (foreign key to gentle_teams_season table)

I want to be able to link a user to a season, but in an n-n relationship (a user can have multiple seasons, a season can have multiple users).

How can I get the users to show up when I'm making a View starting from Seasons?

This is the join I've tried (along with some others), which makes sense to me, but doesn't work. So what am I missing?

$data['gentle_teams_season']['table']['join'] = array(
  'users' => array(
    'left_table' => 'gentle_teams_season_users',
    'left_field' => 'sid',
    'field' => 'sid'
  )
);

$data['gentle_teams_season_users']['table']['join'] = array(
  'users' => array(
    'left_field' => 'uid',
    'field' => 'uid'
  )
);

I believe I'm joining from the seasons_users table to the users, via the intermediary table and then telling the Views API how to join from the intermediary table to the users table. I found that here.

My entire hook_views_data can be found in my GitHub repository, at the bottom of this file. I couldn't get the include (.inc file) to work, unfortunately.

This is my hook_views_data:

/**
* Implementation of hook_views_data()
*/
function gentle_teams_views_data() {
  //watchdog('gentle_teams', 'views data requested');

  /*
   * The seasons
   */
  $data['gentle_teams_season']['table']['group'] = t('Gentle teams');

  $data['gentle_teams_season']['table']['base'] = array(
    'field' => 'id',
    'title' => t('Gentle seasons'),
    'help' => t('Seasons as defined for Gentle Ultimate.'),
    'weight' => -10,
  );

  $data['gentle_teams_season']['id'] = array(
    'title' => t('Season id'),
    'help' => t('The season id'),
    'field' => array(
      'handler' => 'views_handler_field',
      'click_sortable' => TRUE
    ),
    'sort' => array('handler' => 'views_handler_sort'),
  );

  $data['gentle_teams_season']['begin'] = array(
    'title' => t('Begin'),
    'help' => t('The start year of the season'),
    'field' => array(
      'handler' => 'views_handler_field',
      'click_sortable' => TRUE
    ),
    'sort' => array('handler' => 'views_handler_sort'),
  );

  $data['gentle_teams_season']['end'] = array(
    'title' => t('End'),
    'help' => t('The end year of the season'),
    'field' => array(
      'handler' => 'views_handler_field',
      'click_sortable' => TRUE
    ),
    'sort' => array('handler' => 'views_handler_sort'),
  );

  $data['gentle_teams_season']['table']['join'] = array(
    'users' => array(
      'left_table' => 'gentle_teams_season_users',
      'left_field' => 'sid',
      'field' => 'sid'
    )
  );

  $data['gentle_teams_season_users']['table']['join'] = array(
    'users' => array(
      'left_field' => 'uid',
      'field' => 'uid'
    )
  );

  /*
   * The teams
   */
  $data['gentle_teams_team']['table']['group'] = t('Gentle teams');

  $data['gentle_teams_team']['table']['base'] = array(
    'field' => 'id',
    'title' => t('Gentle teams'),
    'help' => t('Teams as defined for Gentle Ultimate.'),
    'weight' => -10,
  );

  $data['gentle_teams_team']['table']['join'] = array(
    'gentle_teams_season' => array(
      'left_field' => 'id',
      'field' => 'sid'
    )
  );

  $data['gentle_teams_team']['id'] = array(
    'title' => t('Team id'),
    'help' => t('The team id'),
    'field' => array(
      'handler' => 'views_handler_field',
      'click_sortable' => TRUE
    ),
    'sort' => array('handler' => 'views_handler_sort'),
  );

  $data['gentle_teams_team']['name'] = array(
    'title' => t('Name'),
    'help' => t('The name of the team'),
    'field' => array(
      'handler' => 'views_handler_field',
      'click_sortable' => TRUE
    ),
    'sort' => array('handler' => 'views_handler_sort'),
  );



  return $data;
}
2
  • what is the structure of you custom table ? what version of views are you using ? and what's the rest of your implementation of hook_views_data like ? Feb 11, 2015 at 19:42
  • Edited my question to provide more info.
    – Peter
    Feb 13, 2015 at 11:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.