You are here

function _nodesymlinks_node_form_alter in NodeSymlinks 7

Implements hook_form_alter().

Adds nodesymlinks item fields to the node form.

1 call to _nodesymlinks_node_form_alter()
nodesymlinks_form_node_form_alter in ./nodesymlinks.module
Implements hook_form_FORM_ID_alter().

File

./nodesymlinks.inc, line 332
Main NodeSymlinks callbacks

Code

function _nodesymlinks_node_form_alter(&$form, &$form_state) {

  // The submit handlers require that this form is cached,
  // regardless of whether Ajax is used.
  $form_state['cache'] = TRUE;
  $node = $form['#node'];

  // Add JavaScript that will disable the path textfield
  // when the automatic alias checkbox is checked.
  $form['#attached']['js'][] = drupal_get_path('module', 'nodesymlinks') . '/nodesymlinks.js';
  if (isset($form_state['values']['menu']['nodesymlinks']['items'])) {
    $nodesymlinks = (array) $form_state['values']['menu']['nodesymlinks']['items'];

    // @TODO: switch to this for PHP 5.3+
    // $nodesymlinks = array_values(array_filter($nodesymlinks, function($x) {return $x['delete'] != 1; }));
    $array_filter_callback = create_function('$x', 'return $x["delete"] != 1;');
    $nodesymlinks = array_values(array_filter($nodesymlinks, $array_filter_callback));
    $form_state['values']['menu']['nodesymlinks']['items'] = $nodesymlinks;
  }
  else {
    $nodesymlinks = (array) $node->nodesymlinks['items'];
  }
  $form['menu']['nodesymlinks'] = array(
    '#type' => 'fieldset',
    // @TODO: Fix this.
    '#type' => 'container',
    '#title' => t('Show in More Locations'),
    '#access' => user_access('administer menu') && user_access('administer nodesymlinks'),
    '#collapsible' => TRUE,
    '#collapsed' => empty($nodesymlinks) ? TRUE : FALSE,
    '#tree' => TRUE,
    '#weight' => 1,
    '#attributes' => array(
      'class' => array(
        'nodesymlinks-items-form',
      ),
    ),
    '#states' => array(
      'invisible' => array(
        'input[name="menu[enabled]"]' => array(
          'checked' => FALSE,
        ),
      ),
    ),
  );
  $form['menu']['nodesymlinks']['items'] = array(
    '#tree' => TRUE,
    '#theme' => 'nodesymlinks_form_items',
  );
  $delta = 0;
  foreach ($nodesymlinks as $item) {
    $form['menu']['nodesymlinks']['items'][$delta++] = _nodesymlinks_form_field($item, $node);
  }
  if (isset($form_state['nodesymlinks']['item_count'])) {
    $new_max_delta = $form_state['nodesymlinks']['item_count'] - 1;
    for ($delta; $delta <= $new_max_delta; ++$delta) {
      $new_item = array(
        'new_item' => TRUE,
        'link_title' => $node->title,
        'mlid' => 0,
        'alias' => array(),
      );
      $form['menu']['nodesymlinks']['items'][$delta] = _nodesymlinks_form_field($new_item, $node);
    }
  }
  $form['menu']['nodesymlinks']['#description'] = _nodesymlinks_form_description();
  $form['menu']['nodesymlinks']['delete'] = array(
    '#type' => 'submit',
    '#value' => t('Delete locations'),
    '#submit' => array(
      'nodesymlinks_node_form_submit_delete',
    ),
    '#ajax' => array(
      'callback' => '_nodesymlinks_form_ajax',
      'wrapper' => 'nodesymlinks-items-ajax',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );
  $form['menu']['nodesymlinks']['add'] = array(
    '#type' => 'submit',
    '#value' => t('Add location'),
    '#submit' => array(
      'nodesymlinks_node_form_submit_add',
    ),
    '#ajax' => array(
      'callback' => '_nodesymlinks_form_ajax',
      'wrapper' => 'nodesymlinks-items-ajax',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );
  if (isset($form_state['clicked_button'])) {
    $clicked_val = $form_state['clicked_button']['#value'];
    if ($clicked_val == t('Add Location') || $clicked_val == t('Delete Location(s)')) {
      $form['menu']['enabled']['#default_value'] = TRUE;
      $form['menu']['nodesymlinks']['#collapsed'] = FALSE;
    }
  }
}