- <?php
- * @file
- * Field Group module.
- *
- * For an overview of all php and javascript hooks, see field_group.api.php.
- *
- */
-
- require_once dirname(__FILE__) . '/field_group.groups.inc';
-
- * Implements hook_config_info().
- */
- function field_group_config_info() {
- $prefixes['field_group.field_group'] = array(
- 'name_key' => array('entity_type', 'bundle', 'mode', 'group_name'),
- 'label_callback' => 'field_group_config_label',
- 'group' => t('Field groups'),
- );
- return $prefixes;
- }
-
- * Given a bundle config file, display a unique label.
- */
- function field_group_config_label($config, $config_name) {
- list($entity_type_name, $bundle, $mode, $group_name) = explode('.', str_replace('field_group.field_group.', '', $config_name));
- $entity_type = entity_get_info($entity_type_name);
- $entity_label = $entity_type['label'];
- $bundle_label = isset($entity_type['bundles'][$bundle]['label']) ? $entity_type['bundles'][$bundle]['label'] : $bundle;
-
- return $entity_label . ' - ' . $bundle_label . ' - Mode: ' . $mode . ' - ' . $group_name;
- }
-
- * Implements hook_menu().
- */
- function field_group_menu() {
- $items = array();
-
-
-
- if (defined('MAINTENANCE_MODE')) {
- return $items;
- }
-
-
- foreach (entity_get_info() as $entity_type => $entity_info) {
- if (isset($entity_info['fieldable']) && $entity_info['fieldable']) {
- foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
- if (isset($bundle_info['admin'])) {
-
- $path = $bundle_info['admin']['path'];
-
-
-
-
-
-
-
- if (isset($bundle_info['admin']['bundle argument'])) {
- $bundle_arg = $bundle_info['admin']['bundle argument'];
- $bundle_pos = (string) $bundle_arg;
-
- }
- else {
- $bundle_arg = $bundle_name;
- $bundle_pos = '0';
- }
-
-
-
- $group_position = count(explode('/', $path)) + 1;
-
-
- $access = array_intersect_key($bundle_info['admin'], backdrop_map_assoc(array(
- 'access callback', 'access arguments',
- )));
- $access += array(
- 'access callback' => 'user_access',
- 'access arguments' => array('administer fields'),
- );
-
-
-
-
-
- if ($access['access callback'] != 'user_access' || $access['access arguments'] != array('administer fields')) {
- $access = array(
- 'access callback' => 'field_ui_admin_access',
- 'access arguments' => array(
- $access['access callback'],
- $access['access arguments'],
- $bundle_pos,
- $bundle_arg,
- ),
- );
- }
-
- $items["$path/groups/%field_group_menu"] = array(
- 'load arguments' => array(
- $entity_type, $bundle_arg, $bundle_pos, '%map',
- ),
- 'title callback' => 'field_group_menu_title',
- 'title arguments' => array($group_position),
- 'page callback' => 'backdrop_get_form',
- 'page arguments' => array(
- 'field_group_configure_form', $group_position,
- ),
- 'file' => 'field_group.field_ui.inc',
- ) + $access;
- $items["$path/groups/%field_group_menu/configure"] = array(
- 'load arguments' => array(
- $entity_type, $bundle_arg, $bundle_pos, '%map',
- ),
- 'title' => 'Edit',
- 'page callback' => 'backdrop_get_form',
- 'page arguments' => array(
- 'field_group_configure_form', $group_position,
- ),
- 'type' => MENU_DEFAULT_LOCAL_TASK,
- 'file' => 'field_group.field_ui.inc',
- ) + $access;
- $items["$path/groups/%field_group_menu/format-type"] = array(
- 'load arguments' => array(
- $entity_type, $bundle_arg, $bundle_pos, '%map',
- ),
- 'title' => 'Format type',
- 'page callback' => 'backdrop_get_form',
- 'page arguments' => array(
- 'field_group_format_type_form', $group_position,
- ),
- 'type' => MENU_LOCAL_TASK,
- 'file' => 'field_group.field_ui.inc',
- ) + $access;
- $items["$path/groups/%field_group_menu/delete"] = array(
- 'load arguments' => array(
- $entity_type, $bundle_arg, $bundle_pos, '%map',
- ),
- 'title' => 'Delete',
- 'page callback' => 'backdrop_get_form',
- 'page arguments' => array(
- 'field_group_delete_form', $group_position,
- ),
- 'type' => MENU_CALLBACK,
- 'file' => 'field_group.field_ui.inc',
- ) + $access;
-
- $items["$path/groups/%field_group_menu/enable"] = array(
- 'load arguments' => array(
- $entity_type, $bundle_arg, $bundle_pos, '%map',
- ),
- 'title' => 'Enable',
- 'page callback' => 'backdrop_get_form',
- 'page arguments' => array(
- 'field_group_enable_form', $group_position,
- ),
- 'type' => MENU_CALLBACK,
- 'file' => 'field_group.field_ui.inc',
- ) + $access;
- }
- }
- }
- }
-
- return $items;
- }
-
- * Title callback for configure form.
- *
- * @param object $group
- * Group definition.
- *
- * @return string
- *
- * @see field_group_menu()
- */
- function field_group_menu_title($group) {
- return $group->label;
- }
-
- * Implements hook_permission().
- */
- function field_group_permission() {
- return array(
- 'administer fieldgroups' => array(
- 'title' => t('Administer field groups'),
- 'description' => t('Display the administration for field groups.'),
- ),
- );
- }
-
- * Menu Wildcard loader function to load group definitions.
- *
- * @param string $group_name
- * The name of the group, as contained in the path.
- * @param string $entity_type
- * The name of the entity.
- * @param string $bundle_name
- * The name of the bundle, as contained in the path.
- * @param int $bundle_pos
- * The position of $bundle_name in $map.
- * @param array $map
- * The translated menu router path argument map.
- *
- * @return object
- * Group definition.
- */
- function field_group_menu_load($group_name, $entity_type, $bundle_name, $bundle_pos, array $map) {
-
- $args = func_get_args();
- $args_pop = array_pop($args);
-
-
- $mode = count($args_pop) == 9 ? array_pop($args_pop) : 'form';
-
- if ($bundle_pos > 0) {
- $bundle = $map[$bundle_pos];
- $bundle_name = field_extract_bundle($entity_type, $bundle);
- }
-
- $group = field_group_load_field_group($group_name, $entity_type, $bundle_name, $mode);
- return empty($group) ? FALSE : $group;
- }
-
- * Loads a group definition.
- *
- * @param string $group_name
- * The name of the group.
- * @param string $entity_type
- * The name of the entity.
- * @param string $bundle_name
- * The name of the bundle.
- * @param string $mode
- * The view mode to load.
- *
- * @return object
- * Group definition.
- */
- function field_group_load_field_group($group_name, $entity_type, $bundle_name, $mode) {
-
- $config = config('field_group.field_group.' . $entity_type . '.' . $bundle_name . '.' . $mode . '.' . $group_name);
- $object = (object) $config->get();
-
- return $object;
- }
-
- * Implements hook_theme_registry_alter().
- */
- function field_group_theme_registry_alter(&$theme_registry) {
-
-
- $entity_info = entity_get_info();
- $entities = array();
- foreach ($entity_info as $entity => $info) {
- if (isset($entity_info[$entity]['fieldable']) && $entity_info[$entity]['fieldable']) {
-
- if ($entity == 'user') $entity = 'user_profile';
- $entities[] = $entity;
- }
- }
-
-
- if (isset($theme_registry['file_entity'])) {
- $entities[] = 'file_entity';
- }
-
-
- if (isset($theme_registry['entity'])) {
- $entities[] = 'entity';
- }
-
-
- if (isset($theme_registry['entity_plus'])) {
- $entities[] = 'entity_plus';
- }
-
- foreach ($entities as $entity) {
- if (isset($theme_registry[$entity])) {
- $theme_registry[$entity]['preprocess functions'][] = 'field_group_build_entity_groups';
- }
- }
- }
-
- * Implements hook_field_attach_delete_bundle().
- */
- function field_group_field_attach_delete_bundle($entity_type, $bundle) {
-
- $list = field_group_read_groups(array(
- 'bundle' => $bundle, 'entity_type' => $entity_type,
- ));
-
-
- if (isset($list[$entity_type], $list[$entity_type][$bundle])) {
- foreach ($list[$entity_type][$bundle] as $group_mode => $groups) {
- foreach ($groups as $group) {
- $config = config('field_group.field_group.' . $entity_type . '.' . $bundle . '.' . $group_mode . '.' . $group->group_name);
- $config->delete();
- }
- }
- }
-
- }
-
- * Implements hook_field_attach_form().
- */
- function field_group_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
-
- $form['#attached']['css'][] = backdrop_get_path('module', 'field_group') . '/css/field_group.field_ui.css';
- field_group_attach_groups($form, 'form', $form_state);
- $form['#pre_render'][] = 'field_group_form_pre_render';
- }
-
- * Implements hook_form_FORM_ID_alter().
- *
- * Using hook_form_field_ui_field_overview_form_alter.
- */
- function field_group_form_field_ui_field_overview_form_alter(&$form, &$form_state) {
- form_load_include($form_state, 'inc', 'field_group', 'field_group.field_ui');
- field_group_field_ui_overview_form_alter($form, $form_state);
- }
-
- * Implements hook_form_FORM_ID_alter().
- *
- * Using hook_form_field_ui_display_form_alter.
- */
- function field_group_form_field_ui_display_form_alter(&$form, &$form_state) {
- form_load_include($form_state, 'inc', 'field_group', 'field_group.field_ui');
- field_group_field_ui_overview_form_alter($form, $form_state, TRUE);
- }
-
- * Implements hook_field_attach_view_alter().
- */
- function field_group_field_attach_view_alter(&$element, $context) {
-
-
- $actual_mode = 'default';
- if (isset($element['#entity_type']) && isset($element['#bundle'])) {
- $view_mode_settings = field_view_mode_settings($element['#entity_type'], $element['#bundle']);
- $view_mode = $context['view_mode'];
- $actual_mode = (!empty($view_mode_settings[$view_mode]['custom_settings']) ? $view_mode : 'default');
- field_group_attach_groups($element, $actual_mode);
- }
- }
-
- * Implements hook_field_group_format_settings().
- *
- * If the group has no format settings, default ones will be added.
- */
- function field_group_field_group_format_settings($group) {
-
- $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 (isset($formatter['format_types'])) {
- $form['formatter'] = array(
- '#title' => t('Display type'),
- '#type' => 'select',
- '#options' => backdrop_map_assoc($formatter['format_types']),
- '#default_value' => isset($group->format_settings['formatter']) ? $group->format_settings['formatter'] : $formatter['default_formatter'],
- '#weight' => -4,
- );
- }
-
- if (isset($formatter['instance_settings']['required_fields']) && $mode == 'form') {
- $form['instance_settings']['required_fields'] = array(
- '#type' => 'checkbox',
- '#title' => t('Mark group as required if it contains required fields.'),
- '#default_value' => isset($group->format_settings['instance_settings']['required_fields']) ? $group->format_settings['instance_settings']['required_fields'] : (isset($formatter['instance_settings']['required_fields']) ? $formatter['instance_settings']['required_fields'] : ''),
- '#weight' => 2,
- );
- }
-
- if (isset($formatter['instance_settings']['id'])) {
- $form['instance_settings']['id'] = array(
- '#title' => t('ID attribute'),
- '#type' => 'textfield',
- '#default_value' => isset($group->format_settings['instance_settings']['id']) ? $group->format_settings['instance_settings']['id'] : (isset($formatter['instance_settings']['id']) ? $formatter['instance_settings']['id'] : ''),
- '#weight' => 10,
- '#element_validate' => array('field_group_validate_id'),
- );
- }
- if (isset($formatter['instance_settings']['classes'])) {
- $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'] : (isset($formatter['instance_settings']['classes']) ? $formatter['instance_settings']['classes'] : ''),
- '#weight' => 11,
- '#element_validate' => array('field_group_validate_css_class'),
- );
- }
-
- if (isset($formatter['instance_settings']['description'])) {
- $form['instance_settings']['description'] = array(
- '#title' => t('Description'),
- '#type' => 'textarea',
- '#default_value' => isset($group->format_settings['instance_settings']['description']) ? $group->format_settings['instance_settings']['description'] : (isset($formatter['instance_settings']['description']) ? $formatter['instance_settings']['description'] : ''),
- '#weight' => 0,
- );
- }
-
- return $form;
- }
-
- * Helper function to prepare basic variables needed for most formatters.
- *
- * Called in field_group_field_group_pre_render(), but can also be called in
- * other implementations of hook_field_group_pre_render().
- *
- * @param object $group
- * The group object passed by reference.
- */
- function field_group_pre_render_prepare(&$group) {
-
- $classes = _field_group_get_html_classes($group);
-
- $group->classes = implode(' ', $classes->required);
- $group->description = !empty($group->format_settings['instance_settings']['description']) ? filter_xss_admin(t($group->format_settings['instance_settings']['description'])) : '';
-
- }
-
- * Implements hook_field_group_pre_render().
- */
- function field_group_field_group_pre_render(&$element, &$group, &$form) {
-
- field_group_pre_render_prepare($group);
-
- $view_mode = isset($form['#view_mode']) ? $form['#view_mode'] : 'form';
-
-
- $form['#attached']['js'][] = array(
- 'data' => array('field_group' => array($group->format_type => $view_mode)),
- 'type' => 'setting',
- );
-
- if (isset($group->format_settings['instance_settings']['id']) && !empty($group->format_settings['instance_settings']['id'])) {
- $element['#id'] = backdrop_html_id($group->format_settings['instance_settings']['id']);
- }
-
- $element['#weight'] = $group->weight;
-
-
- $function = "field_group_pre_render_" . str_replace("-", "_", $group->format_type);
- if (function_exists($function)) {
- $function($element, $group, $form);
- }
-
- }
-
- * Remove empty groups on forms.
- *
- * @param string $name
- * The name of the element.
- * @param array $element
- * The element to check the empty state.
- * @param array $groups
- * Array of group objects.
- * @param array $form_groups
- * Array of form group objects.
- * @param string $entity
- * The entity name.
- */
- function field_group_remove_empty_form_groups($name, array &$element, array $groups, array &$form_groups, $entity) {
-
- $children = element_children($element);
-
- $hasChildren = FALSE;
- if (count($children)) {
- foreach ($children as $child_name) {
-
- if (in_array($child_name, $groups, TRUE)) {
- field_group_remove_empty_form_groups($child_name, $element[$child_name], $groups, $form_groups, $entity);
- }
- $hasChildren = $hasChildren ? TRUE : _field_group_is_empty_element($element, $entity, $child_name, $groups);
-
- }
- }
-
- if (!$hasChildren) {
-
-
- if (empty($element) && isset($form_groups[$name]) && !is_array($form_groups[$name])) {
- foreach ($form_groups as $group_name => $group) {
- if (isset($group->children)) {
- $group_children = array_flip($group->children);
- if (isset($group_children[$name])) {
- unset($form_groups[$group_name]->children[$group_children[$name]]);
- }
- }
- }
- }
-
- $element['#access'] = FALSE;
-
- }
-
- }
-
- * Determine if an element has non-empty children.
- *
- * @param array $element
- * The element to check.
- * @param string $entity
- * The entity type name.
- * @param string $child_name
- * The name of the child element.
- * @param array $groups
- * Array of group objects.
- *
- * @return bool
- * TRUE if element has non-empty children, FALSE otherwise.
- */
- function _field_group_is_empty_element(array $element, $entity, $child_name, array $groups) {
-
- $exceptions = array('user__account', 'comment__author');
- $exception = $entity . '__' . $child_name;
-
- if (in_array($exception, $exceptions)) {
- return TRUE;
- }
-
- if (isset($element[$child_name]['#type'])
- || isset($element[$child_name]['#markup'])
- || isset($element[$child_name]['#prefix'])
- || isset($element[$child_name]['#suffix'])
- ) {
- return TRUE;
- }
-
-
-
- if (in_array($child_name, $groups)) {
- return FALSE;
- }
-
- $children = element_children($element[$child_name]);
-
- foreach ($children as $child) {
- if (_field_group_is_empty_element($element[$child_name], $entity, $child, $groups)) {
- return TRUE;
- }
- }
-
- return FALSE;
-
- }
-
- * Remove empty groups on entity display.
- *
- * @param array $element
- * The element to check the empty state.
- * @param array $groups
- * Array of group objects.
- *
- * @return bool
- * TRUE if group is empty, FALSE otherwise.
- */
- function field_group_remove_empty_display_groups(array &$element, array $groups) {
-
- $empty_child = TRUE;
- $empty_group = TRUE;
-
-
- foreach (element_children($element) as $name) {
-
-
- if (in_array($name, $groups)) {
- $empty_child = field_group_remove_empty_display_groups($element[$name], $groups);
- if (!$empty_child) {
- $empty_group = FALSE;
- }
- }
-
-
- elseif (!empty($element[$name]) && (!isset($element[$name]['#access']) || $element[$name]['#access'])) {
- $empty_group = FALSE;
- }
-
- }
-
-
- if ($empty_group) {
- $element = NULL;
- }
-
- return $empty_group;
-
- }
-
- * Implements hook_field_group_format_summary().
- */
- function field_group_field_group_format_summary($group) {
-
- $group_form = (array) module_invoke_all('field_group_format_settings', $group);
-
- backdrop_alter('field_group_format_settings', $group_form, $group);
-
- $output = '';
- if (isset($group->format_settings['formatter'])) {
- $output .= '<strong>' . $group->format_type . '</strong> ' . $group->format_settings['formatter'] . '';
- }
- if (isset($group->format_settings['instance_settings'])) {
- $last = end($group->format_settings['instance_settings']);
- $output .= '<br />';
- foreach ($group->format_settings['instance_settings'] as $key => $value) {
- if (!is_numeric($value) && empty($value)) {
- continue;
- }
-
- $output .= '<strong>' . $key . '</strong> ';
-
- if (isset($group_form['instance_settings'], $group_form['instance_settings'][$key]['#options'])) {
- if (is_array($value)) {
- $value = implode(', ', array_filter($value));
- } else {
- $value = $group_form['instance_settings'][$key]['#options'][$value];
- }
- }
-
-
- if (backdrop_strlen($value) > 38) {
- $value = truncate_utf8($value, 50, TRUE, TRUE);
- }
-
- elseif (is_numeric($value)) {
- $value = $value == '1' ? t('yes') : t('no');
- }
- $output .= check_plain($value);
- $output .= $last == $value ? ' ' : '<br />';
- }
- }
- return $output;
- }
-
- * Implements hook_field_extra_fields().
- */
- function field_group_field_extra_fields() {
- $extra = array();
-
- $extra['user']['user'] = array('form' => array());
-
-
- if (config_get('system.core', 'user_pictures')) {
- $extra['user']['user']['form']['picture'] = array(
- 'label' => t('Picture'),
- 'description' => t('User picture'),
- 'weight' => 5,
- );
- }
-
-
- if (module_exists('contact')) {
- $extra['user']['user']['form']['contact'] = array(
- 'label' => t('Contact'),
- 'description' => t('Contact user element'),
- 'weight' => 5,
- );
- }
-
-
- if (module_exists('locale')) {
- $extra['user']['user']['form']['locale'] = array(
- 'label' => t('Language settings'),
- 'description' => t('Language settings for the user account.'),
- 'weight' => 5,
- );
- }
-
- return $extra;
- }
-
- * Implements hook_field_attach_rename_bundle().
- */
- function field_group_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
- $prefix = 'field_group.field_group.' . $entity_type;
- $field_group_config_names = config_get_names_with_prefix("$prefix.$bundle_old.");
- foreach ($field_group_config_names as $config_name) {
- $mode_field_group = str_replace("$prefix.$bundle_old.", '', $config_name);
- $field_group_old = config("$prefix.$bundle_old.$mode_field_group");
- $field_group_new = config("$prefix.$bundle_new.$mode_field_group");
- $data = $field_group_old->get();
- $data['bundle'] = $bundle_new;
- $field_group_new->setData($data);
- $field_group_new->save();
- $field_group_old->delete();
- }
-
-
- field_cache_clear();
- entity_info_cache_clear();
- }
-
- * Get all groups that match.
- *
- * @param string $entity_type
- * The name of the entity type.
- * @param string $bundle
- * The name of the bundle.
- * @param string $view_mode
- * The view mode.
- * @param bool $reset
- * Whether to reset the cache or not.
- *
- * @return array
- * Array of group objects.
- */
- function field_group_info_groups($entity_type = NULL, $bundle = NULL, $view_mode = NULL, $reset = FALSE) {
- static $groups = FALSE;
-
- if (!$groups || $reset) {
- $groups = field_group_read_groups();
- }
-
- if (!isset($entity_type)) {
- return $groups;
- }
- elseif (!isset($bundle) && isset($groups[$entity_type])) {
- return $groups[$entity_type];
- }
- elseif (!isset($view_mode) && isset($groups[$entity_type][$bundle])) {
- return $groups[$entity_type][$bundle];
- }
- elseif (isset($groups[$entity_type][$bundle][$view_mode])) {
- return $groups[$entity_type][$bundle][$view_mode];
- }
- return array();
- }
-
- * Read all groups.
- *
- * @param array $conditions
- * Parameters for the query, as elements of the $conditions array.
- * 'entity_type' The name of the entity type.
- * 'bundle' The name of the bundle.
- * 'mode' The view mode.
- *
- * @return array
- * Array of group objects.
- */
- function field_group_read_groups(array $conditions = array()) {
-
- $groups = array();
-
- $records = array();
-
- $config_names = config_get_names_with_prefix('field_group.field_group');
- foreach ($config_names as $config_name) {
- $config = config($config_name);
- $field = (object) $config->get();
- $records[$config_name] = $field;
-
-
- if (!empty($conditions)) {
- foreach ($conditions as $key => $value) {
- if ($field->$key != $value) {
- unset($records[$config_name]);
- }
- }
- }
- }
-
- foreach ($records as $group) {
- $groups[$group->entity_type][$group->bundle][$group->mode][$group->group_name] = $group;
- }
- backdrop_alter('field_group_info', $groups);
- return $groups;
-
- }
-
- * Checks if a field_group exists in required context.
- *
- * @param string $group_name
- * The name of the group.
- * @param string $entity_type
- * The name of the entity.
- * @param string $bundle
- * The bundle for the entity.
- * @param string $mode
- * The view mode context the group will be rendered.
- *
- * @return bool
- * TRUE if group exists, FALSE otherwise.
- */
- function field_group_exists($group_name, $entity_type, $bundle, $mode) {
- $groups = field_group_read_groups();
- return !empty($groups[$entity_type][$bundle][$mode][$group_name]);
- }
-
- * Delete a field group.
- *
- * @param object $group
- * A group definition.
- */
- function field_group_group_delete($group) {
-
- $config = config('field_group.field_group.' . $group->entity_type . '.' . $group->bundle . '.' . $group->mode . '.' . $group->group_name);
- $config->delete();
-
- cache_clear_all('field_groups', 'cache_field');
- module_invoke_all('field_group_delete_field_group', $group);
-
- }
-
- * Saves a group definition.
- *
- * @param object $group
- * A group definition.
- */
- function field_group_group_save(&$group, $new = TRUE) {
-
-
- $object = $group;
-
- if ($new == FALSE) {
-
- module_invoke_all('field_group_update_field_group', $object);
- }
- else {
-
- module_invoke_all('field_group_create_field_group', $object);
- }
-
- $config = config('field_group.field_group.' . $object->entity_type . '.' . $object->bundle . '.' . $object->mode . '.' . $object->group_name);
- $config->setData((array) $object);
- $config->save();
- }
-
- * Retrieve all format possibilities for the field groups.
- *
- * @return array|null
- * Array of formatters or NULL. Attempts to retrieve from static cache if set.
- */
- function field_group_formatter_info() {
- $cache = &backdrop_static(__FUNCTION__, array());
- if (empty($cache)) {
- if ($cached = cache_get('field_group_formatter_info', 'cache_field')) {
- $formatters = $cached->data;
- }
- else {
- $formatters = array();
- $formatters += (array) module_invoke_all('field_group_formatter_info');
- $hidden_region = array(
- 'label' => '<' . t('Hidden') . '>',
- 'description' => '',
- 'format_types' => array(),
- 'instance_settings' => array(),
- 'default_formatter' => '',
- );
- $formatters['display']['hidden'] = $hidden_region;
- cache_set('field_group_formatter_info', $formatters, 'cache_field');
- }
- $cache = $formatters;
- }
- return $cache;
- }
-
- * Attach groups to the (form) build.
- *
- * @param array $element
- * The part of the form.
- * @param string $view_mode
- * The mode for the build.
- */
- function field_group_attach_groups(array &$element, $view_mode) {
-
- $entity_type = $element['#entity_type'];
- $bundle = $element['#bundle'];
-
- $element['#groups'] = field_group_info_groups($entity_type, $bundle, $view_mode);
- $element['#fieldgroups'] = $element['#groups'];
-
-
- $group_children = array();
- foreach ($element['#groups'] as $group_name => $group) {
- if (!empty($group->children)) {
- foreach ($group->children as $child) {
- $group_children[$child] = $group_name;
- }
- }
- }
- $element['#group_children'] = $group_children;
-
- }
-
- * Pre render callback for rendering groups.
- *
- * @param array $element
- * Form that is being rendered.
- *
- * @return array
- * Array with re-arranged fields in forms.
- *
- * @see field_group_field_attach_form
- */
- function field_group_form_pre_render(array &$element) {
- return field_group_build_entity_groups($element, 'form');
- }
-
- * Preprocess/ Pre-render callback.
- *
- * @param array $vars
- * Preprocess vars or form element.
- * @param string $type
- * The type of object being rendered.
- *
- * @return array
- * Array with re-arranged fields in forms.
- *
- * @see field_group_form_pre_render()
- * @see field_group_theme_registry_alter
- * @see field_group_fields_nest()
- */
- function field_group_build_entity_groups(array &$vars, $type) {
-
- if ($type == 'form') {
- $element = &$vars;
- $nest_vars = NULL;
- }
- else {
- $element = &$vars['elements'];
- $nest_vars = &$vars;
- }
-
-
- if (empty($element['#fieldgroups'])) {
- return $element;
- }
-
-
- field_group_fields_nest($element, $nest_vars);
-
-
-
- if (isset($element['#node']->content) && count($element['#node']->content) > 0) {
- $element['#node']->content = array();
- }
-
- $display = isset($element['#view_mode']);
- $groups = array_keys($element['#groups']);
-
-
- if ($display) {
- field_group_remove_empty_display_groups($element, $groups);
- }
- else {
-
- field_group_remove_empty_form_groups('form', $element, $groups, $element['#groups'], $element['#entity_type']);
- }
-
-
- $element['#attached']['js'][] = backdrop_get_path('module', 'field_group') . '/js/field_group.js';
-
-
- backdrop_alter('field_group_build_pre_render', $element);
-
-
- if ($type == 'form') {
- return $element;
- }
-
-
- if (empty($element['#fieldgroups'])) {
- return $element;
- }
-
-
- foreach ($element['#fieldgroups'] as $group) {
- if (!empty($element[$group->group_name]) && $type != 'user_profile') {
- $vars['content'][$group->group_name] = $element[$group->group_name];
- }
- elseif (!empty($element[$group->group_name])) {
- $vars['user_profile'][$group->group_name] = $element[$group->group_name];
- }
- }
-
-
- backdrop_process_attached($element);
- }
-
- * Recursive function to nest fields in the field groups.
- *
- * Will take out all the elements in the form and
- * place them in the correct container element, a fieldgroup.
- * The current group element in the loop is passed recursively so we can
- * stash fields and groups in it while we go deeper in the array.
- *
- * @param array $element
- * The current element to analyse for grouping.
- * @param array|null $vars
- * Rendering vars from the entity being viewed.
- */
- function field_group_fields_nest(array &$element, ?array &$vars = NULL) {
-
-
- $group_references = array();
- foreach ($element['#fieldgroups'] as $group_name => $group) {
-
- if (is_string($group_name)) {
-
-
- $element[$group_name] = array();
- $group_references[$group_name] = &$element[$group_name];
- }
- }
-
-
-
-
- $element_clone = array();
- foreach (element_children($element) as $child_name) {
- $element_clone[$child_name] = $element[$child_name];
-
- if (isset($element['#group_children'][$child_name])) {
- $element_clone[$element['#group_children'][$child_name]] = array();
- }
- }
- $element = array_merge($element_clone, $element);
-
-
-
-
- foreach ($element['#group_children'] as $child_name => $parent_name) {
-
-
- if ($vars) {
-
- if (!isset($element['#fieldgroups'][$child_name]) && isset($vars['content'][$child_name])) {
- $group_references[$parent_name][$child_name] = $vars['content'][$child_name];
- unset($vars['content'][$child_name]);
- }
- elseif (!isset($element['#fieldgroups'][$child_name]) && isset($vars['user_profile'][$child_name])) {
- $group_references[$parent_name][$child_name] = $vars['user_profile'][$child_name];
- unset($vars['user_profile'][$child_name]);
- }
-
-
- else {
- $group_references[$parent_name][$child_name] = &$element[$child_name];
- unset($element[$child_name]);
- }
- }
-
- else {
-
-
-
-
- if (isset($element[$child_name]) && (!isset($element[$child_name]['#access']) || $element[$child_name]['#access'])) {
-
-
- $group_references[$parent_name][$child_name] = &$element[$child_name];
- $group_references[$parent_name]['#weight'] = $element['#fieldgroups'][$parent_name]->weight;
- }
-
-
-
- unset($element[$child_name]);
- }
-
- }
-
-
-
- foreach ($element['#fieldgroups'] as $group_name => $group) {
- field_group_pre_render($group_references[$group_name], $group, $element);
- }
-
- }
-
- * Pre render the field group element.
- *
- * @param array $element
- * Array of group element that needs to be created.
- * @param object $group
- * Object with the group information.
- * @param array $form
- * An associative array containing the structure of the form.
- *
- * @see field_group_fields_nest()
- */
- function field_group_pre_render(array &$element, $group, array &$form) {
-
-
-
- if ($element == array() || !array_diff(array_keys($element), array(
- '#array_parents', '#field_parents',
- ))) {
- return;
- }
-
-
-
- foreach (module_implements('field_group_pre_render') as $module) {
- $function = $module . '_field_group_pre_render';
- if (function_exists($function)) {
-
-
-
- $function($element, $group, $form);
- }
- }
-
-
- backdrop_alter('field_group_pre_render', $element, $group, $form);
-
- }
-
- * Hides field groups including children in a render array.
- *
- * @param array $element
- * A render array. Can be a form, node, user, ...
- * @param array $group_names
- * An array of field group names that should be hidden.
- */
- function field_group_hide_field_groups(array &$element, array $group_names) {
- foreach ($group_names as $group_name) {
- if (isset($element['#fieldgroups'][$group_name]) && isset($element['#group_children'])) {
-
- $element['#fieldgroups'][$group_name]->format_type = 'hidden';
-
- $sub_groups = array();
- foreach (array_keys($element['#group_children'], $group_name) as $field_name) {
- if (isset($element['#fieldgroups'][$field_name])) {
- $sub_groups[] = $field_name;
- } else {
- $element[$field_name]['#access'] = FALSE;
- }
- }
- field_group_hide_field_groups($element, $sub_groups);
- }
- }
- }
-
- * Calculates html classes for a group.
- *
- * @param object $group
- * The Group definition.
- *
- * @return object
- * The HTML classes calculated for the field group.
- *
- * @see hook_field_group_html_classes_alter()
- */
- function _field_group_get_html_classes(&$group) {
-
- if (isset($group->format_settings['formatter'])) {
- $group->collapsible = in_array($group->format_settings['formatter'], array(
- 'collapsible', 'collapsed',
- ));
- $group->collapsed = in_array($group->format_settings['formatter'], array(
- 'collapsed', 'closed',
- ));
- }
-
- $classes = new stdClass();
-
-
- $optional = array(str_replace('_', '-', $group->group_name));
- $required = array();
-
- $optional[] = 'field-group-' . $group->format_type;
-
- if (isset($group->format_settings['formatter']) && $group->collapsible) {
- $required[] = 'collapsible';
- if ($group->collapsed) {
- $required[] = 'collapsed';
- }
- }
-
- if (isset($group->format_settings['instance_settings'])) {
-
-
- if (!empty($group->format_settings['instance_settings']['required_fields'])) {
- $required[] = 'required-fields';
- }
-
-
- if (!empty($group->format_settings['instance_settings']['classes'])) {
- $required[] = check_plain($group->format_settings['instance_settings']['classes']);
- }
- }
-
- $classes->required = $required;
- $classes->optional = $optional;
-
- backdrop_alter('field_group_html_classes', $classes, $group);
-
- return $classes;
- }
-
- * Get the default formatter settings for a given formatter and a mode.
- *
- * @param string $format_type
- * The name of the format type.
- * @param string $mode
- * The mode type.
- *
- * @return array
- * An array of the default formatter and instance settings.
- */
- function _field_group_get_default_formatter_settings($format_type, $mode) {
-
- $field_group_types = field_group_formatter_info();
- $display_mode = $mode == 'form' ? 'form' : 'display';
- $formatter = $field_group_types[$display_mode][$format_type];
-
- return array(
- 'formatter' => isset($formatter['default_formatter']) ? $formatter['default_formatter'] : '',
- 'instance_settings' => $formatter['instance_settings'],
- );
- }
-
- * Preserve instance settings that are shared when switching the format type.
- *
- * @param array $new_instances
- * The default instance settings of the new format type.
- * @param array $original_instances
- * The original instance settings.
- * @param object $group
- * The group being updated.
- *
- * @return array
- * Updated instance settings.
- */
- function _field_group_preserve_shared_instance_settings(array $new_instances, array $original_instances, $group) {
- foreach ($new_instances as $new_instance_key => $new_instance) {
- if (empty($new_instance) && !empty($original_instances[$new_instance_key])) {
- $new_instances[$new_instance_key] = $original_instances[$new_instance_key];
- }
- }
-
-
-
- if (!empty($new_instances['classes'])) {
- $classes = explode(' ', $new_instances['classes']);
- foreach ($classes as $class_key => $class) {
- if (substr($class, 0, 12) === 'field-group-') {
- $classes[$class_key] = 'field-group-' . $group->format_type;
- }
- }
- $new_instances['classes'] = implode(' ', $classes);
- }
-
- return $new_instances;
- }
-
- * Implements hook_field_group_formatter_info().
- */
- function field_group_field_group_formatter_info() {
-
- $formatters = _field_group_field_group_formatter_info();
- return array(
- 'form' => $formatters['form'],
- 'display' => $formatters['display'],
- );
- }
-
- * Implements hook_field_group_format_settings_alter().
- */
- function field_group_field_group_format_settings_alter(&$form, &$group) {
-
- _field_group_field_group_format_settings_alter($form, $group);
- }
-
- * Implements hook_field_group_build_pre_render_alter().
- */
- function field_group_field_group_build_pre_render_alter(&$element) {
-
- _field_group_field_group_build_pre_render_alter($element);
- }
-
- * Implements hook_field_group_field_ui_parent_requirements_alter().
- */
- function field_group_field_group_field_ui_parent_requirements_alter(&$parent_requirements, &$context) {
-
- _field_group_field_group_field_ui_parent_requirements_alter($parent_requirements, $context);
- }
-
- * Implements hook_field_group_html_classes_alter().
- */
- function field_group_field_group_html_classes_alter(&$classes, &$group) {
-
- _field_group_field_group_html_classes_alter($classes, $group);
- }