viewscarousel.module in Views carousel 5
Same filename and directory in other branches
Enable the creation of dynamic loading carousel widgets with views.
File
viewscarousel.moduleView source
<?php
/**
* @file
* Enable the creation of dynamic loading carousel widgets with views.
*/
/**
* Implementation of hook_menu().
*/
function viewscarousel_menu($may_cache) {
if ($may_cache) {
$items[] = array(
'path' => '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 {
$query = views_build_view(queries, &$view, $view->real_args, $use_pager = false, $limit = 0, $page = 0, $offset = 0, $filters = NULL);
$total_rows = db_result(db_query($query['countquery']));
}
$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 .= '<div class="viewscarousel-node" id="' . $id . '">';
$output .= node_view($node);
$output .= '</div>';
}
$output .= '<div class="viewscarousel viewscarousel-' . $settings['orientation'] . '" id="' . $id . '">';
$items = array();
foreach ($nodes as $node) {
$items[] = theme('viewscarousel_item', $fields, $node, $view);
}
$output .= theme('item_list', $items);
$output .= '</div>';
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 .= '<div class="view-field ' . views_css_safe('view-field-' . $field['field']) . '">';
if (!empty($field['label'])) {
$output .= '<span class="view-field-lable">' . $field['label'] . '</span> ';
}
$output .= '<span class="view-field-value">' . views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view) . '</span>';
$output .= '</div>';
}
}
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;
}
Functions
Name![]() |
Description |
---|---|
theme_viewscarousel_display | Theme a carousel. |
theme_viewscarousel_item | Theme a carousel item. |
viewscarousel_form_alter | Implementation of hook_form_alter(). Add carousel settings to block views, on Drupal's block configuration form. |
viewscarousel_get_settings | Return settings for the given carousel. |
viewscarousel_get_views | Return url of all page views that are of the type 'carousel'. |
viewscarousel_js | Fetches the view and returns formated views. |
viewscarousel_menu | Implementation of hook_menu(). |
viewscarousel_setup_form | Generate carousel configuration form. |
viewscarousel_setup_form_submit | Save carousel settings. |
viewscarousel_views_style_plugins | Implementation of hook_views_style_plugins() |