function rooms_unit_edit_form in Rooms - Drupal Booking for Hotels, B&Bs and Vacation Rentals 7
Form callback: create or edit a unit.
Parameters
RoomsUnit $unit: The RoomUnit object to edit or for a create form an empty unit object with only a unit type defined.
2 string references to 'rooms_unit_edit_form'
- rooms_unit_create_form_wrapper in modules/
rooms_unit/ rooms_unit.admin.inc - Form callback wrapper: create a Unit.
- rooms_unit_form_wrapper in modules/
rooms_unit/ rooms_unit.admin.inc - Form callback wrapper: edit a Bookable Unit.
File
- modules/
rooms_unit/ rooms_unit.admin.inc, line 195 - Rooms editing UI.
Code
function rooms_unit_edit_form($form, &$form_state, $unit) {
$form['#attributes']['class'][] = 'rooms-management-form rooms-unit-edit-form';
$form['#attached']['css'] = array(
drupal_get_path('module', 'rooms_unit') . '/css/rooms_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,
);
$form['base_price'] = array(
'#type' => 'textfield',
'#title' => t('Base price'),
'#default_value' => isset($unit->base_price) ? $unit->base_price : '',
'#size' => '5',
'#field_suffix' => t('Per unit per night'),
'#maxlength' => 10,
'#required' => TRUE,
'#weight' => -98,
);
$form['guest_capacity'] = array(
'#type' => 'fieldset',
'#title' => t('Sleeping capacity'),
'#tree' => FALSE,
'#weight' => -96,
'#attributes' => array(
'class' => array(
'rooms-unit-guest-capacity',
),
),
);
$form['guest_capacity']['min_sleeps'] = array(
'#type' => 'textfield',
'#size' => 5,
'#default_value' => isset($unit->min_sleeps) ? $unit->min_sleeps : 1,
'#field_suffix' => t('Person minimum'),
);
$form['guest_capacity']['max_sleeps'] = array(
'#type' => 'textfield',
'#size' => 5,
'#default_value' => isset($unit->max_sleeps) ? $unit->max_sleeps : 1,
'#field_suffix' => t('Person maximum'),
'#description' => t('The total number of guests (adults and children) allowed in this unit.'),
);
$form['child_capacity'] = array(
'#type' => 'fieldset',
'#title' => t('Child capacity'),
'#tree' => FALSE,
'#weight' => -95,
'#attributes' => array(
'class' => array(
'rooms-unit-child-capacity',
),
),
);
$form['child_capacity']['min_children'] = array(
'#type' => 'textfield',
'#size' => 5,
'#default_value' => isset($unit->min_children) ? $unit->min_children : 0,
'#field_suffix' => t('Child minimum'),
);
$form['child_capacity']['max_children'] = array(
'#type' => 'textfield',
'#size' => 5,
'#default_value' => isset($unit->max_children) ? $unit->max_children : 0,
'#field_suffix' => t('Child maximum'),
'#description' => t('The number of children allowed in this unit.'),
);
$form['data'] = array(
'#tree' => TRUE,
'#weight' => -94,
);
$form['data']['bed_arrangement'] = array(
'#type' => 'fieldset',
'#title' => t('Bed arrangement'),
'#attributes' => array(
'class' => array(
'rooms-unit-bed-arrangement',
),
),
);
$form['data']['bed_arrangement']['singles'] = array(
'#type' => 'textfield',
'#size' => 5,
'#default_value' => isset($unit->data['bed_arrangement']['singles']) ? $unit->data['bed_arrangement']['singles'] : '',
'#field_suffix' => t('Single beds'),
);
$form['data']['bed_arrangement']['doubles'] = array(
'#type' => 'textfield',
'#size' => 5,
'#default_value' => isset($unit->data['bed_arrangement']['doubles']) ? $unit->data['bed_arrangement']['doubles'] : '',
'#field_suffix' => t('Double beds'),
);
// Add the field related form elements.
$form_state['rooms_unit'] = $unit;
field_attach_form('rooms_unit', $unit, $form, $form_state, isset($unit->language) ? $unit->language : NULL);
$form['additional_settings'] = array(
'#type' => 'vertical_tabs',
'#weight' => 99,
);
// Unit availability information for administrators.
$form['availability'] = array(
'#type' => 'fieldset',
'#group' => 'additional_settings',
'#title' => t('Status'),
'#tree' => FALSE,
'#weight' => 80,
'#attributes' => array(
'class' => array(
'unit-form-availability ',
),
),
'#attached' => array(
'js' => array(
drupal_get_path('module', 'rooms_unit') . '/js/rooms_unit.js',
),
),
);
$form['availability']['bookable'] = array(
'#type' => 'checkbox',
'#title' => t('Bookable'),
'#description' => t('Uncheck this to temporarily prevent the unit from being booked or appearing in any searches without affecting the scheduled availability on the calendar.'),
'#default_value' => isset($unit->bookable) ? $unit->bookable : 1,
);
$state_options = rooms_unit_state_options();
$form['availability']['default_state'] = array(
'#type' => 'select',
'#title' => t('Default availability'),
'#options' => $state_options,
'#default_value' => isset($unit->default_state) ? $unit->default_state : 1,
'#description' => t('Choose what state to leave the unit in by default.'),
);
// Unit author information for administrators.
$form['author'] = array(
'#type' => 'fieldset',
'#access' => user_access('bypass rooms_unit entities access'),
'#title' => t('Authoring information'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
'#attributes' => array(
'class' => array(
'unit-form-author',
),
),
'#attached' => array(
'js' => array(
drupal_get_path('module', 'rooms_unit') . '/js/rooms_unit.js',
array(
'type' => 'setting',
'data' => array(
'anonymous' => variable_get('anonymous', t('Anonymous')),
),
),
),
),
'#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 rooms_unit entities access'),
'#title' => t('Publishing options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
'#attributes' => array(
'class' => array(
'unit-form-published',
),
),
'#attached' => array(
'js' => array(
drupal_get_path('module', 'rooms_unit') . '/js/rooms_unit.js',
),
),
'#weight' => 95,
);
$form['options']['status'] = array(
'#type' => 'checkbox',
'#title' => t('Published'),
'#default_value' => $unit->status,
);
// If we are creating a new booking unit, bulk creation info.
if ($unit->unit_id == '') {
$form['multi'] = array(
'#type' => 'fieldset',
'#group' => 'additional_settings',
'#title' => t('Create multiple units'),
'#description' => t('Save time by creating multiple bookable units at once all containing the settings above. You can still personalise each one later on.'),
'#weight' => 85,
'#attributes' => array(
'class' => array(
'unit-form-multiple',
),
),
'#attached' => array(
'js' => array(
drupal_get_path('module', 'rooms_unit') . '/js/rooms_unit.js',
),
),
);
$form['multi']['multiple'] = array(
'#type' => 'textfield',
'#size' => 5,
'#default_value' => 1,
'#title' => t('How many units should we create'),
);
}
$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(
'rooms_unit_edit_form_submit',
),
);
if (!empty($unit->name) && rooms_unit_access('delete', $unit)) {
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete Unit'),
'#suffix' => l(t('Cancel'), 'admin/rooms/units'),
'#submit' => $submit + array(
'rooms_unit_form_submit_delete',
),
'#weight' => 45,
);
}
// We append the validate handler to #validate in case a form callback_wrapper
// is used to add validate handlers earlier.
$form['#validate'][] = 'rooms_unit_edit_form_validate';
return $form;
}