bootstrap_fieldgroup.module in Bootstrap fieldgroup 7
Module file for the bootstrap_fieldgroup module.
File
bootstrap_fieldgroup.moduleView source
<?php
// $Id$
/**
* @file
* Module file for the bootstrap_fieldgroup module.
*/
/**
* Implements hook_ctools_plugin_api().
*/
function bootstrap_fieldgroup_ctools_plugin_api($module, $api) {
if ($module == 'field_group' && $api == 'field_group') {
return array(
'version' => 1,
);
}
}
/**
* Implements hook_theme().
*/
function bootstrap_fieldgroup_theme() {
return array(
'bootstrap_fieldgroup_nav' => array(
'template' => 'templates/bootstrap-fieldgroup-nav',
'variables' => array(
'items' => array(),
'group' => NULL,
),
),
'bootstrap_fieldgroup_accordion' => array(
'template' => 'templates/bootstrap-fieldgroup-accordion',
'variables' => array(
'items' => array(),
'group' => NULL,
),
),
);
}
/**
* Implements hook_field_group_formatter_info().
*/
function bootstrap_fieldgroup_field_group_formatter_info() {
$formatters = array();
$formatters['bootstrap_fieldgroup_nav'] = array(
'label' => t('Bootstrap nav: tabs/pills'),
'description' => t('This fieldgroup renders child groups as Bootstrap nav tabs/pills'),
'instance_settings' => array(
'classes' => '',
'bootstrap_nav_type' => 'tabs',
'bootstrap_stacked' => 0,
'bootstrap_orientation' => 0,
),
);
$formatters['bootstrap_fieldgroup_nav_item'] = array(
'label' => t('Bootstrap nav: item'),
'description' => t('This fieldgroup renders the content as a Bootstrap nav item'),
'instance_settings' => array(
'classes' => '',
),
);
$formatters['bootstrap_fieldgroup_accordion'] = array(
'label' => t('Bootstrap Accordion group'),
'description' => t('This fieldgroup renders child groups as Bootstrap Accordion.'),
'instance_settings' => array(
'classes' => '',
'id' => '',
),
);
$formatters['bootstrap_fieldgroup_accordion_item'] = array(
'label' => t('Bootstrap Accordion item'),
'format_types' => array(
'open',
'closed',
),
'description' => t('This fieldgroup renders the contentin a div, part of Bootstrap Accordion group.'),
'default_formatter' => 'closed',
'instance_settings' => array(
'classes' => '',
),
);
$formatters['bootstrap_grid_row'] = array(
'label' => t('Bootstrap Grid Row'),
'description' => t('This fieldgroup renders a Bootstrap row that is meant to include Bootstrap columns as child groups. It can optionally render a heading (title and description) as a child column.'),
'instance_settings' => array(
'classes' => '',
'show_heading' => 0,
'heading_position' => 0,
'label_element' => 'h3',
'description_element' => 'p',
'description' => '',
'panelize' => 0,
'bootstrap_grid_col_xs' => '',
'bootstrap_grid_col_sm' => '',
'bootstrap_grid_col_md' => '',
'bootstrap_grid_col_lg' => '',
),
);
$formatters['bootstrap_grid_col'] = array(
'label' => t('Bootstrap Grid Column'),
'description' => t('This fieldgroup renders the content inside a Bootstrap column with the specified column classes. It can optionally wrap the fields inside a panel.'),
'instance_settings' => array(
'classes' => '',
'panelize' => 0,
'bootstrap_grid_col_xs' => '',
'bootstrap_grid_col_sm' => '',
'bootstrap_grid_col_md' => '',
'bootstrap_grid_col_lg' => '',
),
);
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().
* If the group has no format settings, default ones will be added.
*/
function bootstrap_fieldgroup_field_group_format_settings($group) {
// Add a wrapper for extra settings to use by others.
$form = array(
'instance_settings' => array(),
);
$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 'bootstrap_fieldgroup_nav':
$form['instance_settings']['bootstrap'] = array(
'#tree' => TRUE,
);
$form['instance_settings']['bootstrap_nav_type'] = array(
'#title' => t('Nav type'),
'#type' => 'select',
'#options' => array(
'tabs' => 'Tabs',
'pills' => 'Pills',
),
'#default_value' => isset($group->format_settings['instance_settings']['bootstrap_nav_type']) ? $group->format_settings['instance_settings']['bootstrap_nav_type'] : $formatter['instance_settings']['bootstrap_nav_type'],
'#weight' => 1,
'#attributes' => array(
'class' => array(
'bootstrap-fieldgroup-nav-type',
),
),
);
$form['instance_settings']['bootstrap_stacked'] = array(
'#title' => t('Stacked'),
'#type' => 'select',
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
'#attributes' => array(
'class' => array(
'bootstrap-fieldgroup-stacked',
),
),
'#default_value' => isset($group->format_settings['instance_settings']['bootstrap_stacked']) ? $group->format_settings['instance_settings']['bootstrap_stacked'] : $formatter['instance_settings']['bootstrap_stacked'],
'#weight' => 1.5,
);
$form['instance_settings']['bootstrap_orientation'] = array(
'#title' => t('Position the tabs/pills will appear'),
'#type' => 'select',
'#options' => array(
0 => t('Top'),
1 => t('Right'),
2 => t('Bottom'),
3 => t('Left'),
),
'#attributes' => array(
'class' => array(
'bootstrap-fieldgroup-orientation',
),
),
'#default_value' => isset($group->format_settings['instance_settings']['bootstrap_orientation']) ? $group->format_settings['instance_settings']['bootstrap_orientation'] : $formatter['instance_settings']['bootstrap_orientation'],
'#weight' => 1.7,
);
break;
case 'bootstrap_fieldgroup_accordion':
case 'bootstrap_fieldgroup_accordion_item':
// For now - not use any optional instance_settings.
break;
case 'bootstrap_grid_row':
$form['instance_settings']['show_heading'] = array(
'#title' => t('Show heading'),
'#type' => 'select',
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
'#default_value' => isset($group->format_settings['instance_settings']['show_heading']) ? $group->format_settings['instance_settings']['show_heading'] : $formatter['instance_settings']['show_heading'],
'#weight' => 1,
);
$form['instance_settings']['heading_position'] = array(
'#title' => t('Heading Position'),
'#type' => 'select',
'#options' => array(
0 => t('Left'),
1 => t('Right'),
),
'#default_value' => isset($group->format_settings['instance_settings']['heading_position']) ? $group->format_settings['instance_settings']['heading_position'] : $formatter['instance_settings']['heading_position'],
'#weight' => 1,
);
$form['instance_settings']['label_element'] = array(
'#title' => t('Label element'),
'#type' => 'textfield',
'#default_value' => isset($group->format_settings['instance_settings']['label_element']) ? $group->format_settings['instance_settings']['label_element'] : $formatter['instance_settings']['label_element'],
'#weight' => 1,
);
$form['instance_settings']['description_element'] = array(
'#title' => t('Description element'),
'#type' => 'textfield',
'#default_value' => isset($group->format_settings['instance_settings']['description_element']) ? $group->format_settings['instance_settings']['description_element'] : $formatter['instance_settings']['description_element'],
'#weight' => 1,
);
$form['instance_settings']['panelize'] = array(
'#title' => t('Embed in a panel'),
'#type' => 'select',
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
'#default_value' => isset($group->format_settings['instance_settings']['panelize']) ? $group->format_settings['instance_settings']['panelize'] : $formatter['instance_settings']['panelize'],
'#weight' => 1,
);
$form['instance_settings']['bootstrap_grid_col_xs'] = _bootstrap_fieldgroup_grid_col_format_settings('xs', $group, $formatter, true);
$form['instance_settings']['bootstrap_grid_col_sm'] = _bootstrap_fieldgroup_grid_col_format_settings('sm', $group, $formatter, true);
$form['instance_settings']['bootstrap_grid_col_md'] = _bootstrap_fieldgroup_grid_col_format_settings('md', $group, $formatter, true);
$form['instance_settings']['bootstrap_grid_col_lg'] = _bootstrap_fieldgroup_grid_col_format_settings('lg', $group, $formatter, true);
break;
case 'bootstrap_grid_col':
$form['instance_settings']['panelize'] = array(
'#title' => t('Embed in a panel'),
'#type' => 'select',
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
'#default_value' => isset($group->format_settings['instance_settings']['panelize']) ? $group->format_settings['instance_settings']['panelize'] : $formatter['instance_settings']['panelize'],
'#weight' => 1,
);
$form['instance_settings']['bootstrap_grid_col_xs'] = _bootstrap_fieldgroup_grid_col_format_settings('xs', $group, $formatter);
$form['instance_settings']['bootstrap_grid_col_sm'] = _bootstrap_fieldgroup_grid_col_format_settings('sm', $group, $formatter);
$form['instance_settings']['bootstrap_grid_col_md'] = _bootstrap_fieldgroup_grid_col_format_settings('md', $group, $formatter);
$form['instance_settings']['bootstrap_grid_col_lg'] = _bootstrap_fieldgroup_grid_col_format_settings('lg', $group, $formatter);
break;
default:
}
return $form;
}
/**
* Implements field_group_pre_render_<format-type>.
* Format type: Bootstrap Accordion.
*
* @param $element The field group form element.
* @param $group The Field group object prepared for pre_render.
* @param $form The root element or form.
*/
function field_group_pre_render_bootstrap_fieldgroup_accordion(&$element, $group, &$form) {
$element += array(
'#type' => 'bootstrap_fieldgroup_accordion',
'#theme' => 'bootstrap_fieldgroup_accordion',
'#description' => $group->description,
'#parents' => array(
$group->parent_name,
),
'#attached' => array(
'js' => array(
drupal_get_path('module', 'bootstrap_fieldgroup') . '/bootstrap_fieldgroup.js',
),
),
);
}
/**
* Implements field_group_pre_render_<format-type>.
* Format type: Bootstrap Accordion item.
*
* @param $element The field group form element.
* @param $group The Field group object prepared for pre_render.
* @param $form The root element or form.
*/
function field_group_pre_render_bootstrap_fieldgroup_accordion_item(&$element, $group, &$form) {
$element += array(
'#type' => 'bootstrap_fieldgroup_accordion_item',
'#title' => check_plain(t($group->label)),
'#description' => $group->description,
'#weight' => $group->weight,
'#parents' => array(
$group->parent_name,
),
);
}
/**
* Implements hook_field_group_pre_render().
*/
function bootstrap_fieldgroup_field_group_pre_render(&$element, $group, &$form) {
$element['#group'] = $group;
switch ($group->format_type) {
case 'bootstrap_fieldgroup_accordion':
case 'bootstrap_fieldgroup_nav':
$items = array();
foreach ($group->children as $group_name) {
$items[$group_name] =& $element[$group_name];
}
$element['#items'] = $items;
break;
default:
}
}
/**
* Implements field_group_pre_render_<format-type>.
* Format type: Bootstrap nav tabs/pills.
*/
function field_group_pre_render_bootstrap_fieldgroup_nav(&$element, $group, &$form) {
$element += array(
'#type' => 'bootstrap_fieldgroup_nav',
'#theme' => 'bootstrap_fieldgroup_nav',
'#description' => $group->description,
'#parents' => array(
$group->parent_name,
),
'#attached' => array(
'js' => array(
drupal_get_path('module', 'bootstrap_fieldgroup') . '/bootstrap_fieldgroup.js',
),
),
);
}
/**
* Implements field_group_pre_render_<format-type>.
* Format type: Bootstrap nav item.
*/
function field_group_pre_render_bootstrap_fieldgroup_nav_item(&$element, $group, &$form) {
$element += array(
'#type' => 'bootstrap_fieldgroup_nav_item',
'#title' => check_plain(t($group->label)),
'#description' => $group->description,
'#weight' => $group->weight,
'#parents' => array(
$group->parent_name,
),
);
}
/**
* Implements template_preprocess_bootstrap_tabs().
* @param $variables
*/
function template_preprocess_bootstrap_fieldgroup_nav(&$variables) {
$group =& $variables['group'];
$variables['wrapper_classes'] = $variables['group']->classes;
$variables['wrapper_classes'] .= ' bootstrap-nav-wrapper';
$variables['flip'] = FALSE;
if ($group->format_settings['instance_settings']['bootstrap_orientation']) {
switch ($group->format_settings['instance_settings']['bootstrap_orientation']) {
case 1:
$variables['wrapper_classes'] .= ' tabs-right';
break;
case 2:
$variables['wrapper_classes'] .= ' tabs-below';
$variables['flip'] = TRUE;
break;
case 3:
$variables['wrapper_classes'] .= ' tabs-left';
break;
default:
}
}
$variables['nav_classes'] = $variables['group']->classes;
switch ($group->format_settings['instance_settings']['bootstrap_nav_type']) {
case 'tabs':
$variables['nav_classes'] .= ' nav-tabs';
break;
case 'pills':
$variables['nav_classes'] .= ' nav-pills';
break;
default:
}
if ($group->format_settings['instance_settings']['bootstrap_stacked']) {
$variables['nav_classes'] .= ' nav-stacked';
}
$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'])) {
if (!empty($item['#group'])) {
$label = $item['#group']->label;
}
else {
if (!empty($item['#title'])) {
$label = $item['#title'];
}
}
$id = drupal_html_id('bootstrap-fieldgroup-nav-item--' . $label);
// Is an explicit nav item?
if (!empty($item['#type']) && 'bootstrap_fieldgroup_nav_item' == $item['#type']) {
$classes = $item['#group']->classes;
}
else {
$classes = '';
}
$variables['navs'][] = array(
'content' => l(check_plain(t($label)), NULL, array(
'attributes' => array(
'data-toggle' => 'tab',
),
'fragment' => $id,
'external' => TRUE,
'html' => TRUE,
)),
'classes' => $classes,
);
$variables['panes'][] = array(
'id' => $id,
'content' => drupal_render($item),
);
}
}
// Config?
$variables['active'] = 0;
$variables['is_empty'] = empty($variables['navs']) && empty($variables['panes']);
$variables['is_single'] = count($variables['navs']) == 1;
}
/**
* Implements template_preprocess_bootstrap_tabs().
* @param $variables
*/
function template_preprocess_bootstrap_fieldgroup_accordion(&$variables) {
$group =& $variables['group'];
$variables['classes'] = $variables['group']->classes;
$variables['id'] = drupal_html_id('bootstrap-fieldgroup-accordion--' . $variables['group']->label);
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'])) {
$id = drupal_html_id('bootstrap-fieldgroup-accordion-item--' . $item['#title']);
$is_collapsed = !isset($item['#group']) || $item['#group']->format_settings['formatter'] == 'closed';
// Is an explicit accordion item?
if (!empty($item['#type']) && 'bootstrap_fieldgroup_accordion_item' == $item['#type']) {
$classes = $item['#group']->classes;
}
else {
$classes = '';
}
$variables['panels'][] = array(
'classes' => $classes,
'id' => $id,
'label' => check_plain(t($item['#title'])),
'link_classes' => $is_collapsed ? 'collapsed' : '',
'collapse' => $is_collapsed ? 'collapse' : 'collapse in',
'content' => drupal_render($item),
);
}
}
// Config?
$variables['active'] = 0;
$variables['is_empty'] = empty($variables['panels']);
$variables['is_single'] = count($variables['panels']) == 1;
}
/**
* Implements field_group_pre_render_<format-type>.
* Format type: Bootstrap Grid Row.
*
* @param $element The field group form element.
* @param $group The Field group object prepared for pre_render.
* @param $form The root element or form.
*/
function field_group_pre_render_bootstrap_grid_row(&$element, $group, &$form) {
// Get the textual format of the the options available (xs, sm, md, lg).
$screen_size_texts = _bootstrap_grid_col_screens();
$settings = $group->format_settings['instance_settings'];
// Add only the classes defined in the field group configuration. The rest
// will be omitted.
$col_classes = '';
foreach ($screen_size_texts as $screen_size => $text) {
if (!empty($settings['bootstrap_grid_col_' . $screen_size])) {
$col_classes .= 'col-' . $screen_size . '-' . $settings['bootstrap_grid_col_' . $screen_size] . ' ';
}
}
$prefix = '<div class="row ' . $group->classes . '">';
$suffix = '</div>';
// Wrap the fields in a panel if the panelize option is set to 1.
// The panel needs to be wrapped in a column so that margins align correctly.
if ($settings['panelize']) {
$prefix .= '<div class="col-xs-12"><div class="panel panel-default"><div class="panel-body">';
$suffix .= '</div></div></div>';
}
// If the show_heading configuration option is set to 1, add a column to
// display the title and the description of the field group.
if ($settings['show_heading']) {
// If heading_position is 1, render the column after sub-groups so that it
// shows on the right.
if ($settings['heading_position']) {
$suffix = '<div class="' . $col_classes . 'field-group-bootstrap_grid_row-heading">' . '<' . $settings['label_element'] . '>' . check_plain(t($group->label)) . '</' . $settings['label_element'] . '>' . '<' . $settings['description_element'] . '>' . $group->description . '</' . $settings['description_element'] . '>' . '</div>' . $suffix;
}
else {
$prefix .= '<div class="' . $col_classes . 'field-group-bootstrap_grid_row-heading">' . '<' . $settings['label_element'] . '>' . check_plain(t($group->label)) . '</' . $settings['label_element'] . '>' . '<' . $settings['description_element'] . '>' . $group->description . '</' . $settings['description_element'] . '>' . '</div>';
}
}
$element += array(
'#type' => 'markup',
'#prefix' => $prefix,
'#suffix' => $suffix,
);
}
/**
* Implements field_group_pre_render_<format-type>.
* Format type: Bootstrap Grid Column.
*
* @param $element The field group form element.
* @param $group The Field group object prepared for pre_render.
* @param $form The root element or form.
*/
function field_group_pre_render_bootstrap_grid_col(&$element, $group, &$form) {
// Get the textual format of the the options available (xs, sm, md, lg).
$screen_size_texts = _bootstrap_grid_col_screens();
$settings = $group->format_settings['instance_settings'];
// Add only the classes defined in the field group configuration. The rest
// will be omitted.
$col_classes = '';
foreach ($screen_size_texts as $screen_size => $text) {
if (!empty($settings['bootstrap_grid_col_' . $screen_size])) {
$col_classes .= 'col-' . $screen_size . '-' . $settings['bootstrap_grid_col_' . $screen_size] . ' ';
}
}
$prefix = '<div class="' . $col_classes . $group->classes . '">';
$suffix = '</div>';
// Wrap the fields in a panel if the panelize option is set to 1.
if ($settings['panelize']) {
$prefix .= '<div class="panel panel-default"><div class="panel-body">';
$suffix .= '</div></div>';
}
$element += array(
'#type' => 'markup',
'#prefix' => $prefix,
'#suffix' => $suffix,
);
}
/**
* Helper function that returns the format settings array for the
* bootstrap_grid_col_[xs,sm,md,lg] options used in bootstrap_grid_row and
* bootstrap_grid_col field group format types.
*
* @params string $screen The screen to which the option relates to (xs,sm,md,lg).
* @params object $group The Field group object.
* @params array $formatter The formatter used for the field group.
* @params boolean $is_for_heading Whether the options are for rendering the heading in a Bootstrap Grid Row
* or for the fields in a Bootstrap Grid Column.
*/
function _bootstrap_fieldgroup_grid_col_format_settings($type, $group, $formatter, $is_for_heading = FALSE) {
$screen_size_texts = _bootstrap_grid_col_screens();
$title = t('The number of columns the fieldgroup should span for ' . $screen_size_texts[$type] . ' screens.');
if ($is_for_heading) {
$title = t('The number of columns the heading of the fieldgroup should span for ' . $screen_size_texts[$type] . ' screens.');
}
return array(
'#title' => $title,
'#type' => 'select',
'#options' => _bootstrap_grid_col_options(),
'#default_value' => isset($group->format_settings['instance_settings']['bootstrap_grid_col_' . $type]) ? $group->format_settings['instance_settings']['bootstrap_grid_col_' . $type] : $formatter['instance_settings']['bootstrap_grid_col_' . $type],
);
}
/**
* Returns the choices available for the bootstrap_grid_col_[xs,sm,md,lg]
* configuration options. Provided by a function to avoid hardcoding them in
* multiple places in the code.
*/
function _bootstrap_grid_col_options() {
static $options;
if (!isset($options)) {
$options = array(
0 => null,
1 => '1',
2 => '2',
3 => '3',
4 => '4',
5 => '5',
6 => '6',
7 => '7',
8 => '8',
9 => '9',
10 => '10',
11 => '11',
12 => '12',
);
}
return $options;
}
/**
* Returns the class values suffices available for the bootstrap_grid_col_[xs,sm,md,lg]
* configuration options. Provided by a function to avoid hardcoding them in
* multiple places in the code.
*/
function _bootstrap_grid_col_screens() {
static $screens;
if (!isset($screens)) {
$screens = array(
'xs' => 'extra small',
'sm' => 'small',
'md' => 'medium',
'lg' => 'large',
);
}
return $screens;
}
Functions
Name![]() |
Description |
---|---|
bootstrap_fieldgroup_ctools_plugin_api | Implements hook_ctools_plugin_api(). |
bootstrap_fieldgroup_field_group_formatter_info | Implements hook_field_group_formatter_info(). |
bootstrap_fieldgroup_field_group_format_settings | Implements hook_field_group_format_settings(). If the group has no format settings, default ones will be added. |
bootstrap_fieldgroup_field_group_pre_render | Implements hook_field_group_pre_render(). |
bootstrap_fieldgroup_theme | Implements hook_theme(). |
field_group_pre_render_bootstrap_fieldgroup_accordion | Implements field_group_pre_render_<format-type>. Format type: Bootstrap Accordion. |
field_group_pre_render_bootstrap_fieldgroup_accordion_item | Implements field_group_pre_render_<format-type>. Format type: Bootstrap Accordion item. |
field_group_pre_render_bootstrap_fieldgroup_nav | Implements field_group_pre_render_<format-type>. Format type: Bootstrap nav tabs/pills. |
field_group_pre_render_bootstrap_fieldgroup_nav_item | Implements field_group_pre_render_<format-type>. Format type: Bootstrap nav item. |
field_group_pre_render_bootstrap_grid_col | Implements field_group_pre_render_<format-type>. Format type: Bootstrap Grid Column. |
field_group_pre_render_bootstrap_grid_row | Implements field_group_pre_render_<format-type>. Format type: Bootstrap Grid Row. |
template_preprocess_bootstrap_fieldgroup_accordion | Implements template_preprocess_bootstrap_tabs(). |
template_preprocess_bootstrap_fieldgroup_nav | Implements template_preprocess_bootstrap_tabs(). |
_bootstrap_fieldgroup_grid_col_format_settings | Helper function that returns the format settings array for the bootstrap_grid_col_[xs,sm,md,lg] options used in bootstrap_grid_row and bootstrap_grid_col field group format types. |
_bootstrap_grid_col_options | Returns the choices available for the bootstrap_grid_col_[xs,sm,md,lg] configuration options. Provided by a function to avoid hardcoding them in multiple places in the code. |
_bootstrap_grid_col_screens | Returns the class values suffices available for the bootstrap_grid_col_[xs,sm,md,lg] configuration options. Provided by a function to avoid hardcoding them in multiple places in the code. |