thierry-b
11/26/2013 - 10:43 AM

Fieldgroup unordered list

Fieldgroup unordered list

<?php
/**
 * @file
 *   Create a unordered list as field group.
 */

/**
 * Implements hook_field_group_formatter_info().
 */
function the_aim_ul_fieldgroup_field_group_formatter_info() {
  return array(
    'form' => array(
      'unordered' => array(
        'label' => t('Unordered list'),
        'description' => t('This fieldgroup renders the inner content in a unordered list, the inner items itselve needs to be formatted als list items.'),
        'instance_settings' => array('classes' => 'accordion-tabs'),
      ),
    ),
    'display' => array(
      'unordered' => array(
        'label' => t('Unordered list'),
        'description' => t('This fieldgroup renders the inner content in a unordered list, the inner items itselve needs to be formatted als list items.'),
        'instance_settings' => array('classes' => 'accordion-tabs'),
      ),
    ),
  );
}

/**
 * Implements hook_field_group_format_settings().
 *
 * @params Object $group The group object.
 * @return Array $form The form element for the format settings.
 */
function the_aim_ul_fieldgroup_field_group_format_settings($group) {
  $form = array();

  // Add optional instance_settings.
  switch ($group->format_type) {
    case 'unordered':
			$form['instance_settings'] = array(
			  '#tree' => TRUE,
			  '#weight' => 2,
			);
       $form['instance_settings']['classes'] = array(
        '#title' => t('Extra CSS classes'),
        '#type' => 'textfield',
        '#default_value' => isset($group->format_settings['instance_settings']['classes']) ? $group->format_settings['instance_settings']['classes'] : $formatter['instance_settings']['classes'],
        '#weight' => 3,
        '#element_validate' => array('field_group_validate_css_class'),
      );
      break;
  }

  return $form;
}

/**
 * Implements hook_field_group_pre_render().
 * 
 * @param Array $elements by address.
 * @param Object $group The Field group info.
 */
function the_aim_ul_fieldgroup_field_group_pre_render(&$element, $group, &$form) {

  switch ($group->format_type) {
    // Normal or collapsible div.
    case 'unordered':
      $children = element_children($element);
      foreach ($children as $key => $child) {
        if(is_null($element[$child])) {
          unset($element[$child]);
        } else {
          $element[$child]['#prefix'] = '<li>';
          $element[$child]['#suffix'] = '</li>';
        }
      }
      if(empty($element)) {
        unset($element);
      }
      else {
        $element += array(
          '#type' => 'markup',
          '#prefix' => '<ul class="field-group-format accordion-tabs' . $group->classes . '">',
          '#suffix' => '</ul>',
        );
      }

      break;
  }
}
name = The AIM - Custom unordered list field group
description = the Aim UL field group
core = 7.x
package = "The AIM"
dependencies[] = "field_group"