You are here

function ac_update_create_field_instances in Availability Calendars 7.2

1 call to ac_update_create_field_instances()
ac_update_copy_data in ac_update/ac_update.module

File

ac_update/ac_update.module, line 635
Availability Calendars update: admin update code

Code

function ac_update_create_field_instances(&$result) {
  $old_states = availability_calendars_get_states();
  $current_states = availability_calendar_get_states();

  // Set up the allowed states. An empty array means that all states are allowed
  // which is the case if the count of old and new states are equal.
  $allowed_states = array();
  if (count($old_states) !== count($current_states)) {
    foreach ($current_states as $sid => $state) {
      if (array_key_exists($state['css_class'], $old_states)) {
        $allowed_states[$sid] = $state;
      }
    }
  }

  // Get old settings and copy as many settings as possible to the new fields.
  $settings = availability_calendars_get_settings();

  // Convert defaultstatus to sid.
  $default_state = 1;
  foreach ($current_states as $current_state) {
    if ($current_state['css_class'] === $settings->defaultstatus) {
      $default_state = $current_state['sid'];
      break;
    }
  }
  $c = 0;
  $supported_content_types = variable_get('availability_calendars_settings_content_types', array());
  foreach ($supported_content_types as $supported_content_type) {

    // Create field. We create 1 field per supported content type as this allows
    // the user to define different settings per bundle.
    // If a field name already exists, we skip it.
    do {
      $c++;
      $field_name = 'field_availability' . ($c === 1 ? '' : $c);
    } while (field_read_field($field_name) !== FALSE);
    $field = field_create_field(array(
      'field_name' => $field_name,
      'type' => 'availability_calendar',
      'settings' => array(
        'allocation_type' => $settings->splitday ? AC_ALLOCATION_TYPE_OVERNIGHT : AC_ALLOCATION_TYPE_FULLDAY,
        'allowed_states' => $allowed_states,
        'default_state' => $default_state,
      ),
    ));
    $result['messages'][] = array(
      'message' => "Created Availability Calendar field <em>{$field_name}</em>.",
      'type' => 'status',
    );

    // Create field instance based on created field.
    $instance = array(
      'label' => t('Availability Calendar'),
      'field_name' => $field_name,
      'entity_type' => 'node',
      'bundle' => $supported_content_type,
      'settings' => array(
        'allow_disable' => 1,
        'add_name' => 1,
      ),
      'widget' => array(
        'type' => 'availability_calendar',
        'settings' => array(
          'show_number_of_months' => $settings->editormonthcount,
          'first_day_of_week' => ($settings->startofweek + 4) % 7 + 1,
          // old: 1 = saturday, new: 1 = monday (ISO8601)
          'show_week_number' => 0,
          'show_only_first_letter' => $settings->firstletter,
          'show_split_day' => $settings->splitday,
        ),
      ),
      'display' => array(
        'default' => array(
          'type' => 'availability_calendar',
          'settings' => array(
            'show_number_of_months' => $settings->monthcount,
            'first_day_of_week' => ($settings->startofweek + 4) % 7 + 1,
            // old: 1 = saturday, new: 1 = monday (ISO8601)
            'show_week_number' => 0,
            'show_only_first_letter' => $settings->firstletter,
            'show_split_day' => $settings->splitday,
            'selectable' => 0,
          ),
        ),
      ),
    );
    if ($settings->showteaser) {
      $instance['display']['teaser'] = $instance['display']['default'];
      $instance['display']['teaser']['settings']['show_number_of_months'] = 2;
    }
    field_create_instance($instance);
    $result['messages'][] = array(
      'message' => "Created a field instance (of field <em>{$field_name}</em>) for content type <em>{$supported_content_type}</em>.",
      'type' => 'status',
    );
    $result['field instances'][$supported_content_type] = $field_name;
  }
}