0

I'm using Views 7.x-3.8 and Drupal 7.

I've created cutom tables :

  • youtube_video (with fields : vid, vtitle, description...)
  • youtube_playlist (with fields : pid, ptitle, description...)
  • youtube_video_playlist (with fields: vid, pid, weight)

I define my tables in Views with this hook_views_data() :

function mymodule_map_views_data() {
  //The group name
  $data['youtube_playlist']['table']['group'] = t('Youtube playlist');
  $data['youtube_video']['table']['group'] = t('Youtube video');
  //The base table
  $data['youtube_video']['table']['base'] = array(
    'field' => 'vid',
    'title' => t('youtube video'),
    'help' => t('youtube video should be related to youtube playlist.'),
    'weight' => -10,
    'defaults' => array(
      'field' => 'vtitle',
    ),
  );
  //The join
  $data['youtube_playlist']['table']['join']['youtube_video'] = array(
    'left_table' => 'youtube_video_playlist',
    'left_field' => 'pid',
    'field' => 'pid',
  );
  // Allow this field to be used as a contextual filter.
  $data['youtube_playlist']['pid'] = array(
    'title' => t('playlist id'),
    'help' => t('playlist id'),
    'field' => array(
      'handler' => 'views_handler_field_numeric',
      'click sortable' => TRUE,
    ),
    'argument' => array(
      'handler' => 'views_handler_argument',
      'name field' => 'title',
    ),
  );
  $data['youtube_video_playlist']['table']['join']['youtube_video'] = array(
    'left_field' => 'vid',
    'field' => 'vid',
  );
  //the fields description
  $data['youtube_playlist']['ptitle'] = array(
    'title' => t('youtube playlist title'),
    'help' => t('youtube playlist title.'),
    'field' => array(
      'handler' => 'views_handler_field',
      'click sortable' => TRUE, 
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );
  //the fields description
  $data['youtube_playlist']['description'] = array(
    'title' => t('youtube playlist description'),
    'help' => t('youtube playlist description.'),
    'field' => array(
      'handler' => 'views_handler_field',
      'click sortable' => TRUE, 
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );
  $data['youtube_video']['vtitle'] = array(
    'title' => t('youtube video title'),
    'help' => t('youtube video title.'),
    'field' => array(
      'handler' => 'views_handler_field',
      'click sortable' => TRUE, 
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );
  $data['youtube_video']['vhash'] = array(
    'title' => t('youtube video vhash'),
    'help' => t('youtube video vhash.'),
    'field' => array(
      'handler' => 'views_handler_field',
      'click sortable' => TRUE, 
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );
  return $data;
}

I've created a page view with my video informations, then - added the playlist title to the contextual filter - set the path to 'playlist/%'

This allows me to have a view that displays all videos from a playlist (filtered with a contextual filter). But to be usable, i would like to have a menu item "Playlist" and the submenus being AUTOMATICALLY all the playlists, when i create the view. Something like :

- Home
- Playlists
--playlist 1
--playlist 2
...

and when i click on playlist 1, i've got all the videos from that playlist 1

In menu section in views, I've tried to add menu tabs (and all the others, actually), but i have the message "A display whose path ends with a % cannot be a tab.". So it seems problematic to create (even only) menus with contextual filter.

Is it possible using only the standard view UI, or do i have to add some hooks or other logic ? If so do you have any lead.

1 Answer 1

0

I've managed to find a solution for it, but it is not "perfect" : i will post another question for the remaining problem, since it's not directly related.

  • I set my view (page) with contextual filter (playlist id : 'pid')
  • I set my path name : playlist/%

Then i added in my module the items in the hook_menu :

function mymodule_menu() {
  //Create parent menu item
  $items['playlist'] = array(
    'title' => 'Playlists',
    'menu_name' => 'main-menu',
    'expanded' => TRUE,
    'enabled' => 1,
    'access callback' => TRUE,
    'page callback' => '_mymodule_menu_page',
  );
  //Get all the playlists
  $result = db_select('youtube_playlist', 'p')
    ->fields('p', array('pid'))
    ->orderBy('title', 'ASC')
    ->execute();
  foreach ($result as $record) {
    //Create the menu items, children of "Playlist" menu item
    $items['playlist/' . $record->pid] = array(
      'title' => $record->title,
      'access callback' => TRUE,
      'expanded' => TRUE,
      'page callback' => 'views_page', //'views_embed_view',
      'page arguments' => array('ytv', 'page_2', 1),
      'file path' => drupal_get_path('module', 'views'),
      'file' => 'views.module',
    );
  }
  return $items;
}
with 'page arguments' : 1) view name, 2) view displayid, 3) position in the path the arg appears (from 0)

I now have my menu "Playlists" with all the playlists as submenus, and when i choose one i've got all the videos from that playlist chosen.

The only problem left is the fact the contextual filter is 'pid' and not 'ptitle' (problem of cleaning the ptitle name). But for this question, this is ok.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.