You are here

panels_tabs.module in Panels Tabs 5

Same filename and directory in other branches
  1. 6 panels_tabs.module
  2. 7.2 panels_tabs.module
  3. 7 panels_tabs.module

Definition of the 'tabs' panel style.

File

panels_tabs.module
View source
<?php

/**
 * @file
 * Definition of the 'tabs' panel style.
 */

//----------------------------------------------------------------------------

// Panels hooks.

/**
 * Implementation of hook_panels_styles().
 */
function panels_tabs_panels_styles() {
  return array(
    'tabs' => array(
      'title' => t('Tabs'),
      'description' => t('Presents the panes in tabs.'),
      'render panel' => 'panels_tabs_style_render_panel',
      'settings form' => 'panels_tabs_style_settings_form',
      'settings validate' => 'panels_tabs_style_settings_validate',
    ),
  );
}

//----------------------------------------------------------------------------

// Panels style plugin callbacks.

/**
 * Render callback.
 */
function theme_panels_tabs_style_render_panel($display, $owner_id, $panes, $settings) {
  $output = '';

  // Generate a unique id based on the CSS ID and the name of the panel in the
  // layout.
  $pane_id = reset(array_keys($panes));
  $panel = $display->content[$pane_id]->panel;
  $id = "{$display->css_id}-{$panel}";

  // Add the Javascript to the page, and save the settings for this panel.
  _panels_tabs_add_js($id, $settings['filling_tabs']);
  $tabs = array();
  $tabs[$id] = array(
    '#type' => 'tabset',
  );
  $index = 0;
  foreach ($panes as $pane_id => $content) {

    // Remove the title from the content. We don't want titles in both the tab
    // and the content associated with the tab.
    if ($content->content) {
      $content_without_title = drupal_clone($content);
      unset($content_without_title->title);
      $tabs[$id][$pane_id] = array(
        '#type' => 'tabpage',
        '#title' => $content->title,
        '#content' => theme('panels_pane', $content_without_title, $display->content[$pane_id], $display),
        '#weight' => $index,
      );
      $index++;
    }
  }
  $output = tabs_render($tabs);
  return $output;
}

/**
 * Settings form callback.
 */
function panels_tabs_style_settings_form($settings) {
  $form['filling_tabs'] = array(
    '#type' => 'radios',
    '#title' => t('Horizontally filling tabs'),
    '#options' => array(
      'disabled' => t('Disabled'),
      'equal' => t('Equal width tabs'),
      'smart' => t('Equal width scaling tabs'),
    ),
    '#description' => t("Horizontally filling tabs make sure the tabs consume all available\n      horizontal space.<br />\n      Equal width tabs: sets the <em>width</em> property, forcing each tab\n      to be equally wide. If the text doesn't fit in the tab, the overflow\n      will be hidden.<br />\n      Horizontally filling, smart width tabs: calculates the length of the\n      text in each tab and compares this to the total length of the text on\n      all tabs. It then sets the width property of each tab according to the\n      percentage of text the tab contains."),
    '#default_value' => isset($settings['filling_tabs']) ? $settings['filling_tabs'] : 'disabled',
  );
  return $form;
}

/**
 * Settings form validation callback.
 */
function panels_tabs_style_settings_validate($settings, $form, $form_values) {
  if (!in_array($form_values['filling_tabs'], array(
    'disabled',
    'equal',
    'smart',
  ))) {
    form_error($form['filling_tabs'], t('Invalid value detected.'));
  }
}

//----------------------------------------------------------------------------

// Private functions.

/**
 * Adds the .js file only once per page load.
 */
function _panels_tabs_add_js($id, $filling_tabs) {
  static $added = FALSE;
  static $settings_added;

  // Include the JavaScript once per page.
  if (!$added) {
    drupal_add_js(drupal_get_path('module', 'panels_tabs') . '/panels_tabs.js');
    $added = TRUE;
  }

  // Store the JavaScript settings for every panel that uses this style.
  if (!isset($settings_added[$id])) {
    drupal_add_js(array(
      'panelsTabs' => array(
        $id => array(
          'fillingTabs' => isset($filling_tabs) ? $filling_tabs : 'disabled',
        ),
      ),
    ), 'setting');
    $settings_added[$id] = TRUE;
  }
}

Functions

Namesort descending Description
panels_tabs_panels_styles Implementation of hook_panels_styles().
panels_tabs_style_settings_form Settings form callback.
panels_tabs_style_settings_validate Settings form validation callback.
theme_panels_tabs_style_render_panel Render callback.
_panels_tabs_add_js Adds the .js file only once per page load.