You are here

field_group_easy_responsive_tabs.module in Field Group: Easy Responsive Tabs to Accordion 7

Same filename and directory in other branches
  1. 8 field_group_easy_responsive_tabs.module

Module file for the field_group_easy_responsive_tabs module.

File

field_group_easy_responsive_tabs.module
View source
<?php

/**
 * @file
 * Module file for the field_group_easy_responsive_tabs module.
 */

/**
 * Implements hook_libraries_info().
 */
function field_group_easy_responsive_tabs_libraries_info() {
  $libraries['easy-responsive-tabs'] = array(
    'name' => 'Easy Responsive Tabs to Accordion',
    'vendor url' => 'https://github.com/samsono/Easy-Responsive-Tabs-to-Accordion',
    'download url' => 'https://github.com/samsono/Easy-Responsive-Tabs-to-Accordion/releases/tag/1.2.2',
    'download file url' => 'https://github.com/samsono/Easy-Responsive-Tabs-to-Accordion/archive/1.2.2.tar.gz',
    'version arguments' => array(
      'file' => 'Easy-Responsive-Tabs-to-Accordion.jquery.json',
      // "version": "1.2.1",
      'pattern' => '/version": "(\\d+\\.+\\d+\\.+\\d+)"/',
      'lines' => 5,
    ),
    'files' => array(
      'js' => array(
        'js/easyResponsiveTabs.js',
      ),
      'css' => array(
        'css/easy-responsive-tabs.css',
      ),
    ),
  );
  return $libraries;
}

/**
 * Implements hook_ctools_plugin_api().
 */
function field_group_easy_responsive_tabs_ctools_plugin_api($module, $api) {
  if ($module == 'field_group' && $api == 'field_group') {
    return array(
      'version' => 1,
    );
  }
}

/**
 * Implements hook_theme().
 */
function field_group_easy_responsive_tabs_theme() {
  return array(
    'field_group_easy_responsive_tabs_nav' => array(
      'template' => 'templates/field-group-easy-responsive-tabs-nav',
      'variables' => array(
        'items' => array(),
        'group' => NULL,
      ),
    ),
  );
}

/**
 * Implements hook_field_group_formatter_info().
 */
function field_group_easy_responsive_tabs_field_group_formatter_info() {
  $formatters = array();
  $formatters['field_group_easy_responsive_tabs_nav'] = array(
    'label' => t('Field Group: Easy Responsive Tabs to Accordion (Group)'),
    'description' => t('This fieldgroup renders child groups as Field Group: Easy Responsive Tabs to Accordion Group'),
    'instance_settings' => array(
      'classes' => '',
      'type' => 'default',
      'width' => 'auto',
      'fit' => 1,
      'closed' => 0,
      'activetab_bg' => '#B5AC5F',
      'inactive_bg' => '#E0D78C',
      'active_border_color' => '#9C905C',
      'active_content_border_color' => '#9C905C',
      'tabidentify' => '',
    ),
  );
  $formatters['field_group_easy_responsive_tabs_nav_item'] = array(
    'label' => t('Field Group: Easy Responsive Tabs to Accordion (Item)'),
    'description' => t('This fieldgroup renders the content as a Field Group: Easy Responsive Tabs to Accordion Item'),
    'instance_settings' => array(
      'classes' => '',
    ),
  );
  foreach (array_keys($formatters) as $key) {
    $formatter =& $formatters[$key];
    $formatter['instance_settings']['required_fields'] = 1;
  }
  return array(
    'form' => $formatters,
    'display' => $formatters,
  );
}

/**
 * Implements hook_field_group_format_settings().
 */
function field_group_easy_responsive_tabs_field_group_format_settings($group) {

  // Add a wrapper for extra settings to use by others.
  $form = array(
    'instance_settings' => array(
      '#tree' => TRUE,
      '#weight' => 5,
    ),
  );
  $field_group_types = field_group_formatter_info();
  $mode = $group->mode == 'form' ? 'form' : 'display';
  $formatter = $field_group_types[$mode][$group->format_type];

  // Add optional instance_settings.
  switch ($group->format_type) {
    case 'field_group_easy_responsive_tabs_nav':

      // 'type': default, vertical, accordion.
      $form['instance_settings']['type'] = array(
        '#title' => t('Type'),
        '#description' => t('default, vertical, accordion'),
        '#type' => 'select',
        '#options' => array(
          'default' => 'Default',
          'vertical' => 'Vertical',
          'accordion' => 'Accordion',
        ),
        '#default_value' => isset($group->format_settings['instance_settings']['type']) ? $group->format_settings['instance_settings']['type'] : $formatter['instance_settings']['type'],
        '#weight' => 1,
      );

      // 'width': auto or any custom width.
      $form['instance_settings']['width'] = array(
        '#title' => t('Width'),
        '#description' => t('auto or any custom width'),
        '#type' => 'textfield',
        '#default_value' => isset($group->format_settings['instance_settings']['width']) ? $group->format_settings['instance_settings']['width'] : $formatter['instance_settings']['width'],
        '#weight' => 1.5,
      );

      // 'fit': 100% fits in a container.
      $form['instance_settings']['fit'] = array(
        '#title' => t('Fit'),
        '#description' => t('100% fits in a container'),
        '#type' => 'select',
        '#options' => array(
          0 => t('No'),
          1 => t('Yes'),
        ),
        '#default_value' => isset($group->format_settings['instance_settings']['fit']) ? $group->format_settings['instance_settings']['fit'] : $formatter['instance_settings']['fit'],
        '#weight' => 1.5,
      );

      // 'close': Close the panels on start, the options 'accordion' and 'tabs'
      // keep them closed in there respective view types.
      $form['instance_settings']['closed'] = array(
        '#title' => t('Closed'),
        '#description' => t('Close the panels on start, the options "accordion" and "tabs" keep them closed in there respective view types.'),
        '#type' => 'select',
        '#options' => array(
          0 => t('No'),
          1 => t('Yes'),
        ),
        '#default_value' => isset($group->format_settings['instance_settings']['closed']) ? $group->format_settings['instance_settings']['closed'] : $formatter['instance_settings']['closed'],
        '#weight' => 1.5,
      );

      // Activate: Callback function, gets called if tab is switched.
      // TODO: make an option.
      // 'activetab_bg': background color for active tabs in this group.
      $form['instance_settings']['activetab_bg'] = array(
        '#title' => t('Active tab bg'),
        '#description' => t('background color for active tabs in this group'),
        '#type' => 'textfield',
        '#default_value' => isset($group->format_settings['instance_settings']['activetab_bg']) ? $group->format_settings['instance_settings']['activetab_bg'] : $formatter['instance_settings']['activetab_bg'],
        '#weight' => 1.5,
      );

      // 'inactive_bg': background color for inactive tabs in this group.
      $form['instance_settings']['inactive_bg'] = array(
        '#title' => t('Inactive tab bg'),
        '#description' => t('background color for inactive tabs in this group'),
        '#type' => 'textfield',
        '#default_value' => isset($group->format_settings['instance_settings']['inactive_bg']) ? $group->format_settings['instance_settings']['inactive_bg'] : $formatter['instance_settings']['inactive_bg'],
        '#weight' => 1.5,
      );

      // 'active_border_color': border color for active tabs heads in this group.
      $form['instance_settings']['active_border_color'] = array(
        '#title' => t('Active border color'),
        '#description' => t('border color for active tabs heads in this group'),
        '#type' => 'textfield',
        '#default_value' => isset($group->format_settings['instance_settings']['active_border_color']) ? $group->format_settings['instance_settings']['active_border_color'] : $formatter['instance_settings']['active_border_color'],
        '#weight' => 1.5,
      );

      // 'active_content_border_color': border color for active tabs contect in
      // this group so that it matches the tab head border.
      $form['instance_settings']['active_content_border_color'] = array(
        '#title' => t('Active content border color'),
        '#description' => t('border color for active tabs contect in this group so that it matches the tab head border'),
        '#type' => 'textfield',
        '#default_value' => isset($group->format_settings['instance_settings']['active_content_border_color']) ? $group->format_settings['instance_settings']['active_content_border_color'] : $formatter['instance_settings']['active_content_border_color'],
        '#weight' => 1.5,
      );

      // 'tabidentify': The tab groups identifier *This should be a unique name
      // for each tab group and should not be defined in any styling or css file.
      $form['instance_settings']['tabidentify'] = array(
        '#title' => t('The tab groups identifier'),
        '#description' => t('The tab groups identifier *This should be a unique name for each tab group and should not be defined in any styling or css file.'),
        '#type' => 'textfield',
        '#default_value' => isset($group->format_settings['instance_settings']['tabidentify']) ? $group->format_settings['instance_settings']['tabidentify'] : $formatter['instance_settings']['tabidentify'],
        '#weight' => 2,
      );
      break;
    default:
      break;
  }
  return $form;
}

/**
 * Implements hook_field_group_pre_render().
 */
function field_group_easy_responsive_tabs_field_group_pre_render(&$element, $group, &$form) {
  $element['#group'] = $group;
  switch ($group->format_type) {
    case 'field_group_easy_responsive_tabs_nav':
      $items = array();
      foreach ($group->children as $group_name) {
        $items[$group_name] =& $element[$group_name];
      }
      $element['#items'] = $items;
      break;
    default:
      break;
  }
}

/**
 * Implements field_group_pre_render_<format-type>.
 */
function field_group_pre_render_field_group_easy_responsive_tabs_nav(&$element, $group, &$form) {
  $field_group_types = field_group_formatter_info();
  $mode = $group->mode == 'form' ? 'form' : 'display';
  $formatter = $field_group_types[$mode][$group->format_type];
  $identifier = _field_group_easy_responsive_tabs_get_identifier($group->identifier);
  $gs = isset($group->format_settings['instance_settings']) ? $group->format_settings['instance_settings'] : array();
  $fs = isset($formatter['instance_settings']) ? $formatter['instance_settings'] : array();
  $js_settings = array();
  $js_settings[] = array(
    'identifier' => $identifier,
    'type' => isset($gs['type']) ? $gs['type'] : $fs['type'],
    'width' => 'auto',
    'fit' => (bool) (isset($gs['fit']) ? $gs['fit'] : $fs['fit']),
    'closed' => (bool) (isset($gs['closed']) ? $gs['closed'] : $fs['closed']),
    'tabidentify' => isset($gs['tabidentify']) ? $gs['tabidentify'] : md5($group->classes),
    'activetab_bg' => isset($gs['activetab_bg']) ? $gs['activetab_bg'] : $fs['activetab_bg'],
    'inactive_bg' => isset($gs['inactive_bg']) ? $gs['inactive_bg'] : $fs['inactive_bg'],
    'active_border_color' => isset($gs['active_border_color']) ? $gs['active_border_color'] : $fs['active_border_color'],
    'active_content_border_color' => isset($gs['active_content_border_color']) ? $gs['active_content_border_color'] : $fs['active_content_border_color'],
  );
  drupal_add_js(array(
    'field_group_easy_responsive_tabs' => $js_settings,
  ), 'setting');
  if (($library = libraries_load('easy-responsive-tabs')) && !empty($library['loaded'])) {
    $element += array(
      '#type' => 'field_group_easy_responsive_tabs_nav',
      '#theme' => 'field_group_easy_responsive_tabs_nav',
      '#description' => $group->description,
      '#parents' => array(
        $group->parent_name,
      ),
      '#attached' => array(
        'js' => array(
          drupal_get_path('module', 'field_group_easy_responsive_tabs') . '/js/field_group_easy_responsive_tabs.js',
        ),
      ),
    );
  }
  else {
    watchdog($library['error'], $library['error message']);
  }
}

/**
 * Implements field_group_pre_render_<format-type>.
 */
function field_group_pre_render_field_group_easy_responsive_tabs_nav_item(&$element, $group, &$form) {
  $element += array(
    '#type' => 'field_group_easy_responsive_tabs_nav_item',
    '#title' => check_plain($group->label),
    '#description' => $group->description,
    '#weight' => $group->weight,
    '#parents' => array(
      $group->parent_name,
    ),
  );
}

/**
 * Implements template_preprocess_field_group_easy_responsive_tabs_nav().
 */
function template_preprocess_field_group_easy_responsive_tabs_nav(&$variables) {
  $group =& $variables['group'];
  if (isset($group->format_settings['instance_settings']['tabidentify'])) {
    $tabidentify = $group->format_settings['instance_settings']['tabidentify'];
  }
  else {
    $tabidentify = md5($variables['group']->classes);
  }
  $variables['identifier'] = _field_group_easy_responsive_tabs_get_identifier($group->identifier);
  $variables['tabidentify'] = $tabidentify;
  $variables['wrapper_classes'] = $variables['group']->classes;
  $variables['wrapper_classes'] .= ' field-group-easy-responsive-tabs-nav-wrapper';
  $variables['nav_classes'] = $variables['group']->classes;
  $variables['pane_classes'] = $variables['group']->classes;
  uasort($variables['items'], 'element_sort');
  foreach ($variables['items'] as $key => $item) {

    // Check if item is not empty and we have access to it.
    if ($item && (!isset($item['#access']) || $item['#access'])) {
      $variables['navs'][] = array(
        'content' => t($item['#title']),
        'classes' => $item['#group']->classes,
      );
      $variables['panes'][] = array(
        'content' => drupal_render($item),
      );
    }
  }
  $variables['is_empty'] = !(count($variables['navs']) && count($variables['panes']));
}

/**
 * Convert a group identifier to a usable HTML id attribute.
 */
function _field_group_easy_responsive_tabs_get_identifier($identifier) {
  return preg_replace('/\\PL/u', '-', drupal_strtolower($identifier));
}