You are here

function library_form_alter in Library 6

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

Implementation of hook_form_alter()

File

./library.module, line 156

Code

function library_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'node_type_form' && isset($form['identity']['type']) && $form['#node_type']->type != 'patron') {
    $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 will appear in library views.'),
    );
  }
  elseif (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] . '_node_form' == $form_id) {
    $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;
      if (isset($form_state['item_count'])) {
        $item_count = $form_state['item_count'];
      }
      else {
        $item_count = max(1, empty($node->items) ? 1 : count($node->items));
      }
      if (variable_get('library_unique_titles', 0) == LIBRARY_UNIQUE_TITLES) {
        $type = node_get_types('type', $node_type);
        if ($type->has_title) {
          unset($form['title']);
        }
        $form['title_wrapper'] = array(
          '#tree' => FALSE,
          '#prefix' => '<div class="clear-block" id="title-wrapper">',
          '#suffix' => '</div>',
          '#weight' => -5,
        );
        $form['title_wrapper']['title'] = array(
          '#type' => 'textfield',
          '#title' => check_plain($type->title_label),
          '#required' => TRUE,
          '#default_value' => $node->title,
          '#maxlength' => 255,
          '#ahah' => array(
            'path' => 'library/title_js',
            'wrapper' => 'title-wrapper',
          ),
          '#weight' => -5,
        );
      }

      // Add a wrapper for the items and more button.
      $form['item_wrapper'] = array(
        '#tree' => FALSE,
        '#weight' => -4,
        '#prefix' => '<div class="clear-block" id="library-item-wrapper">',
        '#suffix' => '</div>',
      );

      // Container for just the library items.
      $form['item_wrapper']['items'] = array(
        '#prefix' => '<div id="library-items">',
        '#suffix' => '</div>',
        '#theme' => 'library_items_field',
      );

      // Add the current choices to the form.
      for ($delta = 0; $delta < $item_count; $delta++) {
        $form['item_wrapper']['items'][$delta] = _library_item_form($delta, $node->items[$delta]);
      }

      // We name our button 'library_more' to avoid conflicts with other modules using
      // AHAH-enabled buttons with the id 'more'.
      $form['item_wrapper']['library_more'] = array(
        '#type' => 'submit',
        '#value' => t('Add an Item'),
        '#weight' => 1,
        '#submit' => array(
          'library_more_items_submit',
        ),
        // If no javascript action.
        '#ahah' => array(
          'path' => 'library/js',
          'wrapper' => 'library-items',
          'method' => 'replace',
          'effect' => 'fade',
        ),
      );
      $form['#submit'][] = 'library_node_form_submit';
    }
  }
  elseif ($form_id == 'search_form' && $form['module']['#value'] == 'library' && user_access('use advanced search')) {

    // Keyword boxes:
    $form['advanced'] = array(
      '#type' => 'fieldset',
      '#title' => t('More Search Options'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#attributes' => array(
        'class' => 'search-advanced',
      ),
    );
    $form['advanced']['keywords'] = array(
      '#prefix' => '<div class="criterion">',
      '#suffix' => '</div>',
    );
    $form['advanced']['keywords']['or'] = array(
      '#type' => 'textfield',
      '#title' => t('Containing any of the words'),
      '#size' => 30,
      '#maxlength' => 255,
    );
    $form['advanced']['keywords']['phrase'] = array(
      '#type' => 'textfield',
      '#title' => t('Containing the phrase'),
      '#size' => 30,
      '#maxlength' => 255,
    );
    $form['advanced']['keywords']['negative'] = array(
      '#type' => 'textfield',
      '#title' => t('Containing none of the words'),
      '#size' => 30,
      '#maxlength' => 255,
    );

    // Taxonomy box:
    if ($taxonomy = module_invoke('taxonomy', 'form_all', 1)) {
      $form['advanced']['category'] = array(
        '#type' => 'select',
        '#title' => t('Only in the category(s)'),
        '#prefix' => '<div class="criterion">',
        '#size' => 10,
        '#suffix' => '</div>',
        '#options' => $taxonomy,
        '#multiple' => TRUE,
      );
    }

    // Node types:
    $types = array_map('check_plain', library_get_item_types('names'));
    $form['advanced']['type'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Only of the type(s)'),
      '#prefix' => '<div class="criterion">',
      '#suffix' => '</div>',
      '#options' => $types,
    );
    $form['advanced']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Advanced search'),
      '#prefix' => '<div class="action">',
      '#suffix' => '</div>',
    );
    $form['#validate'][] = 'node_search_validate';
  }
  elseif (module_exists('content') && $form_id == 'content_field_edit_form') {
    $node_type = $form['type_name']['#value'];
    if (variable_get('library_' . $node_type, LIBRARY_ITEM_NOT_IN_LIBRARY) == LIBRARY_ITEM_IN_LIBRARY) {
      $newform = array();
      $newform['library_field_settings'] = array(
        '#type' => 'fieldset',
        '#title' => t('Library Settings'),
      );
      $newform['library_field_settings']['library_display_field_' . $form['field_name']['#value']] = array(
        '#type' => 'checkbox',
        '#title' => t('Display this field in the library'),
        '#default_value' => variable_get('library_display_field_' . $form['field_name']['#value'], 0),
        '#return_value' => 1,
      );
      $pos = array_search('widget', array_keys($form));
      $form = array_merge(array_slice($form, 0, $pos), $newform, array_slice($form, $pos));
      $form['#submit'][] = 'library_field_submit';
    }
  }
  elseif ($form_id == '') {
  }
  elseif ($form_id == 'search_block_form' && variable_get('library_search_block', 0) == 1) {
    $form['#submit'][] = 'library_search_box_form_submit';
  }
}