You are here

function library_form_alter in Library 5.2

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

Implementation of hook_form_alter()

File

./library.module, line 124

Code

function library_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
    $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.'),
    );
  }
  else {
    if (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';
      }
    }
    else {
      if ($form_id == 'search_form' && $form['module']['#value'] == 'library' && user_access('use advanced search')) {

        // Keyword boxes:
        $form['advanced'] = array(
          '#type' => 'fieldset',
          '#title' => t('Library advanced search'),
          '#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']['library_status'] = array(
              '#type' => 'checkboxes',
              '#title' => t('Only with the status of'),
              '#prefix' => '<div class="criterion">',
              '#suffix' => '</div>',
              '#options' => array(variable_get('library_available_text', 'AVAILABLE'), variable_get('library_unavailable_noduedates_text', 'UNAVAILABLE'), variable_get('library_reference_only_text', 'REFERENCE ONLY')),
            );
        */
        $form['advanced']['submit'] = array(
          '#type' => 'submit',
          '#value' => t('Advanced search'),
          '#prefix' => '<div class="action">',
          '#suffix' => '</div>',
        );
        $form['#validate'][] = 'node_search_validate';
      }
      else {
        if (module_exists('content') && $form_id == '_content_admin_field') {
          $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';
          }
        }
      }
    }
  }
}