- <?php
- * @file
- * Field_group.field_ui.inc is a file that contains most functions
- * needed on the Fields UI Manage forms (display and fields).
- */
-
- * Helper function to get the form parameters to use while
- * building the fields and display overview form.
- *
- * @param array $form
- * An associative array containing the structure of the form.
- * @param bool $display_overview
- * Set to TRUE to get display overview form parameters.
- *
- * @return object
- * Group parameters.
- */
- function field_group_field_ui_form_params(array $form, $display_overview) {
-
- $params = new stdClass();
- $params->entity_type = $form['#entity_type'];
- $params->bundle = $form['#bundle'];
- $params->admin_path = _field_ui_bundle_admin_path($params->entity_type, $params->bundle);
- $params->display_overview = $display_overview;
-
- if ($display_overview) {
- $params->region_callback = 'field_group_display_overview_row_region';
- $params->mode = $form['#view_mode'];
- }
- else {
- $params->region_callback = 'field_group_field_overview_row_region';
- $params->mode = 'form';
- }
-
- $params->groups = field_group_info_groups($params->entity_type, $params->bundle, $params->mode, TRUE);
-
-
- $params->parents = array();
- foreach ($params->groups as $name => $group) {
- foreach ($group->children as $child) {
- $params->parents[$child] = $name;
- }
- }
-
- return $params;
- }
-
- * Alter the fields overview and display overview screen.
- *
- * @param array $form
- * An associative array containing the structure of the form, which is passed
- * by reference.
- * @param array $form_state
- * An associative array containing the current state of the form, which is
- * passed by reference.
- * @param bool $display_overview
- * Set to TRUE if altering the display overview.
- */
- function field_group_field_ui_overview_form_alter(array &$form, array &$form_state, $display_overview = FALSE) {
-
-
- if ($display_overview && empty($form['#fields']) && empty($form['#extra'])) {
- return;
- }
-
- $params = field_group_field_ui_form_params($form, $display_overview);
-
-
- if (!$display_overview) {
-
- $form['fields']['#prefix'] = '<div id="field-display-overview-wrapper">';
- $form['fields']['#suffix'] = '</div>';
- }
- $form['#groups'] = array_keys($params->groups);
-
- $table = &$form['fields'];
-
-
-
- if (isset($table['#regions'])) {
- $table['#regions'] += array(
- 'add_new' => array('title' => ' '),
- );
- }
-
-
- foreach ($params->groups as $name => $group) {
- $table['#parent_options'][$name] = $group->label;
- }
- $table['#parent_options']['_add_new_group'] = t('Add new group');
-
-
- foreach (element_children($table) as $name) {
- $table[$name]['parent_wrapper']['parent']['#options'] = $table['#parent_options'];
-
- if (empty($table[$name]['parent_wrapper']['parent']['#default_value'])) {
- $table[$name]['parent_wrapper']['parent']['#default_value'] = isset($params->parents[$name]) ? $params->parents[$name] : '';
- }
- }
-
- $field_group_types = field_group_formatter_info();
- $formatter_options = field_group_field_formatter_options($display_overview ? 'display' : 'form');
-
- $refresh_rows = isset($form_state['values']['refresh_rows']) ? $form_state['values']['refresh_rows'] : (isset($form_state['input']['refresh_rows']) ? $form_state['input']['refresh_rows'] : NULL);
-
-
- foreach (array_keys($params->groups) as $name) {
-
-
-
- if (isset($form_state['field_group'][$name])) {
- $group = & $form_state['field_group'][$name];
- }
- else {
- $group = & $params->groups[$name];
- }
-
-
-
-
-
- if (isset($refresh_rows) && $refresh_rows == $name) {
- $settings = isset($form_state['values']['fields'][$name]) ? $form_state['values']['fields'][$name] : (isset($form_state['input']['fields'][$name]) ? $form_state['input']['fields'][$name] : NULL);
- if (array_key_exists('settings_edit', $settings)) {
- $group = $form_state['field_group'][$name];
- }
- field_group_formatter_row_update($group, $settings);
- }
-
-
- if (!empty($form_state['values'][$name . '_formatter_settings_update'])) {
- field_group_formatter_settings_update($group, $form_state['values']['fields'][$name]);
- }
-
- $form_state['field_group'][$name] = $group;
-
- $settings = field_group_format_settings_form($group);
-
- $id = strtr($name, '_', '-');
- $js_rows_data[$id] = array('type' => 'group', 'name' => $name);
-
- $parent_options = $table['#parent_options'];
- unset($parent_options[$name]);
- $table[$name] = array(
- '#attributes' => array(
- 'class' => array(
- 'draggable', 'field-group',
- ),
- 'id' => $id,
- ),
- '#row_type' => 'group',
- '#region_callback' => $params->region_callback,
- '#js_settings' => array('rowHandler' => 'group'),
- 'human_name' => array(
- '#markup' => check_plain(t($group->label)),
- '#prefix' => '<span class="group-label">',
- '#suffix' => '</span>',
- ),
- 'weight' => array(
- '#type' => 'textfield',
- '#default_value' => $group->weight,
- '#size' => 3,
- '#attributes' => array('class' => array('field-weight')),
- ),
- 'parent_wrapper' => array(
- 'parent' => array(
- '#type' => 'select',
- '#options' => $parent_options,
- '#empty_value' => '',
- '#default_value' => isset($params->parents[$name]) ? $params->parents[$name] : '',
- '#attributes' => array('class' => array('field-parent')),
- '#parents' => array('fields', $name, 'parent'),
- ),
- 'hidden_name' => array(
- '#type' => 'hidden',
- '#default_value' => $name,
- '#attributes' => array('class' => array('field-name')),
- ),
- ),
- );
-
- $table[$name] += array(
- 'group_name' => array(
- '#markup' => check_plain($name),
- ),
- );
-
- if (!$display_overview) {
- $destination = _field_ui_bundle_admin_path($form['#entity_type'], $form['#bundle']);
- $field_group_types = field_group_formatter_info();
-
-
-
- $table[$name] += array(
- 'format' => array(
- 'type' => array(
- '#type' => 'link',
- '#title' => $field_group_types['form'][$group->format_type]['label'],
- '#href' => $params->admin_path . '/groups/' . $name . '/format-type/form',
- '#options' => array(
- 'query' => array('destination' => $destination . '/fields')),
- '#attributes' => array('class' => array('field-group-type')),
- ),
- ),
- );
-
-
-
- $table[$name]['settings_summary'] = array('#markup' => '');
- if (!empty($group->format_settings)) {
- $table[$name]['settings_summary'] = field_group_format_settings_summary($name, $group);
- }
-
- $destination = _field_ui_bundle_admin_path($form['#entity_type'], $form['#bundle']);
- $operations = array(
- 'configure' => array(
- 'title' => t('Configure'),
- 'href' => $params->admin_path . '/groups/' . $name . '/configure/form',
- 'query' => array('destination' => $destination . '/fields'),
- ),
- 'delete' => array(
- 'title' => t('Delete'),
- 'href' => $params->admin_path . '/groups/' . $name . '/delete/form',
- 'query' => array('destination' => $destination . '/fields'),
- ),
- );
-
-
- $table[$name]['operations'] = array(
- '#type' => 'dropbutton',
- '#links' => $operations,
- );
- }
- else {
-
- $table[$name] += array(
- 'format' => array(
- 'type' => array(
- '#type' => 'select',
- '#options' => $formatter_options,
- '#default_value' => $group->format_type,
- '#attributes' => array('class' => array('field-group-type')),
- ),
- ),
- );
-
- $base_button = array(
- '#submit' => array('field_ui_display_overview_multistep_submit'),
- '#ajax' => array(
- 'callback' => 'field_ui_display_overview_multistep_js',
- 'wrapper' => 'field-display-overview-wrapper',
- 'effect' => 'fade',
- ),
- '#field_name' => $name,
- );
-
- if ($form_state['formatter_settings_edit'] == $name) {
- $table[$name]['format']['#cell_attributes'] = array('colspan' => $display_overview ? 3 : 2);
- $format_type_label = $field_group_types['form'][$group->format_type]['label'] ?? '';
- $table[$name]['format']['format_settings'] = array(
- '#type' => 'container',
- '#attributes' => array('class' => array('field-formatter-settings-edit-form')),
- '#parents' => array('fields', $name, 'format_settings'),
- '#weight' => -5,
- 'label' => array(
- '#markup' => t('Field group format:') . ' <span class="formatter-name">' . $format_type_label . '</span>',
- ),
-
- 'settings' => $settings,
- 'actions' => array(
- '#type' => 'actions',
- 'save_settings' => $base_button + array(
- '#type' => 'submit',
- '#name' => $name . '_formatter_settings_update',
- '#value' => t('Update'),
- '#op' => 'update',
- ),
- 'cancel_settings' => $base_button + array(
- '#type' => 'submit',
- '#name' => $name . '_formatter_settings_cancel',
- '#value' => t('Cancel'),
- '#op' => 'cancel',
-
- '#limit_validation_errors' => array(),
- ),
- ),
- );
- $table[$name]['#attributes']['class'][] = 'field-formatter-settings-editing';
- $table[$name]['format']['type']['#attributes']['class'] = array('element-invisible');
- }
- else {
-
-
- $table[$name]['settings_summary'] = array('#markup' => '');
- if (!empty($group->format_settings)) {
- $table[$name]['settings_summary'] = field_group_format_settings_summary($name, $group);
- }
-
- $table[$name]['settings_edit'] = array(
- '#type' => 'container',
- '#attributes' => array(
- 'class' => array(
- 'field-formatter-settings-edit-wrapper',
- ),
- ),
- );
- $table[$name]['settings_edit']['edit'] = $base_button + array(
- '#type' => 'submit',
- '#name' => $name . '_group_settings_edit',
- '#value' => t('Configure'),
- '#attributes' => array(
- 'class' => array(
- 'field-formatter-settings-edit field-group-formatter-settings-edit button-secondary',
- ),
- 'alt' => t('Configure'),
- ),
- '#op' => 'edit',
-
- '#limit_validation_errors' => array(),
- );
- $table[$name]['settings_edit']['delete'] = array(
- '#type' => 'link',
- '#title' => t('Delete'),
- '#href' => '',
- '#attributes' => array('class' => array('field-formatter-settings-edit field-group-formatter-settings-delete button')),
- );
- $table[$name]['settings_edit']['delete']['#href'] = $params->admin_path . '/groups/' . $name . '/delete/' . $params->mode;
- }
- }
- }
-
-
- $parent_options = $table['#parent_options'];
- unset($parent_options['_add_new_group']);
- $table['_add_new_group'] = field_group_add_row('_add_new_group', $parent_options, $params);
-
- $table['_add_new_group'] += array(
- 'format' => array(
- 'type' => array(
- '#type' => 'select',
- '#options' => $formatter_options,
- '#default_value' => 'fieldset',
- '#description' => t('Groups can have unlimited nesting.'),
- '#prefix' => '<div class="add-new-placeholder"> </div>',
- ),
- '#cell_attributes' => array('colspan' => 3),
- ),
- );
-
- if (!$display_overview) {
-
- $form['refresh_rows'] = array('#type' => 'hidden');
- $form['refresh'] = array(
- '#type' => 'submit',
- '#value' => t('Refresh'),
- '#op' => 'refresh_table',
- '#submit' => array('field_ui_display_overview_multistep_submit'),
- '#ajax' => array(
- 'callback' => 'field_ui_display_overview_multistep_js',
- 'wrapper' => 'field-display-overview-wrapper',
- 'effect' => 'fade',
-
-
- 'progress' => 'none',
- ),
- );
- }
-
- $form['#attached']['css'][] = backdrop_get_path('module', 'field_group') . '/css/field_group.field_ui.css';
- $form['#attached']['js'][] = backdrop_get_path('module', 'field_group') . '/js/field_group.field_ui.js';
-
- $form['#validate'][] = 'field_group_field_overview_validate';
- $form['#submit'][] = 'field_group_field_overview_submit';
-
-
- field_group_field_ui_create_vertical_tabs($form, $form_state, $params);
-
-
- if ($form['#groups']) {
-
- $parent_requirements = array();
- $context = array(
- 'form' => $form,
- 'display_overview' => $display_overview,
- );
- backdrop_alter('field_group_field_ui_parent_requirements', $parent_requirements, $context);
-
- foreach ($form['#groups'] as $group_name) {
- $group_check = field_group_load_field_group($group_name, $params->entity_type, $params->bundle, $params->mode);
- if (isset($parent_requirements[$group_check->format_type])) {
- if (!$group_check->parent_name || field_group_load_field_group($group_check->parent_name, $params->entity_type, $params->bundle, $params->mode)->format_type != $parent_requirements[$group_check->format_type]['parent']) {
- backdrop_set_message(t($parent_requirements[$group_check->format_type]['message']), 'warning', FALSE);
- }
- }
- }
- }
- }
-
- * Return an array of field_group_formatter options.
- *
- * @param string $type
- * Name of field group type.
- *
- * @return array
- * Array of field group formatter options.
- */
- function field_group_field_formatter_options($type) {
- $options = &backdrop_static(__FUNCTION__);
-
- if (!isset($options)) {
- $options = array();
- $field_group_types = field_group_formatter_info();
- foreach ($field_group_types[$type] as $name => $field_group_type) {
- $options[$name] = $field_group_type['label'];
- }
- }
- return $options;
- }
-
- * Helper function to add a row in the overview forms.
- *
- * @param string $name
- * Machine name.
- * @param array $parent_options
- * An array of field group parents.
- * @param object $params
- * Group parameters.
- */
- function field_group_add_row($name, array $parent_options, $params) {
- return array(
- '#attributes' => array(
- 'class' => array('draggable', 'field-group', 'add-new'),
- ),
- '#row_type' => 'add_new_group',
- '#js_settings' => array('rowHandler' => 'group'),
- '#region_callback' => $params->region_callback,
- 'label' => array(
- '#title_display' => 'invisible',
- '#title' => t('Label for new group'),
- '#type' => 'textfield',
- '#size' => 15,
- '#description' => t('Label'),
- '#prefix' => '<div class="label-input"><div class="add-new-placeholder">' . t('Add new group') . '</div>',
- '#suffix' => '</div>',
- ),
- 'weight' => array(
- '#type' => 'textfield',
- '#default_value' => (int) field_info_max_weight($params->entity_type, $params->bundle, $params->mode) + 3,
- '#size' => 3,
- '#title_display' => 'invisible',
- '#title' => t('Weight for new group'),
- '#attributes' => array('class' => array('field-weight')),
- '#prefix' => '<div class="add-new-placeholder"> </div>',
- ),
- 'parent_wrapper' => array(
- 'parent' => array(
- '#title_display' => 'invisible',
- '#title' => t('Parent for new group'),
- '#type' => 'select',
- '#options' => $parent_options,
- '#empty_value' => '',
- '#attributes' => array('class' => array('field-parent')),
- '#prefix' => '<div class="add-new-placeholder"> </div>',
- '#parents' => array('fields', $name, 'parent'),
- ),
- 'hidden_name' => array(
- '#type' => 'hidden',
- '#default_value' => $name,
- '#attributes' => array('class' => array('field-name')),
- ),
- ),
- 'group_name' => array(
- '#type' => 'machine_name',
- '#title' => t('New field name'),
- '#title_display' => 'invisible',
-
- '#field_prefix' => '<span dir="ltr">group_',
- '#field_suffix' => '</span>‎',
- '#size' => 15,
- '#description' => t('A unique machine-readable name containing letters, numbers, and underscores.'),
-
- '#maxlength' => 26,
- '#prefix' => '<div class="add-new-placeholder"> </div>',
- '#machine_name' => array(
- 'source' => array('fields', $name, 'label'),
- 'exists' => '_field_group_group_name_exists',
- 'standalone' => TRUE,
- 'label' => '',
- ),
- '#required' => FALSE,
- '#cell_attributes' => array('colspan' => $params->display_overview ? 1 : 2),
- ),
- );
- }
-
- * Creates a form for field_group formatters.
- *
- * @param object $group
- * The Field Group object, which is passed by reference.
- *
- * @return array
- * An associative array containing the structure of the form.
- */
- function field_group_format_settings_form(&$group) {
- $form = array();
- $form['label'] = array(
- '#type' => 'textfield',
- '#title' => t('Field group label'),
- '#default_value' => $group->label,
- '#weight' => -5,
- '#element_validate' => array('field_group_format_settings_label_validate'),
- );
- $addition = (array) module_invoke_all('field_group_format_settings', $group);
-
- backdrop_alter('field_group_format_settings', $addition, $group);
-
- $form += $addition;
-
- if (!empty($addition['label']['#description'])) {
- $form['label']['#description'] = $addition['label']['#description'];
- }
-
- return $form;
- }
-
- * 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_label_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;
- if ($display_overview) {
- $group = $form_state['values']['fields'][$element['#parents'][1]];
- $settings = $group['format_settings']['settings'];
- $name = $form_state['formatter_settings_edit'];
- $form_state['values']['fields'][$name]['settings_edit_form']['settings'] = $settings;
- $collapse_error = $group['format']['type'] == 'fieldset' && ($settings['formatter'] == 'collapsed' || $settings['formatter'] == 'collapsible') && empty($settings['label']);
- $required_error = $group['format']['type'] == 'details' && empty($settings['label']);
- }
- else {
- $values = $form_state['values'];
-
- $group = $form_state['complete_form']['#group'];
- $collapse_error = $group->format_type == 'fieldset' && ($values['formatter'] == 'collapsed' || $values['formatter'] == 'collapsible') && empty($values['label']);
- $required_error = $group->format_type == 'details' && empty($values['label']);
- }
-
- if ($collapse_error) {
- form_error($element, t('The label is required when formatter is collapsible or collapsed'));
- }
- if ($required_error) {
- form_error($element, t('The label is required'));
- }
- }
-
- * Update the row so that the group variables are updated.
- * The rendering of the elements needs the updated defaults.
- *
- * @param object $group
- * Group definition, passed by reference.
- * @param array $settings
- * Configuration settings.
- */
- function field_group_formatter_row_update(&$group, array $settings) {
-
- if (!empty($settings['format']['type']) && $settings['format']['type'] != $group->format_type) {
- $group->format_type = $settings['format']['type'];
- field_group_formatter_settings_update($group, $settings);
- }
- }
-
- * Update handler for field_group configuration settings.
- *
- * @param object $group
- * The group definition object.
- * @param array $settings
- * Configuration settings.
- */
- function field_group_formatter_settings_update(&$group, array $settings) {
-
-
- if (empty($settings['format_settings']['settings'])) {
- $original_formatter_settings = $group->format_settings;
- $mode = $group->mode == 'form' ? 'form' : 'display';
- $group->format_settings = _field_group_get_default_formatter_settings($group->format_type, $mode);
-
-
- $group->format_settings['instance_settings'] = _field_group_preserve_shared_instance_settings($group->format_settings['instance_settings'], $original_formatter_settings['instance_settings'], $group);
-
- }
- else {
- $group->format_type = $settings['format']['type'];
- $group->label = $settings['format_settings']['settings']['label'];
- $group->format_settings = $settings['format_settings']['settings'];
- }
- }
-
- * Creates a summary for the field format configuration summary.
- *
- * @param string $group_name
- * The name of the group.
- * @param object $group
- * The group definition.
- *
- * @return array
- * Array of summary ready to be rendered.
- */
- function field_group_format_settings_summary($group_name, $group) {
- $summary = implode('<br />', (array) module_invoke_all('field_group_format_summary', $group));
- return array(
- '#markup' => '<div class="field-formatter-summary">' . $summary . '</div>',
- '#cell_attributes' => array('class' => array('field-formatter-summary-cell')),
- );
- }
-
- * Returns the region to which a row in the 'Manage fields' screen belongs.
- *
- * @param array $row
- * A field or field_group row.
- *
- * @return string
- * The current region.
- */
- function field_group_field_overview_row_region(array $row) {
- switch ($row['#row_type']) {
- case 'group':
- return 'main';
- case 'add_new_group':
-
-
- if (empty($row['label']['#value'])) {
- return 'add_new';
- }
- return 'main';
- }
- }
-
- * Returns the region to which a row in the 'Manage display' screen belongs.
- *
- * @param array $row
- * A field or field_group row.
- *
- * @return string
- * The current region.
- */
- function field_group_display_overview_row_region(array $row) {
- switch ($row['#row_type']) {
- case 'group':
- return ($row['format']['type']['#value'] == 'hidden' ? 'hidden' : 'visible');
- case 'add_new_group':
-
-
- if (empty($row['label']['#value'])) {
- return 'add_new';
- }
- return ($row['format']['type']['#value'] == 'hidden' ? 'hidden' : 'visible');
- }
- }
-
- * Render API callback: Checks if a group machine name is taken.
- *
- * @param string $value
- * The machine name, not prefixed with 'group_'.
- *
- * @return bool
- * Always FALSE.
- *
- * @todo
- * This callback can only check the name globally without context.
- * Field group names, however, can be the same in different view modes and
- * content types. See field_group_field_overview_validate() for current
- * approach to validating the group name.
- */
- function _field_group_group_name_exists($value) {
- return FALSE;
- }
-
- * Validate handler for the overview screens.
- *
- * @param array $form
- * An associative array containing the structure of the form.
- * @param array $form_state
- * The state of the form.
- */
- function field_group_field_overview_validate(array $form, array &$form_state) {
- $form_values = $form_state['values']['fields'];
- $entity_type = $form['#entity_type'];
- $bundle = $form['#bundle'];
- $mode = (isset($form['#view_mode']) ? $form['#view_mode'] : 'form');
-
- $group = $form_values['_add_new_group'];
-
-
- if (array_filter(array($group['label'], $group['group_name']))) {
-
-
- if (!$group['group_name']) {
- form_set_error('fields][_add_new_group][group_name', t('Add new group: you need to provide a group name.'));
- }
-
- else {
- $group_name = $group['group_name'];
-
-
- if (backdrop_substr($group_name, 0, 6) != 'group_') {
- $group_name = 'group_' . $group_name;
- form_set_value($form['fields']['_add_new_group']['group_name'], $group_name, $form_state);
- }
-
-
- if (!preg_match('!^group_[a-z0-9_]+$!', $group_name)) {
- form_set_error('fields][_add_new_group][group_name', t('Add new group: the group name %group_name is invalid. The name must include only lowercase unaccentuated letters, numbers, and underscores.', array('%group_name' => $group_name)));
- }
- if (backdrop_strlen($group_name) > 32) {
- form_set_error('fields][_add_new_group][group_name', t("Add new group: the group name %group_name is too long. The name is limited to 32 characters, including the 'group_' prefix.", array('%group_name' => $group_name)));
- }
-
-
- if (field_group_exists($group_name, $entity_type, $bundle, $mode)) {
- form_set_error('fields][_add_new_group][group_name', t('Add new group: the group name %group_name already exists.', array('%group_name' => $group_name)));
- }
- }
- }
- }
-
- * Submit handler for the overview screens.
- *
- * @param array $form
- * The complete form.
- * @param array $form_state
- * The state of the form.
- */
- function field_group_field_overview_submit(array $form, array &$form_state) {
-
- $form_values = $form_state['values']['fields'];
- $entity_type = $form['#entity_type'];
- $bundle = $form['#bundle'];
- $mode = (isset($form['#view_mode']) ? $form['#view_mode'] : 'form');
-
-
- $children = array_fill_keys($form['#groups'], array());
- foreach ($form_values as $name => $value) {
- if (!empty($value['parent'])) {
-
-
- if ($name == '_add_new_field' && isset($form_state['fields_added']['_add_new_field'])) {
- $name = $form_state['fields_added']['_add_new_field'];
- }
- elseif ($name == '_add_existing_field' && isset($form_state['fields_added']['_add_existing_field'])) {
- $name = $form_state['fields_added']['_add_existing_field'];
- }
- $children[$value['parent']][$name] = $name;
- }
- }
-
-
- if (!empty($form_values['_add_new_group']['group_name'])) {
- $values = $form_values['_add_new_group'];
-
- $field_group_types = field_group_formatter_info();
- $formatter = $field_group_types[($mode == 'form' ? 'form' : 'display')][$values['format']['type']];
-
- $new_group = (object) array(
- 'group_name' => $values['group_name'],
- 'entity_type' => $entity_type,
- 'bundle' => $bundle,
- 'mode' => $mode,
- 'children' => isset($children['_add_new_group']) ? array_keys($children['_add_new_group']) : array(),
- 'parent_name' => $values['parent'],
- 'weight' => $values['weight'],
- 'label' => $values['label'],
- 'format_type' => $values['format']['type'],
- );
- $new_group->format_settings = array('formatter' => isset($formatter['default_formatter']) ? $formatter['default_formatter'] : '');
- if (isset($formatter['instance_settings'])) {
- $new_group->format_settings['instance_settings'] = $formatter['instance_settings'];
- }
-
- $classes = _field_group_get_html_classes($new_group);
- $new_group->format_settings['instance_settings']['classes'] = implode(' ', $classes->optional);
-
- field_group_group_save($new_group, TRUE);
-
-
- $form_state['groups_added']['_add_new_group'] = $new_group->group_name;
- backdrop_set_message(t('New group %label successfully created.', array('%label' => $new_group->label)));
-
-
-
- foreach (array_keys($children) as $parent) {
- if (isset($children[$parent]['_add_new_group'])) {
- unset($children[$parent]['_add_new_group']);
- $children[$parent][$new_group->group_name] = $new_group->group_name;
- }
- }
- }
-
-
- $groups = field_group_info_groups($entity_type, $bundle, $mode, TRUE);
- foreach ($form['#groups'] as $group_name) {
- $group = $groups[$group_name];
- $group->label = $form_state['field_group'][$group_name]->label;
- $group->children = array_keys($children[$group_name]);
- $group->parent_name = $form_values[$group_name]['parent'];
- $group->weight = $form_values[$group_name]['weight'];
-
- if ($mode != 'form') {
-
-
- $old_format_type = $group->format_type;
- $group->format_type = isset($form_values[$group_name]['format']['type']) ? $form_values[$group_name]['format']['type'] : 'visible';
- if (isset($form_state['field_group'][$group_name]->format_settings)) {
- $group->format_settings = $form_state['field_group'][$group_name]->format_settings;
- }
-
-
-
- if ($group->format_type != $old_format_type) {
- $mode = $group->mode == 'form' ? 'form' : 'display';
- $default_formatter_settings = _field_group_get_default_formatter_settings($group->format_type, $mode);
- $group->format_settings += $default_formatter_settings;
- $group->format_settings['instance_settings'] += $default_formatter_settings['instance_settings'];
- }
- }
-
- field_group_group_save($group, FALSE);
- }
-
- cache_clear_all('field_groups', 'cache_field');
- }
-
- * Validate the entered css class from the submitted format settings.
- *
- * @param array $element
- * The validated element.
- * @param array $form_state
- * The state of the form.
- */
- function field_group_validate_css_class(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;
- if ($display_overview) {
- $classes = $form_state['values']['fields'][$form_state['formatter_settings_edit']]['format_settings']['settings']['instance_settings']['classes'];
- }
- else {
- $classes = $form_state['values']['instance_settings']['classes'];
- }
-
- if (!empty($classes) && !preg_match('!^[A-Za-z0-9-_ ]+$!', $classes)) {
- form_error($element, t('The css class must include only letters, numbers, underscores and dashes.'));
- }
- }
-
- * Validate the entered id attribute from the submitted format settings.
- *
- * @param array $element
- * The validated element.
- * @param array $form_state
- * The state of the form.
- */
- function field_group_validate_id(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;
- if ($display_overview) {
- $id = $form_state['values']['fields'][$form_state['formatter_settings_edit']]['format_settings']['settings']['instance_settings']['id'];
- } else {
- $id = $form_state['values']['instance_settings']['id'];
- }
-
- if (!empty($id) && !preg_match('!^[A-Za-z0-9-_]+$!', $id)) {
- form_error($element, t('The id must include only letters, numbers, underscores and dashes.'));
- }
- }
-
- * Implements hook_field_info_max_weight().
- */
- function field_group_field_info_max_weight($entity_type, $bundle, $context) {
- $weights = array();
- foreach (field_group_info_groups($entity_type, $bundle, $context) as $group) {
- $weights[] = $group->weight;
- }
- return $weights ? max($weights) : NULL;
- }
-
- * Menu callback; present a form for removing a group.
- *
- * @param array $form
- * An associative array containing the structure of the form.
- * @param array $form_state
- * An associative array containing the current state of the form, which is
- * passed by reference.
- * @param object $group
- * A group definition.
- * @param string $view_mode
- * The display mode.
- *
- * @return array
- * A form render array.
- */
- function field_group_delete_form(array $form, array &$form_state, $group, $view_mode = 'form') {
- $form['#group'] = $group;
- $admin_path = _field_group_get_admin_path($group->entity_type, $group->bundle, $view_mode);
- $form['#redirect'] = array($admin_path);
- $output = confirm_form($form,
- t('Are you sure you want to delete the group %group?', array('%group' => t($group->label))),
- $admin_path,
- t('This action cannot be undone.'),
- t('Delete'), t('Cancel'),
- 'confirm'
- );
- return $output;
- }
-
- * Remove group from bundle.
- *
- * @param array $form
- * An associative array containing the structure of the form.
- * @param array $form_state
- * An associative array containing the current state of the form, which is
- * passed by reference.
- */
- function field_group_delete_form_submit(array $form, array &$form_state) {
- $group = $form['#group'];
- $bundle = $group->bundle;
- $entity_type = $group->entity_type;
- $group->mode = $form_state['build_info']['args'][1];
- $bundles = (array) field_info_bundles();
- $bundle_label = $bundles[$entity_type][$bundle]['label'];
- field_group_group_delete($group);
- backdrop_set_message(t('The group %group has been deleted from the %type content type.', array(
- '%group' => t($group->label), '%type' => $bundle_label,
- )));
-
- $form_state['redirect'] = $form['#redirect'];
- }
-
- * Configure group on the Manage fields screen.
- *
- * @param array $form
- * An associative array containing the structure of the form.
- * @param array $form_state
- * An associative array containing the current state of the form, which is
- * passed by reference.
- * @param object $group
- * Group object.
- * @param string $view_mode
- * The view mode: form or the actual display mode.
- */
- function field_group_configure_form(array $form, array &$form_state, $group, $view_mode = 'form') {
- $form['#group'] = $group;
- $form_state['#group'] = $group;
- $form['#entity_type'] = $group->entity_type;
- $form['#bundle'] = $group->bundle;
- $form['#view_mode'] = $view_mode;
-
- $form['#attached']['css'][] = backdrop_get_path('module', 'field_group') . '/css/field_group.field_ui.css';
-
- $field_group_types = field_group_formatter_info();
- $format_type_label = $field_group_types['form'][$group->format_type]['label'] ?? '';
- $form['format_type'] = array(
- '#markup' => t('Field group format: !format_type', array(
- '!format_type' => ' <span class="formatter-name">' . $format_type_label . '</span>',
- )),
- '#weight' => -6,
- '#prefix' => '<div class="field-group-formatter">',
- '#suffix' => '</div>',
- );
-
- $form['label'] = array(
- '#type' => 'textfield',
- '#title' => t('Field group label'),
- '#default_value' => $group->label,
- '#weight' => -5,
- '#element_validate' => array('field_group_format_settings_label_validate'),
- );
-
- $addition = (array) module_invoke_all('field_group_format_settings', $group);
-
- backdrop_alter('field_group_format_settings', $addition, $group);
-
- $form += $addition;
-
-
- if (!empty($addition['label']['#description'])) {
- $form['label']['#description'] = $addition['label']['#description'];
- }
-
- $form['actions'] = array(
- '#type' => 'actions',
- );
- $form['actions']['submit'] = array(
- '#type' => 'submit',
- '#value' => 'Update',
- );
-
- $form['actions']['cancel'] = array(
- '#type' => 'link',
- '#title' => 'Cancel',
- '#href' => _field_group_get_admin_path($group->entity_type, $group->bundle, $view_mode),
- );
-
- return $form;
- }
-
- * Submit handler for field_group_configure_form().
- */
- function field_group_configure_form_submit(array $form, array &$form_state) {
- $group = $form['#group'];
- $group->mode = $form['#view_mode'];
- $group->label = $form_state['values']['label'];
- $group->format_settings['instance_settings'] = $form_state['values']['instance_settings'];
- $group->format_settings['formatter'] = isset($form_state['values']['formatter']) ? $form_state['values']['formatter'] : '';
-
- try {
- field_group_group_save($group, FALSE);
- backdrop_set_message(t('Saved configuration for group %label.', array('%label' => $group->group_name)));
- } catch (Exception $e) {
- backdrop_set_message(t('There was a problem saving the configuration for group %label.', array('%label' => $group->group_name)), 'error');
- }
-
- cache_clear_all('field_groups', 'cache_field');
-
- $admin_path = _field_group_get_admin_path($form['#entity_type'], $form['#bundle'], $form['#view_mode']);
- $form_state['redirect'] = $admin_path;
- }
-
- * Form constructor for the format type edit page.
- *
- * @param array $form
- * An associative array containing the structure of the form.
- * @param array $form_state
- * An associative array containing the current state of the form, which is
- * passed by reference.
- * @param object $group
- * Group object.
- *
- * @see field_group_menu()
- * @see field_group_format_type_form_submit()
- * @ingroup forms
- */
- function field_group_format_type_form(array $form, array &$form_state, $group) {
- $form = array(
- '#bundle' => $group->bundle,
- '#entity_type' => $group->entity_type,
- '#group' => $group,
- );
-
- $form['field_group'] = array(
- '#type' => 'fieldset',
- '#title' => t('Change format type'),
- );
-
- $form['field_group']['type'] = array(
- '#type' => 'select',
- '#title' => t('Format type'),
- '#required' => TRUE,
- '#options' => field_group_field_formatter_options('form'),
- '#default_value' => $group->format_type,
- '#description' => t('Select a new format type for %field_group field group on the form. Each format type has its own settings which can be configured afterwards.', array('%field_group' => $group->label)),
- '#attributes' => array('class' => array('field-group-type')),
- );
-
- $form['actions'] = array('#type' => 'actions');
- $form['actions']['submit'] = array(
- '#type' => 'submit', '#value' => t('Continue'),
- );
-
- $form['#validate'] = array();
- $form['#submit'] = array('field_group_format_type_form_submit');
-
- return $form;
- }
-
- * Form submission handler for field_group_format_type_form().
- */
- function field_group_format_type_form_submit($form, &$form_state) {
- $form_values = $form_state['values'];
- $group = $form['#group'];
- $mode = $group->mode;
- $original_format_type = $group->format_type;
- if ($original_format_type == $form_values['type']) {
- backdrop_set_message(t('The chosen format type is the same as the original. No changes made.'));
- return;
- }
-
- $original_formatter_settings = $group->format_settings;
- $group->format_type = $form_values['type'];
- $group->format_settings = _field_group_get_default_formatter_settings($group->format_type, $mode);
-
-
- $group->format_settings['instance_settings'] = _field_group_preserve_shared_instance_settings($group->format_settings['instance_settings'], $original_formatter_settings['instance_settings'], $group);
-
- try {
- field_group_group_save($group, FALSE);
- backdrop_set_message(t('Changed the format type for group %label.', array('%label' => $group->group_name)));
- } catch (Exception $e) {
- backdrop_set_message(t('There was a problem changing the format type for group %label.', array('%label' => $group->group_name)), 'error');
- }
-
- cache_clear_all('field_groups', 'cache_field');
- }
-
- * Helper function to return the admin path for field UI.
- *
- * @param string $entity_type
- * The entity type.
- * @param string $bundle
- * The bundle name.
- * @param string $view_mode
- * The view mode: form or the actual display mode.
- *
- * @return string
- * System path pointing to the field listing for the requested view mode.
- */
- function _field_group_get_admin_path($entity_type, $bundle, $view_mode) {
- $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
- if ($view_mode == 'form') {
- $admin_path .= '/fields';
- } else {
- $admin_path .= '/display/' . $view_mode;
- }
-
- return $admin_path;
- }
-
- * Create vertical tabs.
- *
- * @param array $form
- * An associative array containing the structure of the form, which is passed
- * by reference.
- * @param array $form_state
- * An associative array containing the current state of the form.
- * @param object $params
- * Group parameters.
- */
- function field_group_field_ui_create_vertical_tabs(array &$form, array &$form_state, $params) {
-
- $form_state['field_group_params'] = $params;
-
- $entity_info = entity_get_info($params->entity_type);
- $view_modes = array();
- if ($params->mode != 'default') {
- $view_modes['default'] = t('Default');
- }
- if ($params->mode != 'form') {
- $view_modes['0'] = t('Form');
- }
- foreach ($entity_info['view modes'] as $view_mode => $data) {
- if ($data['custom settings'] && $params->mode != $view_mode) {
- $view_modes[$view_mode] = $data['label'];
- }
- }
-
-
- if (!isset($form['additional_settings'])) {
- $form['additional_settings'] = array(
- '#type' => 'vertical_tabs',
- '#theme_wrappers' => array('vertical_tabs'),
- '#prefix' => '<div>',
- '#suffix' => '</div>',
- '#tree' => TRUE,
- );
- $form['#attached']['js'][] = 'core/misc/form.js';
- $form['#attached']['js'][] = 'core/misc/collapse.js';
- }
-
-
- $form['additional_settings']['field_group'] = array(
- '#type' => 'fieldset',
- '#title' => t('Field groups'),
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- '#parents' => array('additional_settings'),
- );
- $form['additional_settings']['field_group']['fieldgroup_clone'] = array(
- '#title' => t('Clone Field groups'),
- '#description' => t('Select source view mode or form.'),
- '#type' => 'select',
- '#options' => $view_modes,
- '#default_value' => 'none',
- );
- $form['additional_settings']['field_group']['fieldgroup_submit'] = array(
- '#type' => 'submit',
- '#value' => t('Clone'),
- '#validate' => array('field_group_field_ui_clone_field_groups_validate'),
- '#submit' => array('field_group_field_ui_clone_field_groups'),
- );
-
- }
-
- * Validate handler to validate saving existing field groups from one view mode
- * or form to another.
- *
- * @param array $form
- * An associative array containing the structure of the form.
- * @param array $form_state
- * An associative array containing the current state of the form, which is
- * passed by reference.
- */
- function field_group_field_ui_clone_field_groups_validate(array $form, array &$form_state) {
-
- $source_mode = $form_state['#source_mode'] = $form_state['values']['additional_settings']['fieldgroup_clone'] == '0' ? 'form' : $form_state['values']['additional_settings']['fieldgroup_clone'];
- $groups_to_clone = $form_state['#groups_to_clone'] = field_group_read_groups(array(
- 'bundle' => $form_state['field_group_params']->bundle,
- 'entity_type' => $form_state['field_group_params']->entity_type,
- 'mode' => $source_mode,
- ));
-
- $form_state['#source_groups'] = array();
-
- if (!empty($groups_to_clone) && isset($groups_to_clone[$form_state['field_group_params']->entity_type], $groups_to_clone[$form_state['field_group_params']->entity_type][$form_state['field_group_params']->bundle], $groups_to_clone[$form_state['field_group_params']->entity_type][$form_state['field_group_params']->bundle][$source_mode])) {
- $form_state['#source_groups'] = $groups_to_clone[$form_state['field_group_params']->entity_type][$form_state['field_group_params']->bundle][$source_mode];
- }
-
-
- $non_existing_types = array('div');
- backdrop_alter('field_group_mode_types_excluded', $non_existing_types, $form_state['field_group_params']->mode);
-
- if (!empty($form_state['#source_groups'])) {
- $field_instances = field_info_instances($form_state['field_group_params']->entity_type, $form_state['field_group_params']->bundle);
- $visible_fields = array();
- foreach ($field_instances as $field_name => $field_instance) {
- if ($form_state['field_group_params']->mode == 'form' || $field_instance['display'][$form_state['field_group_params']->mode]['type'] != 'hidden') {
- $visible_fields[] = $field_name;
- }
- }
- $form_state['#visible_fields'] = $visible_fields;
- }
-
- foreach ($form_state['#source_groups'] as $key => $group) {
- if (in_array($group->format_type, $non_existing_types)) {
- unset($form_state['#source_groups'][$key]);
- backdrop_set_message(t('Skipping @group because this type does not exist in current mode', array('@group' => $group->label)), 'warning');
- }
- }
-
- if (empty($form_state['#source_groups'])) {
-
- form_set_error('additional_settings][fieldgroup_clone', t('No field groups were found in selected view mode.'));
- return;
- }
-
- }
-
- * Submit handler to save existing field groups from one view mode or form to
- * another.
- *
- * @param array $form
- * An associative array containing the structure of the form.
- * @param array $form_state
- * An associative array containing the current state of the form, which is
- * passed by reference.
- */
- function field_group_field_ui_clone_field_groups(array $form, array &$form_state) {
-
- $fields = array_keys($form_state['values']['fields']);
-
- if (!empty($form_state['#source_groups'])) {
- foreach ($form_state['#source_groups'] as $source_group) {
- if (in_array($source_group->group_name, $fields)) {
- backdrop_set_message(t('Field group @group is not cloned since a group already exists with the same name.', array('@group' => $source_group->group_name)), 'warning');
- continue;
- }
-
-
- $source_group->mode = $form_state['field_group_params']->mode;
- $source_group->children = array_intersect($source_group->children, $form_state['#visible_fields']);
-
- field_group_group_save($source_group, TRUE);
-
- backdrop_set_message(t('Field group @group cloned successfully.', array('@group' => $source_group->group_name)));
-
- }
- }
-
- }