You are here

function group_form in Group 7

Generates the group editing form.

Pathauto support is also added here, albeit with a twist.

See also

group_install()

File

forms/group.inc, line 14
Group editing UI.

Code

function group_form($form, &$form_state, Group $group, $op = 'edit') {
  $group_type = group_type_load($group->type);
  if ($op == 'add') {
    drupal_set_title(t('Create @name', array(
      '@name' => $group_type
        ->label(),
    )), PASS_THROUGH);
  }
  elseif ($op == 'edit') {
    drupal_set_title(t('Edit @title', array(
      '@title' => $group
        ->label(),
    )), PASS_THROUGH);
  }

  // A bundle property is needed for entity_form_field_validate().
  $form['type'] = array(
    '#type' => 'value',
    '#value' => $group->type,
  );
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => $group
      ->label(),
    '#description' => t('The name of the group.'),
    '#required' => TRUE,
    '#maxlength' => 255,
    '#size' => 30,
  );

  // Insert vertical tabs to add admin options to.
  $form['additional_settings'] = array(
    '#type' => 'vertical_tabs',
    '#weight' => 99,
  );

  // Add Path module support.
  if (module_exists('path')) {
    group_attach_path_form($group, $form, $form_state);
  }

  // Load fields based on group type, this will also enable Pathauto support by
  // invoking the hook implementation pathauto_field_attach_form().
  field_attach_form('group', $group, $form, $form_state, entity_language('group', $group));

  // Tweak the Pathauto additions so the user is notified the checkbox won't do
  // anything unless they enable Entity tokens. See group_install() for info.
  if (isset($form['path']['pathauto']) && !module_exists('entity_token')) {
    $group->path['pathauto'] = FALSE;

    // Only disable the whole '!enabled' state if there is one entry in it.
    if (count($form['path']['alias']['#states']['!enabled']) == 1) {
      unset($form['path']['alias']['#states']['!enabled']);
    }
    else {
      unset($form['path']['alias']['#states']['!enabled']['input[name="path[pathauto]"]']);
    }

    // Disable the Pathauto checkbox and add a message.
    $message = t('You need to enable the Entity tokens module for Pathauto to work with Group.');
    $form['path']['pathauto']['#default_value'] = 0;
    $form['path']['pathauto']['#disabled'] = TRUE;
    $form['path']['pathauto']['#description'] .= '<br /><strong>' . t('Please note:') . '</strong> ' . $message;
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save @group_type', array(
      '@group_type' => $group_type
        ->label(),
    )),
    '#weight' => 40,
  );
  if ($op == 'edit' && group_access('delete group', $group)) {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete @group_type', array(
        '@group_type' => $group_type
          ->label(),
      )),
      '#weight' => 45,
      '#limit_validation_errors' => array(),
      '#submit' => array(
        'group_form_submit_delete',
      ),
    );
  }
  return $form;
}