You are here

function library_form_alter in Library 7

Same name and namespace in other branches
  1. 5.2 library.module \library_form_alter()
  2. 6.2 library.module \library_form_alter()
  3. 6 library.module \library_form_alter()

Implements hook_form_alter().

File

./library.module, line 168

Code

function library_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'node_type_form') {
    $form['workflow']['library'] = array(
      '#type' => 'radios',
      '#title' => t('Library Item'),
      '#default_value' => variable_get('library_' . $form['#node_type']->type, LIBRARY_ITEM_NOT_IN_LIBRARY),
      '#options' => array(
        LIBRARY_ITEM_IN_LIBRARY => t('Yes'),
        LIBRARY_ITEM_NOT_IN_LIBRARY => t('No'),
      ),
      '#description' => t('Library items can have a status of available or unavailable.'),
    );
  }
  elseif (!empty($form['#node_edit_form'])) {
    $node = $form['#node'];
    $node_type = $form['type']['#value'];
    if (variable_get('library_' . $node_type, LIBRARY_ITEM_NOT_IN_LIBRARY) == LIBRARY_ITEM_IN_LIBRARY) {
      $form['#cache'] = TRUE;
      $form['#tree'] = TRUE;
      if (!isset($node->library_items)) {
        $node->library_items[0] = array();
      }
      if (isset($form_state['library_item_count']) && !is_array($form_state['library_item_count'])) {
        $item_count = $form_state['library_item_count'];
      }
      else {
        $item_count = max(1, empty($node->library_items) ? 1 : count($node->library_items));
      }

      // Add a wrapper for the items and more button.
      $form['library_items_fieldset'] = array(
        '#type' => 'fieldset',
        '#title' => t('Instances of this item in the library'),
        '#prefix' => '<div id="library-items-fieldset-wrapper">',
        '#suffix' => '</div>',
        '#theme' => 'library_items_field',
      );

      // Add the current choices to the form.
      for ($i = 0; $i < $item_count; $i++) {
        if (isset($node->library_items[$i])) {
          $form['library_items_fieldset']['library_items'][$i] = _library_item_form($i, $node->library_items[$i]);
        }
        else {
          $form['library_items_fieldset']['library_items'][$i] = _library_item_form($i, array());
        }
      }
      $form['library_items_fieldset']['add_library_item'] = array(
        '#type' => 'submit',
        '#value' => t('Add an Item'),
        '#weight' => 1,
        // #submit is needed to deliver the deactivated javascript fallback.
        '#submit' => array(
          'library_add_more_add_one',
        ),
        '#ajax' => array(
          'callback' => 'library_add_more_callback',
          'wrapper' => 'library-items-fieldset-wrapper',
        ),
      );
      $form['#submit'][] = 'library_node_form_submit';
    }
  }
}