You are here

function availability_calendar_field_widget_month_form in Availability Calendars 7.3

Same name and namespace in other branches
  1. 7.5 availability_calendar.widget.inc \availability_calendar_field_widget_month_form()
  2. 7.4 availability_calendar.widget.inc \availability_calendar_field_widget_month_form()

Defines the form elements to edit this field.

Called by our implementation of hook_field_widget_form(). Parameters are as passed to hook_field_widget_form(). Return is what hook_field_widget_form() should return.

Return value

array Form elements to edit this field on an entity edit form.

1 call to availability_calendar_field_widget_month_form()
availability_calendar_field_widget_form in ./availability_calendar.field.inc
Implements hook_field_widget_form(). @link http://api.drupal.org/api/drupal/modules--field--field.api.php/function/...

File

./availability_calendar.widget.inc, line 21

Code

function availability_calendar_field_widget_month_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  static $new_cid_count = 0;
  $item = isset($items[$delta]) ? $items[$delta] : NULL;
  $cid = !empty($item['cid']) ? $item['cid'] : 'new' . ++$new_cid_count;
  $name = !empty($item['name']) ? $item['name'] : '';
  $settings = $instance['widget']['settings'] + $instance['settings'] + $field['settings'];

  // Make sure this file gets loaded on submit
  $form_state['build_info']['files'][] = array(
    'type' => 'inc',
    'module' => 'availability_calendar',
    'name' => 'availability_calendar.widget',
  );
  $description = '';
  if ($settings['allow_disable']) {
    $description .= t("Uncheck the checkbox if you don't want a calendar at all for this entity. ");
  }
  $description .= t('To update the calendar: select a state and click on a begin and end date. Repeat if needed. When finished, click on <em>@button</em>.', array(
    '@button' => t('Save'),
  ));
  $element = array_merge($element, array(
    '#type' => 'container',
    '#element_validate' => array(
      'availability_calendar_field_widget_month_form_validate',
    ),
    '#attached' => availability_calendar_field_widget_month_attach_js_css($cid, $name, $settings),
  ));
  $element['title_description'] = array(
    '#type' => 'item',
    '#title' => $element['#title'],
    '#description' => $description,
  );
  $element['enabled'] = array(
    '#type' => 'checkbox',
    // Don't show the checkbox if the user may not disable the calendar.
    '#access' => $settings['allow_disable'],
    '#title' => t('Enable an availability calendar for this @entity', array(
      '@entity' => $element['#entity_type'],
    )),
    '#default_value' => isset($item['enabled']) ? $item['enabled'] : 1,
    '#attributes' => array(
      'class' => array(
        'availability-enable',
      ),
    ),
  );
  $element['calendar_details_div'] = array(
    '#type' => 'markup',
    '#markup' => '<div class="availability-details">',
  );
  $element['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#description' => t('Useful to distinghuish calendars when you have multiple calendar values for this field.'),
    '#default_value' => isset($item['name']) ? $item['name'] : '',
  );
  $allowed_states = availability_calendar_get_states(array_filter($settings['allowed_states']), 'label');
  $default_state = array_key_exists($settings['default_state'], $allowed_states) ? $settings['default_state'] : NULL;
  $element['availability_states'] = array(
    '#type' => 'radios',
    '#title' => t('States'),
    '#default_value' => $default_state,
    '#options' => $allowed_states,
    '#attributes' => array(
      'class' => array(
        'availability-states',
      ),
    ),
  );
  $element['availability_calendar'] = array(
    '#type' => 'markup',
    '#theme' => $instance['widget']['type'],
    '#markup' => '',
    '#cid' => $cid,
    '#name' => $name,
    '#mode' => 'all',
    '#settings' => $settings,
  );
  $element['calendar_details_enddiv'] = array(
    '#type' => 'markup',
    '#markup' => '</div>',
  );
  $element['availability_changes'] = array(
    '#type' => 'hidden',
    '#title' => t('Changes in availability'),
    '#default_value' => '',
    '#attributes' => array(
      'class' => array(
        'availability-changes',
      ),
    ),
  );

  // Add element cid. It does not have to be sent to the client, but is used on
  // submit. Store 0 for new calendars.
  $element['cid'] = array(
    '#type' => 'hidden',
    '#access' => FALSE,
    '#default_value' => (int) $cid,
  );

  // Add the unique cid, used to match changes, fields, and elements in the
  // processing phase.
  $element['cid_unique'] = array(
    '#type' => 'hidden',
    '#access' => FALSE,
    '#default_value' => $cid,
  );
  return $element;
}