You are here

function similarterms_form_alter in Similar By Terms 6

Same name and namespace in other branches
  1. 7 similarterms.module \similarterms_form_alter()

Implementation of hook_form_alter().

File

./similarterms.module, line 337
Similar By Terms module displays a block with similar content based on taxonomy terms.

Code

function similarterms_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['type']) && $form['type']['#value'] . '_node_form' == $form_id) {
    if (!variable_get('similarterms_override_options', 0)) {
      return;
    }

    // no need to alter form if block module is off
    if (!module_exists('block')) {
      return;
    }
    $blocks = similarterms_get_active_block_vocabularies();

    // no need to alter form if no similarterms blocks are active
    if (sizeof($blocks) == 0) {
      return;
    }
    $overrides = array();
    if (is_numeric($form['nid']['#value'])) {
      $overrides = similarterms_get_overrides($form['nid']['#value']);
    }
    $form['similarterms'] = array(
      '#type' => 'fieldset',
      '#title' => t('Similar Terms'),
      '#description' => t('Override the links generated by similar terms module.'),
      '#collapsible' => 1,
      '#collapsed' => sizeof($overrides) ? 0 : 1,
    );
    $node_type = $form['type']['#value'];
    $no_similarterms = 1;
    foreach (similarterms_taxonomy_get_vocabularies() as $v) {
      if (!$blocks[$v->vid]) {
        continue;
      }
      $types = variable_get('simterms_sametype_' . $v->vid, array(
        '0' => '0',
      ));
      if (!$types[$node_type] && !isset($types[0])) {
        continue;
      }
      $no_similarterms = 0;
      $form['similarterms']['similarterms_vid_' . $v->vid] = array(
        '#type' => 'fieldset',
        '#title' => $v->name,
        '#collapsible' => 1,
        '#collapsed' => sizeof($overrides[$v->vid]) ? 0 : 1,
        '#tree' => 1,
      );
      $form['similarterms']['similarterms_vid_' . $v->vid]['similarterms_paths'] = array(
        '#type' => 'fieldset',
        '#title' => t('Paths'),
        '#tree' => 1,
      );
      $count = variable_get('simterms_count_' . $v->vid, 5);
      for ($i = 0; $i < $count; $i++) {
        $default_value = '';
        if ($overrides[$v->vid][$i]) {
          $default_value = drupal_lookup_path('alias', 'node/' . $overrides[$v->vid][$i]);
          if (!$default_value) {
            $default_value = 'node/' . $overrides[$v->vid][$i];
          }
        }
        $form['similarterms']['similarterms_vid_' . $v->vid]['similarterms_paths'][$i] = array(
          '#type' => 'textfield',
          '#title' => t('Path %d', array(
            '%d' => $i + 1,
          )),
          '#default_value' => $default_value ? $default_value : '',
        );
      }
      $form['similarterms']['similarterms_vid_' . $v->vid]['similarterms_override_delete'] = array(
        '#type' => 'checkbox',
        '#title' => t('Delete Similar Terms Overrides?'),
        '#description' => t('If this option is checked all paths will be deleted for this vocabulary.'),
        '#default_value' => 0,
      );
    }
    if ($no_similarterms) {
      unset($form['similarterms']);
    }
  }
}