- <?php
- * @file
- * Defines some field group types.
- */
-
- * Field group formatters.
- *
- * @see field_group_field_group_formatter_info()
- */
- function _field_group_field_group_formatter_info() {
-
- return array(
- 'form' => array(
- 'html-element' => array(
- 'label' => t('HTML element'),
- 'description' => t('This field group renders the inner content in a HTML element with classes and attributes.'),
- 'instance_settings' => array(
- 'description' => '',
- 'wrapper' => '', 'wrapper_other' => '', 'show_label' => 0,
- 'label_element' => 'h2', 'label_element_other' => '',
- 'classes' => '', 'attributes' => '',
- 'required_fields' => 1, 'id' => '',
- ),
- ),
- 'fieldset' => array(
- 'label' => t('Fieldset'),
- 'description' => t('This field group renders the inner content in a fieldset with the title as legend.'),
- 'format_types' => array('open', 'collapsible', 'collapsed'),
- 'instance_settings' => array(
- 'description' => '', 'classes' => '', 'required_fields' => 1,
- 'id' => '',
- ),
- 'default_formatter' => 'open',
- ),
- 'details' => array(
- 'label' => t('Details'),
- 'description' => t('This field group renders the inner content in a Details element with the title as summary.'),
- 'format_types' => array('open', 'closed'),
- 'instance_settings' => array(
- 'description' => '', 'classes' => '', 'required_fields' => 1,
- 'id' => '', 'label_element' => 'span', 'name' => '',
- ),
- 'default_formatter' => 'closed',
- ),
- 'tabs' => array(
- 'label' => t('Vertical tabs group'),
- 'description' => t('This field group renders child groups in its own vertical tabs wrapper.'),
- 'instance_settings' => array('classes' => '', 'id' => ''),
- ),
- 'tab' => array(
- 'label' => t('Vertical tab'),
- 'description' => t('This field group renders the content in a fieldset, part of vertical tabs group.'),
- 'format_types' => array('open', 'closed'),
- 'instance_settings' => array(
- 'description' => '', 'classes' => '', 'required_fields' => 1,
- ),
- 'default_formatter' => 'closed',
- ),
- ),
- 'display' => array(
- 'html-element' => array(
- 'label' => t('HTML element'),
- 'description' => t('This field group renders the inner content in a HTML element with classes and attributes.'),
- 'instance_settings' => array(
- 'description' => '',
- 'wrapper' => '', 'wrapper_other' => '', 'show_label' => 0,
- 'label_element' => 'h2', 'label_element_other' => '',
- 'classes' => '', 'attributes' => '',
- 'required_fields' => 1, 'id' => '',
- ),
- ),
- 'fieldset' => array(
- 'label' => t('Fieldset'),
- 'description' => t('This field group renders the inner content in a fieldset with the title as legend.'),
- 'format_types' => array('open', 'collapsible', 'collapsed'),
- 'instance_settings' => array(
- 'description' => '', 'classes' => '', 'id' => '',
- ),
- 'default_formatter' => 'open',
- ),
- 'details' => array(
- 'label' => t('Details'),
- 'description' => t('This field group renders the inner content in a Details element with the title as summary.'),
- 'format_types' => array('open', 'closed'),
- 'instance_settings' => array(
- 'description' => '', 'label_element' => 'span', 'classes' => '',
- 'required_fields' => 1, 'id' => '', 'name' => '',
- ),
- 'default_formatter' => 'closed',
- ),
- 'tabs' => array(
- 'label' => t('Vertical tabs group'),
- 'description' => t('This field group renders child groups in its own vertical tabs wrapper.'),
- 'instance_settings' => array('classes' => '', 'id' => ''),
- ),
- 'tab' => array(
- 'label' => t('Vertical tab'),
- 'description' => t('This field group renders the content in a fieldset, part of vertical tabs group.'),
- 'format_types' => array('open', 'closed'),
- 'instance_settings' => array('description' => '', 'classes' => ''),
- 'default_formatter' => 'closed',
- ),
- ),
- );
- }
-
- * Implements field_group_pre_render_<format-type>.
- * Format type: Fieldset.
- *
- * @param array $element
- * The field group form element.
- * @param object $group
- * The Field group object prepared for pre_render.
- * @param array $form
- * The root element or form.
- */
- function field_group_pre_render_fieldset(array &$element, $group, array &$form) {
-
- $element += array(
- '#type' => 'fieldset',
- '#title' => check_plain(t($group->label)),
- '#collapsible' => $group->collapsible,
- '#collapsed' => $group->collapsed,
- '#pre_render' => array(),
- '#attributes' => array('class' => explode(' ', $group->classes)),
- '#description' => $group->description,
- );
-
- if ($group->collapsible || $group->collapsed) {
- $element['#attached']['library'][] = array('system', 'backdrop.collapse');
- }
- }
-
- * Implements field_group_pre_render_<format-type>.
- * Format type: Details.
- *
- * @param array $element
- * The field group form element.
- * @param object $group
- * The Field group object prepared for pre_render.
- * @param array $form
- * The root element or form.
- */
- function field_group_pre_render_details(array &$element, $group, array &$form) {
- $label_element = isset($group->format_settings['instance_settings']['label_element']) ? $group->format_settings['instance_settings']['label_element'] : 'span';
- $element += array(
- '#type' => 'details',
- '#summary' => '<' . $label_element . '>' . check_plain(t($group->label)) . '</' . $label_element . '>',
- '#attributes' => array('class' => explode(' ', $group->classes)),
- '#details' => $group->description,
- );
- if (!empty($element['#id'])) {
- $element['#attributes']['id'] = $element['#id'];
- }
- if (!empty($group->format_settings['instance_settings']['name'])) {
- $element['#attributes']['name'] = $group->format_settings['instance_settings']['name'];
- }
- if (!$group->collapsed) {
- $element['#attributes']['open'] = TRUE;
- }
- }
-
- * Implements field_group_pre_render_<format-type>.
- * Format type: HTML element.
- *
- * @param array $element
- * The field group form element.
- * @param object $group
- * The Field group object prepared for pre_render.
- * @param array $form
- * The root element or form.
- */
- function field_group_pre_render_html_element(array &$element, $group, array &$form) {
- $html_element = isset($group->format_settings['instance_settings']['wrapper_other']) ? $group->format_settings['instance_settings']['wrapper_other'] : '';
- $wrapper = $group->format_settings['instance_settings']['wrapper'];
- if (!empty($wrapper) && $wrapper != 'other') {
- $html_element = $wrapper;
- }
- $show_label = isset($group->format_settings['instance_settings']['show_label']) ? $group->format_settings['instance_settings']['show_label'] : 0;
- $label_element = isset($group->format_settings['instance_settings']['label_element_other']) ? $group->format_settings['instance_settings']['label_element_other'] : '';
- $label = $group->format_settings['instance_settings']['label_element'];
- if (!empty($label) && $label != 'other') {
- $label_element = $label;
- }
- $configured_attributes = isset($group->format_settings['instance_settings']['attributes']) ? ' ' . $group->format_settings['instance_settings']['attributes'] : '';
- $group->classes = trim($group->classes);
-
-
-
- preg_match_all('/([^\s=]+)="([^"]+)"/', $configured_attributes, $matches);
-
- $element_attributes = array();
-
- foreach ($matches[1] as $key => $attribute) {
- $element_attributes[$attribute] = $matches[2][$key];
- }
-
-
- if (!isset($element_attributes['class']) && $group->classes) {
- $element_attributes['class'] = $group->classes;
- }
- elseif (isset($element_attributes['class']) && $group->classes) {
- $element_attributes['class'] .= ' ' . $group->classes;
- }
-
- if (isset($element['#id'])) {
- $element_attributes['id'] = $element['#id'];
- }
-
-
- if (function_exists('filter_xss_attributes')) {
-
- $attributes = backdrop_attributes(filter_xss_attributes($element_attributes));
- }
- else {
-
- $element_attributes = _filter_xss_attributes(backdrop_attributes($element_attributes));
- $attributes = $element_attributes ? ' ' . implode(' ', $element_attributes) : '';
- }
-
- $element['#prefix'] = '<' . $html_element . $attributes . '>';
- if ($show_label) {
- $element['#prefix'] .= '<' . $label_element . ' class="field-group-label">' . check_plain(t($group->label)) . '</' . $label_element . '>';
- }
- if (!empty($group->description)) {
- $element['#prefix'] .= '<div class="description">' . $group->description . '</div>';
- }
- $element['#suffix'] = '</' . $html_element . '>';
- }
-
- * Implements field_group_pre_render_<format-type>.
- * Format type: Vertical tabs wrapper.
- *
- * @param array $element
- * The field group form element.
- * @param object $group
- * The Field group object prepared for pre_render.
- * @param array $form
- * The root element or form.
- */
- function field_group_pre_render_tabs(array &$element, $group, array &$form) {
-
- $classes = 'field-group-' . $group->format_type . '-wrapper';
- if (!empty($group->classes)) {
- $classes .= ' ' . $group->classes;
- }
-
- $id = !empty($element['#id']) ? ' id="' . $element['#id'] . '"' : '';
-
- $element += array(
- '#type' => 'vertical_tabs',
- '#theme_wrappers' => array('vertical_tabs'),
- '#prefix' => '<div class="' . $classes . '"' . $id . '>',
- '#suffix' => '</div>',
- );
-
-
-
- if (!empty($group->label)) {
- $element['#title'] = check_plain($group->label);
- }
-
- $element[$group->group_name . '__active_tab'] = array(
- '#type' => 'hidden',
- '#default_value' => '',
- '#attributes' => array('class' => array('vertical-tabs-active-tab')),
- );
-
- $element['#attached']['library'][] = array('system', 'backdrop.collapse');
- }
-
- * Implements field_group_pre_render_<format-type>.
- * Format type: Vertical tab.
- *
- * @param array $element
- * The field group form element.
- * @param object $group
- * The Field group object prepared for pre_render.
- * @param array $form
- * The root element or form.
- */
- function field_group_pre_render_tab(array &$element, $group, array &$form) {
-
- $view_mode = isset($form['#view_mode']) ? $form['#view_mode'] : 'form';
-
- $form['#attached']['js'][] = array(
- 'data' => array('field_group' => array('tabs' => $view_mode)),
- 'type' => 'setting',
- );
-
- $add = array(
- '#type' => 'fieldset',
- '#id' => 'edit-' . $group->group_name,
- '#title' => check_plain(t($group->label)),
- '#collapsible' => $group->collapsible,
- '#collapsed' => $group->collapsed,
- '#attributes' => array('class' => explode(" ", $group->classes)),
- '#description' => $group->description,
- );
-
-
-
- if ($view_mode != 'form') {
- $add['#group'] = empty($group->parent_name) ? 'additional_settings' : $group->parent_name;
- $add['#parents'] = array($add['#group']);
- $element += $add;
- }
-
-
- elseif (!empty($group->parent_name)) {
- $add['#group'] = $group->parent_name;
- $element += $add;
- }
-
-
- else {
-
- $add['#parents'] = array('additional_settings');
- $add['#group'] = 'additional_settings';
- $add['#weight'] = -30 + $group->weight;
-
-
-
- if (isset($form['additional_settings']['group']['#groups']['additional_settings'])) {
-
-
- $form['additional_settings']['group']['#groups']['additional_settings'][$group->group_name] = $add;
- $form['additional_settings']['group']['#groups'][$group->group_name] = array('#group_exists' => TRUE);
-
- foreach (element_children($element) as $fieldname) {
- $form['additional_settings']['group']['#groups']['additional_settings'][$group->group_name][$fieldname] = &$element[$fieldname];
- unset($element[$fieldname]);
- }
- }
-
-
-
- else {
- if (!isset($form['additional_settings']['#type'])) {
- $form['additional_settings'] = array(
- '#type' => 'vertical_tabs',
- '#weight' => $group->weight,
- '#theme_wrappers' => array('vertical_tabs'),
- '#prefix' => '<div class="field-group-' . $group->format_type . '-wrapper">',
- '#suffix' => '</div>',
- );
- $form['#attached']['library'][] = array('system', 'backdrop.collapse');
- }
- $form['additional_settings'][$group->group_name] = $add;
-
- foreach (element_children($element) as $fieldname) {
- $form['additional_settings'][$group->group_name][$fieldname] = &$element[$fieldname];
- unset($element[$fieldname]);
- }
- }
- }
-
- }
-
- * Field group format settings
- *
- * @see field_group_field_group_format_settings_alter()
- */
- function _field_group_field_group_format_settings_alter(&$form, &$group) {
- $field_group_types = field_group_formatter_info();
- $mode = $group->mode == 'form' ? 'form' : 'display';
- $formatter = $field_group_types[$mode][$group->format_type];
-
- if ($mode == 'display') {
- $select_name = 'fields[' . $group->group_name . '][format_settings][settings][instance_settings]';
- } else {
- $select_name = 'instance_settings';
- }
-
-
- switch ($group->format_type) {
- case 'html-element':
- $form['instance_settings']['wrapper'] = array(
- '#title' => t('Wrapper tag'),
- '#type' => 'select',
- '#options' => array(
- 'section' => t('Section'), 'article' => t('Article'),
- 'header' => t('Header'), 'footer' => t('Footer'),
- 'aside' => t('Aside'), 'div' => t('Div'), 'other' => t('Other'),
- ),
- '#default_value' => isset($group->format_settings['instance_settings']['wrapper']) ? $group->format_settings['instance_settings']['wrapper'] : 'section',
- '#description' => t('The HTML element to use as a wrapper for the fields.'),
- );
-
- $form['instance_settings']['wrapper_other'] = array(
- '#title' => t('Other wrapper'),
- '#type' => 'textfield',
- '#default_value' => isset($group->format_settings['instance_settings']['wrapper_other']) ? $group->format_settings['instance_settings']['wrapper_other'] : $formatter['instance_settings']['wrapper_other'],
- '#description' => t('For example: <code>p</code>, <code>span</code>.'),
- '#weight' => 1,
- '#indentation' => 1,
- '#states' => array(
- 'visible' => array(
- 'select[name="' . $select_name . '[wrapper]"]' => array('value' => 'other'),
- ),
- ),
- '#element_validate' => array('field_group_format_settings_other_tag_validate'),
- );
-
- $form['instance_settings']['show_label'] = array(
- '#title' => t('Show label'),
- '#type' => 'select',
- '#options' => array(0 => t('No'), 1 => t('Yes')),
- '#default_value' => isset($group->format_settings['instance_settings']['show_label']) ? $group->format_settings['instance_settings']['show_label'] : $formatter['instance_settings']['show_label'],
- '#weight' => 2,
- );
-
- $form['instance_settings']['label_element'] = array(
- '#title' => t('Label tag'),
- '#type' => 'select',
- '#options' => array(
- 'h2' => t('Header 2'),
- 'h3' => t('Header 3'),
- 'div' => t('Div'),
- 'p' => t('P'),
- 'span' => t('Span'),
- 'other' => t('Other'),
- ),
- '#default_value' => isset($group->format_settings['instance_settings']['label_element']) ? $group->format_settings['instance_settings']['label_element'] : $formatter['instance_settings']['label_element'],
- '#description' => t('An HTML element to use for the label.'),
- '#weight' => 3,
- '#indentation' => 1,
- '#states' => array(
- 'visible' => array(
- 'select[name="' . $select_name . '[show_label]"]' => array('value' => 1),
- ),
- ),
- );
-
- $form['instance_settings']['label_element_other'] = array(
- '#title' => t('Other label tag'),
- '#type' => 'textfield',
- '#default_value' => isset($group->format_settings['instance_settings']['label_element_other']) ? $group->format_settings['instance_settings']['label_element_other'] : '',
- '#description' => t('An alternative HTML element to use for the label, without the angle brackets. For example, <code>h4</code>, <code>strong</code>.'),
- '#weight' => 4,
- '#indentation' => 1,
- '#states' => array(
- 'visible' => array(
- 'select[name="' . $select_name . '[show_label]"]' => array('value' => 1),
- 'select[name="' . $select_name . '[label_element]"]' => array('value' => 'other'),
- ),
- ),
- '#element_validate' => array('field_group_format_settings_other_tag_validate'),
- );
-
- $form['instance_settings']['attributes'] = array(
- '#title' => t('Attributes'),
- '#type' => 'textfield',
- '#default_value' => isset($group->format_settings['instance_settings']['attributes']) ? $group->format_settings['instance_settings']['attributes'] : $formatter['instance_settings']['attributes'],
- '#description' => t('For example, <code>name="MY-ANCHOR"</code> to create an anchor that can be used in a link from elsewhere.'),
- '#weight' => 5,
- );
- break;
- case 'fieldset':
- $form['label']['#description'] = t('Please enter a label for collapsible elements');
- break;
- case 'details':
- $form['label']['#description'] = t('Please enter a label for the summary');
- $form['instance_settings']['label_element'] = array(
- '#title' => t('Label element'),
- '#type' => 'select',
- '#options' => array(
- 'span' => t('Span'), 'strong' => t('Strong'), 'em' => t('Emphasis'),
- 'h2' => t('Header 2'), 'h3' => t('Header 3')),
- '#default_value' => isset($group->format_settings['instance_settings']['label_element']) ? $group->format_settings['instance_settings']['label_element'] : $formatter['instance_settings']['label_element'],
- '#weight' => 2,
- );
- $form['instance_settings']['name'] = array(
- '#title' => t('Name attribute'),
- '#type' => 'textfield',
- '#default_value' => isset($group->format_settings['instance_settings']['name']) ? $group->format_settings['instance_settings']['name'] : $formatter['instance_settings']['name'],
- '#description' => t('The name attribute specifies a group name, as per the <a href="@url">HTML specification</a>. Give multiple <code>details</code> elements the same name value to group them. Only one of the grouped <code>details</code> elements can be open at a time — opening one will cause another to close — so do not use if that behaviour is not desired. If multiple grouped <code>details</code> elements are given the open attribute, only the first one in the source order will be rendered open.', array('@url' => 'https://html.spec.whatwg.org/multipage/interactive-elements.html#the-details-element')),
- '#weight' => 5,
- );
- break;
- case 'tabs':
- break;
- case 'tab':
- default:
- }
- }
-
- * Validate the label. Label is required for fieldsets that are collapsible.
- *
- * @param array $element
- * The element that has the label.
- * @param array $form_state
- * An associative array containing the current state of the form.
- */
- function field_group_format_settings_other_tag_validate(array $element, array &$form_state) {
- $display_overview = (isset($form_state['build_info']['form_id']) && $form_state['build_info']['form_id'] == 'field_ui_display_form') ? TRUE : FALSE;
- $value = '';
- $validate = FALSE;
- $required_error = FALSE;
- $tag_error = FALSE;
-
- if ($display_overview) {
- $group = $form_state['values']['fields'][$element['#parents'][1]];
- $settings = $group['format_settings']['settings']['instance_settings'];
- } else {
- $settings = $form_state['values']['instance_settings'];
- }
-
- $parents = $element['#parents'];
- $element_name = implode('][', $element['#parents']);
- $field_name = array_pop($parents);
- $main_field_name = substr($field_name, 0, -6);
- if ($settings[$main_field_name] == 'other') {
- $validate = TRUE;
- $value = $settings[$field_name];
- if (!$value) {
- $required_error = TRUE;
- }
- if (!preg_match('/^[a-z][a-z0-9-]*$/i', $value)) {
- $tag_error = TRUE;
- }
- }
-
- if ($required_error) {
- form_set_error($element_name, t('The %field_title field is required.', array('%field_title' => $element['#title'])));
- }
- if ($validate && $tag_error) {
- form_set_error($element_name, t('The %field_title field should be a valid HTML element name <em>without</em> the angle brackets.', array('%field_title' => $element['#title'])));
- }
- }
-
- * Pre render alter
- *
- * @see field_group_field_group_build_pre_render_alter()
- */
- function _field_group_field_group_build_pre_render_alter(&$element) {
-
- $element['#attached']['js'][] = backdrop_get_path('module', 'field_group') . '/js/field_groups.js';
- }
-
- * Parent requirements.
- *
- * @see field_group_field_group_field_ui_parent_requirements_alter()
- */
- function _field_group_field_group_field_ui_parent_requirements_alter(&$parent_requirements, &$context) {
-
- if ($context['display_overview']) {
- $parent_requirements['tab'] = array(
- 'parent' => 'tabs',
- 'message' => 'Each <em>Vertical tab</em> element needs to have a parent <em>Vertical tabs</em> group element.',
- );
- } else {
- $parent_requirements['tab'] = array(
- 'parent' => 'tabs',
- 'message' => 'A <em>Vertical tab</em> element without a parent will be shown in the <em>Additional settings</em> tab group. If this was unintended, create a new parent <em>Vertical tabs</em> group element.',
- );
- }
- }
-
- * Alters classes.
- *
- * @see field_group_field_group_html_classes_alter()
- */
- function _field_group_field_group_html_classes_alter(&$classes, &$group) {
-
- if ($group->format_type == 'tab') {
- $classes->required[] = 'collapsible';
- }
- }