You are here

field_collection_tabs.module in Field Collection Tab formatter 7

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

Provides formatter for field collections on tabs @copyright Copyright(c) 2012 Previous Next Pty Ltd @license GPL v2 http://www.fsf.org/licensing/licenses/gpl.html @author Lee Rowlands larowlan at previousnext dot com dot au

File

field_collection_tabs.module
View source
<?php

/**
 * @file
 * Provides formatter for field collections on tabs
 * @copyright Copyright(c) 2012 Previous Next Pty Ltd
 * @license GPL v2 http://www.fsf.org/licensing/licenses/gpl.html
 * @author Lee Rowlands larowlan at previousnext dot com dot au
 */

/**
 * Implements hook_field_formatter_info().
 */
function field_collection_tabs_field_formatter_info() {
  return array(
    'field_collection_tabs' => array(
      'label' => t('Tabs'),
      'description' => t('Renders each field collection item on a tab'),
      'field types' => array(
        'field_collection',
      ),
      'settings' => array(
        'title_field' => 0,
        'view_mode' => 'full',
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function field_collection_tabs_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $element = array();
  if ($display['type'] == 'field_collection_tabs') {
    $options = array(
      t('No titles'),
    );
    $fields = field_info_instances('field_collection_item', $field['field_name']);
    foreach ($fields as $field_name => $field) {
      $options[$field_name] = $field['label'];
    }
    $element['title_field'] = array(
      '#type' => 'select',
      '#title' => t('Field to use for tab titles'),
      '#description' => t('Select the field to use for tab titles'),
      '#default_value' => $settings['title_field'],
      '#options' => $options,
    );
    $entity_type = entity_get_info('field_collection_item');
    $options = array();
    foreach ($entity_type['view modes'] as $mode => $info) {
      $options[$mode] = $info['label'];
    }
    $element['view_mode'] = array(
      '#type' => 'select',
      '#title' => t('View mode'),
      '#options' => $options,
      '#default_value' => $settings['view_mode'],
      '#description' => t('Select the view mode'),
    );
  }
  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function field_collection_tabs_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  if ($display['type'] == 'field_collection_tabs') {
    $fields = field_info_instances('field_collection_item', $field['field_name']);
    if (!empty($fields[$settings['title_field']])) {
      return t('Title field: %title_field', array(
        '%title_field' => $fields[$settings['title_field']]['label'],
      ));
    }
    else {
      return t('No title');
    }
  }
}

/**
 * Implements hook_field_formatter_view().
 */
function field_collection_tabs_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  switch ($display['type']) {

    // This formatter simply outputs the net_field/period wrapped in a div.
    case 'field_collection_tabs':
      $titles = $tabs = array();
      $title_field = !empty($display['settings']['title_field']) ? $display['settings']['title_field'] : FALSE;
      $view_mode = !empty($display['settings']['view_mode']) ? $display['settings']['view_mode'] : 'full';
      foreach ($items as $delta => $item) {
        if ($field_collection = field_collection_field_get_entity($item)) {
          $content = $field_collection
            ->view($view_mode);
          $tab_id = drupal_clean_css_identifier($field['field_name'] . '-tab-' . $delta);
          if ($title_field && ($render_array = $content['field_collection_item'][$field_collection->item_id])) {
            $titles[] = drupal_render($render_array[$title_field]);
          }
          else {
            $titles[] = t('Tab @delta', array(
              '@delta' => $delta,
            ));
          }
          $tabs[] = drupal_render($render_array);
        }
      }
      $element[] = array(
        '#theme' => 'field_collection_tabs',
        '#titles' => $titles,
        '#tabs' => $tabs,
        '#field_name' => $field['field_name'],
        '#prefix' => '<div class="field-collection-tabs">',
        '#suffix' => '</div>',
        '#attached' => array(
          'library' => array(
            array(
              'system',
              'ui.tabs',
            ),
          ),
          'js' => array(
            drupal_get_path('module', 'field_collection_tabs') . '/field_collection_tabs.js',
          ),
        ),
      );
      break;
  }
  return $element;
}

/**
 * Implements hook_theme().
 */
function field_collection_tabs_theme() {
  return array(
    'field_collection_tabs' => array(
      'variables' => array(
        'titles' => array(),
        'tabs' => array(),
        'field_name' => NULL,
      ),
    ),
    'field_collection_tabs_tab_title' => array(
      'variables' => array(
        'title' => NULL,
        'tab_id' => NULL,
      ),
    ),
    'field_collection_tabs_tab' => array(
      'variables' => array(
        'tab' => NULL,
        'tab_id' => NULL,
      ),
    ),
  );
}

/**
 * Default implementation of theme_field_collection_tabs().
 */
function theme_field_collection_tabs($variables) {
  $titles = $tabs = array();
  foreach ($variables['titles'] as $delta => $title) {
    $tab_id = drupal_clean_css_identifier($variables['field_name'] . '-tab-' . $delta);
    $titles[] = theme('field_collection_tabs_tab_title', array(
      'title' => $title,
      'tab_id' => $tab_id,
    ));
    $tabs[] = theme('field_collection_tabs_tab', array(
      'tab' => $variables['tabs'][$delta],
      'tab_id' => $tab_id,
    ));
  }
  return theme('item_list', array(
    'items' => $titles,
  )) . implode('', $tabs);
}

/**
 * Default implementation of theme_field_collection_tabs_tab_title().
 */
function theme_field_collection_tabs_tab_title($variables) {
  $tab_id = $variables['tab_id'];
  $title = $variables['title'];
  return '<a href="#' . $tab_id . '">' . $title . '</a>';
}

/**
 * Default implementation of theme_field_collection_tabs_tab().
 */
function theme_field_collection_tabs_tab($variables) {
  $tab_id = $variables['tab_id'];
  $tab = $variables['tab'];
  return '<div id="' . $tab_id . '">' . $tab . '</div>';
}