You are here

function bat_unit_edit_form in Booking and Availability Management Tools for Drupal 7

Form callback: create or edit a unit.

Parameters

BatUnit $unit: The BatUnit object to edit or for a create form an empty unit object with only a unit type defined.

1 call to bat_unit_edit_form()
bat_unit_form in modules/bat_unit/bat_unit.admin.inc
Form callback: edit a Unit.
2 string references to 'bat_unit_edit_form'
bat_unit_create_form_wrapper in modules/bat_unit/bat_unit.admin.inc
Form callback wrapper: create a Unit.
bat_unit_form_wrapper in modules/bat_unit/bat_unit.admin.inc
Form callback wrapper: edit a Unit.

File

modules/bat_unit/bat_unit.admin.inc, line 211
BatUnit editing UI.

Code

function bat_unit_edit_form($form, &$form_state, BatUnit $unit) {
  $form['#attributes']['class'][] = 'bat-management-form bat-unit-edit-form';
  $form['#attached']['css'] = array(
    drupal_get_path('module', 'bat_unit') . '/css/bat_unit.css',
  );
  $form['type'] = array(
    '#type' => 'value',
    '#value' => $unit->type,
  );

  // Add the default field elements.
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Unit name'),
    '#default_value' => isset($unit->name) ? $unit->name : '',
    '#maxlength' => 255,
    '#required' => TRUE,
    '#weight' => -99,
  );

  // Add the field related form elements.
  $form_state['bat_unit'] = $unit;
  field_attach_form('bat_unit', $unit, $form, $form_state, isset($unit->language) ? $unit->language : NULL);
  $form['additional_settings'] = array(
    '#type' => 'vertical_tabs',
    '#weight' => 99,
  );

  // Unit author information for administrators.
  $form['author'] = array(
    '#type' => 'fieldset',
    '#access' => user_access('bypass bat_unit entities access'),
    '#title' => t('Authoring information'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#group' => 'additional_settings',
    '#attributes' => array(
      'class' => array(
        'unit-form-author',
      ),
    ),
    '#weight' => 90,
  );
  $form['author']['author_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Authored by'),
    '#maxlength' => 60,
    '#autocomplete_path' => 'user/autocomplete',
    '#default_value' => !empty($unit->author_name) ? $unit->author_name : '',
    '#weight' => -1,
    '#description' => t('Leave blank for %anonymous.', array(
      '%anonymous' => variable_get('anonymous', t('Anonymous')),
    )),
  );
  $form['author']['date'] = array(
    '#type' => 'textfield',
    '#title' => t('Authored on'),
    '#maxlength' => 25,
    '#description' => t('Format: %time. The date format is YYYY-MM-DD and %timezone is the time zone offset from UTC. Leave blank to use the time of form submission.', array(
      '%time' => !empty($unit->date) ? date_format(date_create($unit->date), 'Y-m-d H:i:s O') : format_date($unit->created, 'custom', 'Y-m-d H:i:s O'),
      '%timezone' => !empty($unit->date) ? date_format(date_create($unit->date), 'O') : format_date($unit->created, 'custom', 'O'),
    )),
    '#default_value' => !empty($unit->date) ? $unit->date : '',
  );

  // Unit publishing options for administrators.
  $form['options'] = array(
    '#type' => 'fieldset',
    '#access' => user_access('bypass bat_unit entities access'),
    '#title' => t('Publishing options'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#group' => 'additional_settings',
    '#attributes' => array(
      'class' => array(
        'unit-form-published',
      ),
    ),
    '#weight' => 95,
  );
  $form['options']['status'] = array(
    '#type' => 'checkbox',
    '#title' => t('Published'),
    '#default_value' => $unit->status,
  );
  $form['actions'] = array(
    '#type' => 'actions',
    '#tree' => FALSE,
  );

  // We add the form's #submit array to this button along with the actual submit
  // handler to preserve any submit handlers added by a form callback_wrapper.
  $submit = array();
  if (!empty($form['#submit'])) {
    $submit += $form['#submit'];
  }
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Unit'),
    '#submit' => $submit + array(
      'bat_unit_edit_form_submit',
    ),
  );
  if (!empty($unit->name) && bat_unit_access('delete', $unit)) {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete Unit'),
      '#suffix' => l(t('Cancel'), 'admin/bat/config/units'),
      '#submit' => $submit + array(
        'bat_unit_form_submit_delete',
      ),
      '#weight' => 45,
    );
  }
  if (isset($form['actions']['delete']) && isset($form_state['input']['js']) && $form_state['input']['js']) {
    unset($form['actions']['delete']);
  }

  // We append the validate handler to #validate in case a form callback_wrapper
  // is used to add validate handlers earlier.
  $form['#validate'][] = 'bat_unit_edit_form_validate';
  return $form;
}