You are here

function mb_content_form_alter in More Buttons 7

Implements hook_form_alter().

File

mb_content/mb_content.module, line 84
Provides additional buttons for nodes.

Code

function mb_content_form_alter(&$form, &$form_state, $form_id) {

  // Make sure the form alter changes take place only in certain forms.
  // Get the right node types.
  $node_type = '';
  $module = 'mb_content';

  // Node add/edit forms.
  if (isset($form['#node']->type) && isset($form['#node_edit_form'])) {
    $node_type = $form['#node']->type;
  }
  elseif (isset($form['type']) && !empty($form['#type']['#value']) && isset($form['#node_edit_form'])) {
    $node_type = $form['#type']['#value'];
  }
  elseif (isset($form['#node']->type) && empty($form['#type']['#value']) && isset($form['#node_edit_form'])) {

    // Use temporary an content type as dummy to handle button management on the "Add content type" page.
    $node_type = 'mb_content_type_dummy';
  }
  switch ($form_id) {
    case 'node_type_form':

      // Provide the prepared MB Content button settings.
      // Check the specific case add content type form.
      if (empty($form['#node_type']->type)) {

        // Add content type.
        $type = 'mb_content_type_dummy';
      }
      else {

        // Edit an content type.
        $type = $form['#node_type']->type;
      }

      // It makes no sense to use the MB Content module with the content type panel.
      if ($type == 'panel') {
        return;
      }

      /**
       * The additional buttons and tab settings.
       */
      $form['node_buttons'] = array(
        '#type' => 'fieldset',
        '#title' => t('Button settings - content'),
        '#group' => 'additional_settings',
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#weight' => 4,
        '#attached' => array(
          'js' => array(
            drupal_get_path('module', $module) . '/' . $module . '_node_form.js',
          ),
        ),
      );

      // Provide "Cancel" button settings.
      $form['node_buttons'][$module . '_cancel'] = array(
        '#type' => 'select',
        '#title' => t('Cancel button'),
        '#description' => t('Please select using the button or its position.'),
        '#options' => mb_cancel_button_positions(),
        '#default_value' => variable_get($module . '_cancel_' . $type, 0),
      );

      // Provide "Save and continue" button settings.
      $form['node_buttons'][$module . '_sac'] = array(
        '#type' => 'select',
        '#title' => t('Save and continue button'),
        '#description' => t('Please select using the button or its position.'),
        '#options' => mb_save_button_positions($module),
        '#default_value' => variable_get($module . '_sac_' . $type, 0),
      );

      // Provide the "Create new" tab option.
      $form['node_buttons'][$module . '_tabcn'] = array(
        '#type' => 'checkbox',
        '#title' => t('Create new tab'),
        '#description' => t('Enable an "Create new" tab in addition to the %view or %edit tab.', array(
          '%view' => t('View'),
          '%edit' => t('Edit'),
        )),
        '#default_value' => variable_get($module . '_tabcn_' . $type, 0),
      );

      // Use an own submit callback to save the settings
      // on the add/edit content type forms.
      $form['#submit'][] = 'mb_content_admin_form_submit';
      break;
    case 'node_type_delete_confirm':

      // Use an own submit callback to delete MB Content
      // content type system variables if content types will be deleted.
      $form['#submit'][] = 'mb_content_delete_confirm_submit';
      break;
    case $node_type . '_node_form':

      // It makes no sense to use the MB Content module with the content type panel.
      if ($node_type == 'panel') {
        return;
      }
      $mb_default_values = mb_default_values();
      $mb_content_values = mb_get_values('mb');
      $destination = drupal_get_destination();
      $settings = array();
      $settings['cancel'] = variable_get($module . '_cancel_' . $node_type, 0);
      $settings['sac'] = variable_get($module . '_sac_' . $node_type, 0);
      $settings['tabcn'] = variable_get($module . '_tabcn_' . $node_type, 0);

      // Insert the referer.
      $referer = preg_replace('/.*\\/node\\//', 'node/', url($_SERVER["HTTP_REFERER"]));
      $form['#referer'] = $referer;
      $form_state['cache'] = TRUE;

      /**
       * Destination parameter handling.
       *
       * Deactivate the destination parameter
       * and keep these parameter in order to be able to use it anyway.
       */
      if (preg_match('/\\?destination/', $form['#action'])) {
        $form_state['storage']['#mb_redirect'] = preg_replace('/^.*\\?destination\\=/', '', $form['#action']);
      }
      elseif (preg_match('/\\?destination/', $form['#referer'])) {
        $form_state['storage']['#mb_redirect'] = preg_replace('/^.*\\?destination\\=/', '', $form['#referer']);
      }
      $form['#action'] = preg_replace('/\\?destination.*/', '', $form['#action']);
      if (isset($form_state['storage']['#mb_redirect'])) {
        $form['storage']['#mb_redirect'] = $form_state['storage']['#mb_redirect'];
      }
      elseif (isset($form['storage']['#mb_redirect'])) {
        $form_state['#mb_redirect'] = $form['storage']['#mb_redirect'];
      }

      // Identify the "Create new" tab action.
      if (preg_match('/node\\/\\d+/', $referer) && !isset($form_state['clicked_button']['#id'])) {
        $form['#tabcn_referer'] = $referer;
      }

      /**
       *  The "Save and continue" form element.
       */
      if ($settings['sac'] > 0) {

        // Left of Save.
        if ($settings['sac'] == 1) {
          $weight_sac = $form['actions']['submit']['#weight'] - 0.025;
        }

        // Right of Save
        if ($settings['sac'] == 2) {
          $weight_sac = $form['actions']['submit']['#weight'] + 0.025;
        }

        // Define the "Save and continue" form element.
        $form['actions']['sac'] = array(
          '#type' => 'submit',
          '#value' => isset($mb_content_values['sac']) ? t('@sac-value', array(
            '@sac-value' => t($mb_content_values['sac']),
          )) : t($mb_default_values['sac']),
          '#weight' => $weight_sac,
        );
        $form['#validate'][] = 'mb_content_changed_validate';
        $form['#submit'][] = 'mb_content_sac_submit';
      }

      /**
       *  The "Cancel" form element.
       */
      if ($settings['cancel'] > 0) {
        if ($settings['cancel'] == 1) {
          $weight_cancel = $form['actions']['submit']['#weight'] - 1;
        }
        elseif ($settings['cancel'] == 2) {
          $weight_cancel = 16;
        }

        // Define the "Cancel" form element.
        $form['actions']['cancel'] = array(
          '#type' => 'submit',
          '#value' => isset($mb_content_values['cancel']) ? t('@cancel-value', array(
            '@cancel-value' => t($mb_content_values['cancel']),
          )) : t($mb_default_values['cancel']),
          '#weight' => $weight_cancel,
          '#validate' => array(
            'mb_content_cancel_validate',
          ),
          '#limit_validation_errors' => array(),
        );
      }

      /**
       *  Extra validation for the standard buttons.
       *  Without this break the normal form handling.
       *  @see mb_content_cancel_validate(), mb_content_changed_submit()
       *  Call edit form from admin/content is no needed this callbacks.
       */
      if ($settings['sac'] > 0 || $destination['destination'] != 'admin/content') {
        $form['#validate'][] = 'mb_content_changed_validate';

        // Corresponding submit callbacks.
        $form['#submit'][] = 'mb_content_changed_submit';
      }

      // OG compatibility.
      // Provide the group ID.
      // Add an new group article.
      //      if (isset($_GET['gids_node'])) {
      //        $gids_node = $_GET['gids_node'];
      //        $form['#mnb']['og']['group_id'] = (int)$gids_node[0];
      //      }
      // Edit an group article.
      //      $og_context = module_invoke('og', 'get_context_by_url');
      //      if (isset($og_context->nid)) {
      //        $form['#mnb']['og']['group_id'] = $og_context->nid;
      //      }
      break;
  }
}