1 field_group.field_ui.inc field_group_field_ui_overview_form_alter(array &$form, array &$form_state, $display_overview = FALSE)

Alter the fields overview and display overview screen.

Parameters

array $form: An associative array containing the structure of the form, which is passed by reference.

array $form_state: An associative array containing the current state of the form, which is passed by reference.

bool $display_overview: Set to TRUE if altering the display overview.

File

core/modules/field_group/field_group.field_ui.inc, line 62
Field_group.field_ui.inc is a file that contains most functions needed on the Fields UI Manage forms (display and fields).

Code

function field_group_field_ui_overview_form_alter(array &$form, array &$form_state, $display_overview = FALSE) {

  // Only start altering the form if we need to.
  if ($display_overview && empty($form['#fields']) && empty($form['#extra'])) {
    return;
  }

  $params = field_group_field_ui_form_params($form, $display_overview);

  // Add some things to be able to preserve synced usage of field_ui.
  if (!$display_overview) {
    // Add AJAX wrapper.
    $form['fields']['#prefix'] = '<div id="field-display-overview-wrapper">';
    $form['fields']['#suffix'] = '</div>';
  }
  $form['#groups'] = array_keys($params->groups);

  $table = &$form['fields'];

  // Add a region for 'add_new' rows, but only when fields are
  // available and thus regions.
  if (isset($table['#regions'])) {
    $table['#regions'] += array(
      'add_new' => array('title' => '&nbsp;'),
    );
  }

  // Extend available parenting options.
  foreach ($params->groups as $name => $group) {
    $table['#parent_options'][$name] = $group->label;
  }
  $table['#parent_options']['_add_new_group'] = t('Add new group');

  // Update existing rows accordingly to the parents.
  foreach (element_children($table) as $name) {
    $table[$name]['parent_wrapper']['parent']['#options'] = $table['#parent_options'];
    // Inherit the value of the parent when default value is empty.
    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);

  // Create the group rows and check actions.
  foreach (array_keys($params->groups) as $name) {

    // Play around with form_state so we only need to hold things
    // between requests, until the save button was hit.
    if (isset($form_state['field_group'][$name])) {
      $group = & $form_state['field_group'][$name];
    }
    else {
      $group = & $params->groups[$name];
    }

    // Check the currently selected formatter, and merge persisted values for
    // formatter settings for the group.
    // This needs to be done first, so all fields are updated before creating
    // form elements.
    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);
    }

    // Save the group when the configuration is submitted.
    if (!empty($form_state['values'][$name . '_formatter_settings_update'])) {
      field_group_formatter_settings_update($group, $form_state['values']['fields'][$name]);
    }
    // After all updates are finished, let the form_state know.
    $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);
    // A group cannot be selected as its own parent.
    $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();

      // Display a link to the format type configure form on the Manage fields
      // page.
      $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')),
          ),
        ),
      );

      // After saving, the settings are updated here as well. First we create
      // the element for the table cell.
      $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'),
        ),
      );

      // Add the operations.
      $table[$name]['operations'] = array(
        '#type' => 'dropbutton',
        '#links' => $operations,
      );
    }
    else {
      // Display the format type form inline.
      $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>',
          ),
          // Create a settings form where hooks can pick in.
          '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',
              // Do not check errors for the 'Cancel' button.
              '#limit_validation_errors' => array(),
            ),
          ),
        );
        $table[$name]['#attributes']['class'][] = 'field-formatter-settings-editing';
        $table[$name]['format']['type']['#attributes']['class'] = array('element-invisible');
      }
      else {
        // After saving, the settings are updated here as well. First we create
        // the element for the table cell.
        $table[$name]['settings_summary'] = array('#markup' => '');
        if (!empty($group->format_settings)) {
          $table[$name]['settings_summary'] = field_group_format_settings_summary($name, $group);
        }
        // Add the configure button.
        $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',
          // Do not check errors for the 'Configure' button.
          '#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;
      }
    }
  }

  // Additional row: add new group.
  $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">&nbsp;</div>',
      ),
      '#cell_attributes' => array('colspan' => 3),
    ),
  );

  if (!$display_overview) {
    // See field_ui.admin.inc for more details on refresh rows.
    $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',
        // The button stays hidden, so we hide the AJAX spinner too. Ad-hoc
        // spinners will be added manually by the client-side script.
        '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';

  // Create the settings for field group as vertical tabs.
  field_group_field_ui_create_vertical_tabs($form, $form_state, $params);

  // Show a warning if the user has not set up required containers.
  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);
        }
      }
    }
  }
}