Hi. Not sure anyone is really working on the drupal 7 port but I'm trying to get a redirect working for my block form. Don't want it to go anywhere when they submit my form. Just stay on the current page or maybe homepage. I am using the following with a scattershot approach to test just where in the code I should be calling my $form_state['redirect'] but it's not working in any of these spots!

function formblock_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'node_type_form') {
    $options = array(t('Disabled'), t('Enabled'));
    $form['formblock'] = array(
      '#type' => 'fieldset',
      '#title' => t('Form block'),
      '#weight' => 10,
    );
    $form['actions']['submit']['#submit'][]='formblock_submit';
    $form_state['redirect'] = '/';
    $form['formblock']['formblock_expose'] = array(
      '#type' => 'radios',
      '#title' => t('Enable data entry from a block'),
      '#default_value' => variable_get('formblock_expose_'. $form['#node_type']->type, 0),
      '#options' => $options,
      '#description' => t('Enable this option to make the entry form for this content type available as a block.'),
    );
    if($form_id == 'formblock_node_form')
      {
        $form['actions']['submit']['#submit'][]='formblock_submit';
      }
    }

    function formblock_submit($form, &$form_state) {
      $form_state['redirect'] = '/';   
  }
}

Comments

rumblewand’s picture

Ok well obviously I want that on the block not the admin stuff so I moved it down here.

/**
* Generate a block containing a node entry form.
*/
function formblock_get_block($type) {
  if (node_access('create', $type)) {
    // Include page handler for node_add()
    module_load_include('inc', 'node', 'node.pages');
    // Note title before rendering of form.
    $title = drupal_get_title();
    $form = node_add($type);
    $types = node_type_get_names();
    $form['title']['#access']=TRUE;
    $form['title']['#prefix']='<div style="display:none;">';
    $form['title']['#value']='A/C Filter Reminder';
    $form['title']['#suffix']='</div>';
    $form['body']['#access']=FALSE;
    $form['tabs']['#access']=FALSE;
    $form['actions']['submit']['#submit'][]='formblock_submit';

    function formblock_submit($form, &$form_state) {
      $form_state['redirect'] = '/';   
  }
  
    // Restore title, which will have been overridden.
    drupal_set_title($title);
    return array(
      'subject' => t('@type form', array('@type' => $types[$type])),
      'content' => $form,
    );
  }
}

still with no success.

rumblewand’s picture

Got it. Needed to call it within the formblock_form_alter but outside of the two instances provided.

function formblock_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'node_type_form') {
    $options = array(t('Disabled'), t('Enabled'));
    $form['formblock'] = array(
      '#type' => 'fieldset',
      '#title' => t('Form block'),
      '#weight' => 10,
    );
    $form['formblock']['formblock_expose'] = array(
      '#type' => 'radios',
      '#title' => t('Enable data entry from a block'),
      '#default_value' => variable_get('formblock_expose_'. $form['#node_type']->type, 0),
      '#options' => $options,
      '#description' => t('Enable this option to make the entry form for this content type available as a block.'),
    );
}
    $form['actions']['submit']['#submit'][]='formblock_submit';
   
function formblock_submit($form, &$form_state) {
  $form_state['redirect'] = '';
  }
}
rumblewand’s picture

spoke too soon. that does work for creating the content in a block HOWEVER it prevents you from deleting content once it's created so... suck.

rumblewand’s picture

Status:Active» Fixed

Ok. sort of fixed but as of now it loses the system message verifying that the user successfully created anything after submitting the form.

/**
* Implementation of hook_form_alter().
*/

function formblock_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'node_type_form') {
    $options = array(t('Disabled'), t('Enabled'));
    $form['formblock'] = array(
      '#type' => 'fieldset',
      '#title' => t('Form block'),
      '#weight' => 10,
    );
    $form['formblock']['formblock_expose'] = array(
      '#type' => 'radios',
      '#title' => t('Enable data entry from a block'),
      '#default_value' => variable_get('formblock_expose_'. $form['#node_type']->type, 0),
      '#options' => $options,
      '#description' => t('Enable this option to make the entry form for this content type available as a block.'),
    );
}
  if ($form_id == 'ac_filter_reminder_node_form') {
    $form['actions']['submit']['#submit'][]='ac_filter_reminder_submit';
   
    function ac_filter_reminder_submit($form, &$form_state) {
      $form_state['redirect'] = '';
    }
  }
}

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

perumal.karthi’s picture

Thanks for this post.

prakash.mukta’s picture

Version:6.x-1.x-dev» 7.x-1.x-dev

Hi,

Redirection issue is fixed in Drupal 7 and you will get message Node created successfully !! Small modifications to above code

/**
* Implementation of hook_form_alter().
*/

function formblock_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'node_type_form') {
$options = array(t('Disabled'), t('Enabled'));
$form['formblock'] = array(
'#type' => 'fieldset',
'#title' => t('Form block'),
'#weight' => 10,
);
$form['formblock']['formblock_expose'] = array(
'#type' => 'radios',
'#title' => t('Enable data entry from a block'),
'#default_value' => variable_get('formblock_expose_'. $form['#node_type']->type, 0),
'#options' => $options,
'#description' => t('Enable this option to make the entry form for this content type available as a block.'),
);
}
if ($form_id == 'ac_filter_reminder_node_form') {
$form['actions']['submit']['#submit'][]='ac_filter_reminder_submit';

}
}
/*
This function needs to be out of Alter form
*/
function ac_filter_reminder_submit($form, &$form_state) {
$form_state['redirect'] = ''; // U can give path here as per your requirement
}

I hope this will help...
This is my first comment, I am new to this.
If any mistakes plz let me know. Thx

thatpatguy’s picture

adding onto what prakash.mukta posted I made a small tweak for my own specific purpose. I figured I might as well add it here in case someone else is looking to do something similar. This will check to see what language the site is in (for multilanguage website) as well as determine if the person submitting it is logged in or anonymous.

function FORM_ID_submit($form, &$form_state) {
  $lang = $GLOBALS['language']->language;
  $logged_in = $GLOBALS['user']->uid;
  if ($logged_in == '0'){    // Anonymous users have the uid of 0, if the user is logged in there will be no redirect
    if ($lang == 'fr'){
      $form_state['redirect'] = 'FRENCH_SUBMISSION_PAGE';
    }else{
      $form_state['redirect'] = 'ENGLISH_SUBMISSION_PAGE';
    }
  }
}