You are here

foundation_group.module in ZURB Section 8.5

File

foundation_group.module
View source
<?php

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

  // Add a wrapper for extra settings to use by others.
  $form = array(
    'instance_settings' => array(
      '#tree' => TRUE,
      '#weight' => 2,
    ),
  );
  $field_group_types = field_group_formatter_info();
  $mode = $group->mode == 'form' ? 'form' : 'display';
  $formatter = $field_group_types[$mode][$group->format_type];
  if ($group->format_type === 'foundation_group_accordion') {
    $form['instance_settings']['group_type'] = array(
      '#title' => t('Grouping type'),
      '#type' => 'select',
      '#options' => drupal_map_assoc(array(
        'auto',
        'accordion',
      )),
      '#default_value' => isset($group->format_settings['instance_settings']['group_type']) ? $group->format_settings['instance_settings']['group_type'] : $formatter['instance_settings']['group_type'],
    );
  }
  return $form;
}

/**
 * Implements field_group_pre_render_<format-type>().
 */
function field_group_pre_render_foundation_group_accordion(&$element, &$group, &$form) {

  // Convenience variables.
  $settings =& $group->format_settings['instance_settings'];
  $classes =& $settings['classes'];

  // Cast classes to array for easy manipulation and for adherence to
  // drupal_attributes() requirements.
  $classes = !empty($classes) ? explode(' ', $classes) : array();

  // Add required classes.
  $classes[] = 'accordion-container';

  // Add extra required class for group type.
  $classes[] = $settings['group_type'];

  // Strip duplicate classes.
  $classes = array_unique($classes);
  $element += array(
    '#type' => 'foundation_accordion',
    '#theme' => 'foundation_accordion',
    '#title' => check_plain(t($group->label)),
    '#attributes' => array(
      'id' => drupal_clean_css_identifier($group->label),
    ),
    '#description' => $group->description,
    '#parents' => array(
      $group->parent_name,
    ),
    '#settings' => $settings,
  );
}

/**
 * Implements field_group_pre_render_<format-type>().
 */
function field_group_pre_render_foundation_group_accordion_item(&$element, $group, &$form) {

  // Convenience variables.
  $settings =& $group->format_settings['instance_settings'];
  $classes =& $settings['classes'];

  // Cast classes to array for easy manipulation and for adherence to
  // drupal_attributes() requirements.
  $classes = !empty($classes) ? explode(' ', $classes) : array();

  // Add required classes.
  $classes += array(
    'foundation-accordion-item',
  );

  // Strip duplicate classes.
  $classes = array_unique($classes);
  $element += array(
    '#type' => 'foundation_accordion_item',
    '#group' => $group->parent_name,
    '#title' => check_plain(t($group->label)),
    '#attributes' => array(
      'id' => isset($element['#id']) ? $element['#id'] : drupal_html_id($group->label),
    ),
    '#description' => $group->description,
    '#parents' => array(
      $group->parent_name,
    ),
    '#formatter' => $group->format_settings['formatter'],
    '#settings' => $group->format_settings['instance_settings'],
  );
}

/**
 * Implements template_preprocess_foundation_accordion().
 */
function template_preprocess_foundation_accordion(&$variables) {
  $toggable = isset($variables['element']['#settings']['toggable']) ? $variables['element']['#settings']['toggable'] : FALSE;

  // Prepare accordion items for rendering.
  $items_processed = array();
  foreach (element_children($variables['element'], TRUE) as $key) {
    $item =& $variables['element'][$key];
    if (!empty($item) && $item['#type'] === 'foundation_accordion_item') {

      // Add classes array for drupal_attributes().
      $item['#attributes']['class'] = $item['#settings']['classes'];
      $item_processed = array(
        'title' => $item['#title'],
        'content' => drupal_render($item),
        'container_attributes' => $item['#attributes'],
        'content_attributes' => array(),
      );
      if ($item['#formatter'] === 'open') {
        $item_processed['container_attributes']['class'][] = 'active';
        $item_processed['content_attributes']['class'][] = 'active';
      }

      // Add required classes.
      $item_processed['content_attributes']['class'][] = 'content';
      $items_processed[] = $item_processed;
    }
  }

  // Replace items with processed version but keep original version.
  $variables['items'] = $items_processed;

  // Setup data-options.
  $data_options = array();

  // Setup attributes.
  $attributes =& $variables['element']['#attributes'];
  if (!empty($data_options)) {
    $attributes['data-options'] = implode('; ', $data_options);
  }

  // Add classes.
  $attributes['class'] = $variables['element']['#settings']['classes'];
}

Functions

Namesort descending Description
field_group_pre_render_foundation_group_accordion Implements field_group_pre_render_<format-type>().
field_group_pre_render_foundation_group_accordion_item Implements field_group_pre_render_<format-type>().
foundation_group_field_group_format_settings Implements hook_field_group_format_settings().
template_preprocess_foundation_accordion Implements template_preprocess_foundation_accordion().