'viewscarousel/js', 'callback' => 'viewscarousel_js', 'access' => user_access('access content'), ); } else { foreach (viewscarousel_get_views() as $name => $view) { $items[] = array( 'path' => $view->url .'/view', 'title' => t('View'), 'callback' => 'views_view_page', 'access' => user_access('access content'), 'weight' => -10, 'type' => MENU_DEFAULT_LOCAL_TASK, ); $items[] = array( 'path' => $view->url .'/viewscarousel', 'title' => t('Carousel setup'), 'description' => t('Carousel setup.'), 'access' => user_access('administer content'), 'callback' => 'drupal_get_form', 'callback arguments' => array('viewscarousel_setup_form', $name), 'type' => MENU_LOCAL_TASK, 'weight' => 5, ); } } return $items; } /** * Implementation of hook_form_alter(). Add carousel settings to block * views, on Drupal's block configuration form. */ function viewscarousel_form_alter($form_id, &$form) { if ($form_id == 'block_admin_configure' && $form['module']['#value'] == 'views' && array_key_exists($form['delta']['#value'], viewscarousel_get_views('block'))) { $form['carousel_settings'] = array( '#type' => 'fieldset', '#title' => 'Carousel settings', '#collapsible' => TRUE, '#weight' => -1, ); $form['carousel_settings'] += viewscarousel_setup_form($form['delta']['#value'], TRUE); $form['#submit']['viewscarousel_setup_form_submit'] = array(); } } /** * Generate carousel configuration form. */ function viewscarousel_setup_form($view_name, $is_block = FALSE) { $settings = viewscarousel_get_settings($view_name); $form['carousel_settings'] = array('#tree' => TRUE); $form['carousel_settings']['orientation'] = array( '#title' => t('Orientation'), '#default_value' => $settings['orientation'], '#type' => 'select', '#options' => array('vertical' => t('Vertical'), 'horizontal' => t('Horizontal')), ); $form['carousel_settings']['visible_items'] = array( '#title' => t('Visible items'), '#type' => 'textfield', '#default_value' => $settings['visible_items'], '#description' => t('The number of items that will be visible.') ); $form['carousel_settings']['scroll_items'] = array( '#title' => t('Scroll items'), '#type' => 'textfield', '#default_value' => $settings['scroll_items'], '#description' => t('The number of items to scroll by.') ); $form['carousel_settings']['auto_scroll'] = array( '#title' => t('Auto scroll'), '#type' => 'textfield', '#default_value' => $settings['auto_scroll'], '#description' => t('Specifies how many seconds to periodically auto scroll the content. If set to 0 then auto scroll is turned off.') ); $form['carousel_settings']['item_width'] = array( '#title' => t('Item width'), '#type' => 'textfield', '#default_value' => $settings['item_width'], '#description' => t('Item width in pixels. Pass this, if you want the items set to the specified width. Otherwise, the assigned css value is used.') ); $form['carousel_settings']['item_height'] = array( '#title' => t('Item height'), '#type' => 'textfield', '#default_value' => $settings['item_height'], '#description' => t('Item height in pixels. Pass this, if you want the items set to the specified height. Otherwise, the assigned css value is used.') ); $form['carousel_settings']['wrap'] = array( '#type' => 'checkbox', '#title' => t('Wrap'), '#return_value' => TRUE, '#default_value' => $settings['wrap'], '#description' => t('Specifies whether to wrap at the last item and jump back to the start.'), ); $form['view_name'] = array('#type' => 'value', '#value' => $view_name); // If we aren't configuring a block, add a submit button. if (!$is_block) { $form['submit'] = array( '#type' => 'submit', '#value' => t('Submit'), ); } return $form; } /** * Save carousel settings. */ function viewscarousel_setup_form_submit($form_id, $form_values) { variable_set('carousel_'. $form_values['view_name'], $form_values['carousel_settings']); } /** * Return url of all page views that are of the type 'carousel'. */ function viewscarousel_get_views($type = 'page', $reset = FALSE) { static $views; if (is_null($views[$type]) || $reset) { $views[$type] = array(); $where = ($type == 'page') ? "page = 1 AND page_type = 'carousel'" : "block = 1 AND block_type = 'carousel'"; $result = db_query("SELECT name, url FROM {view_view} WHERE $where ORDER BY name"); while ($row = db_fetch_object($result)) { $views[$type][$row->name] = $row; } } return $views[$type]; } /** * Implementation of hook_views_style_plugins() */ function viewscarousel_views_style_plugins() { $plugins = array(); $plugins['carousel'] = array( 'name' => t('Carousel'), 'theme' => 'viewscarousel_display', 'summary_theme' => 'viewscarousel_display', 'validate' => 'viewscarousel_validate', 'needs_fields' => TRUE, 'needs_table_header' => FALSE, ); return $plugins; } /** * Return settings for the given carousel. */ function viewscarousel_get_settings($view_name) { $defaults = array( 'orientation' => 'horizontal', 'visible_items' => 3, 'scroll_items' => 2, 'auto_scroll' => 0, 'item_width' => '', 'item_height' => '', 'wrap' => FALSE, 'first_node' => TRUE, ); return array_merge($defaults, variable_get('carousel_'. $view_name, array())); } /** * Theme a carousel. */ function theme_viewscarousel_display(&$view, &$nodes, $type) { $path = drupal_get_path('module', 'viewscarousel'); drupal_add_js($path .'/jquery.jcarousel.pack.js'); drupal_add_js($path .'/viewscarousel.js'); drupal_add_css($path .'/viewscarousel.css'); $id = 'viewscarousel-'. $view->name; $settings = viewscarousel_get_settings($view->name); // If total_rows is not set, i.e. pager is set to FALSE or this is // not a page view, get the view query in order to get total_rows. if (isset($view->total_rows)) { $total_rows = $view->total_rows; } else { // Maybe there is a better way to do this, e.g. not using a // 'private' views function. $query = _views_get_query($view, $views->argument, $views->filter); $count_query = db_rewrite_sql($query['countquery'], 'node', 'nid', $query['rewrite_args']); $total_rows = db_result(db_query($count_query)); } $settings += array('view' => $view->name, 'total' => $total_rows); drupal_add_js(array('viewscarousel' => array($id => $settings)), 'setting'); $fields = _views_get_fields(); $output = ''; if ($settings['first_node']) { $node = array_shift($nodes); $node = node_load($node->nid); $output .= '
'; $output .= node_view($node); $output .= '
'; } $output .= '
'; $items = array(); foreach ($nodes as $node) { $items[] = theme('viewscarousel_item', $fields, $node, $view); } $output .= theme('item_list', $items); $output .= '
'; return $output; } /** * Theme a carousel item. */ function theme_viewscarousel_item(&$fields, &$node, &$view) { $output = ''; foreach ($view->field as $field) { if ($fields[$field['id']]['visible'] !== FALSE) { $output .= '
'; if (!empty($field['label'])) { $output .= ''. $field['label'] .' '; } $output .= ''. views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view) .''; $output .= '
'; } } return $output; } /** * Fetches the view and returns formated views. */ function viewscarousel_js() { $view = views_get_view($_GET['view']); $nodes_per_page = ($view->build_type == 'page') ? $view->nodes_per_page : $view->nodes_per_block; // TODO: handle views arguments. $items = views_build_view('items', $view, array(), TRUE, $nodes_per_page); $fields = _views_get_fields(); // Render views items. $data = array(); foreach ($items['items'] as $item) { $data[] = theme('viewscarousel_item', $fields, $item, $view); } print drupal_to_js(array('data' => $data)); exit(); }