0

I have a content type called 'Checklist' which I bring forward using Views, in it's node-edit form.

I wrote PHP code within the header in order to do this, here it is:

<?php 

    $view = views_get_current_view();
    $arg0 = $view->args[0];

    $nodeToLoad = $view->result[0]->nid; //I get the node to load from Fields within my view
    module_load_include('inc', 'node', 'node.pages');

    $node = node_load($nodeToLoad);
    dsm($nodeToLoad);
    $edit_page = drupal_get_form('page_node_form', $node);

    $edit_page['additional_settings']['#access'] = FALSE;
    $edit_page['buttons']['submit']['#submit'][] = 'redirect_submit';
    print drupal_render($edit_page );

    function redirect_submit($form, &$form_state){
        $form_state['redirect'] = 'www.google.com';
    }
?>

Now what I want is to add a custom redirect upon saving this particular form.

I've tried this to no avail:

    $edit_page['buttons']['submit']['#submit'][] = 'redirect_submit';
    print drupal_render($edit_page );

    function redirect_submit($form, &$form_state){
        $form_state['redirect'] = 'www.google.com';
    }

Is it possible to this within the Views header?


Notes:

  • I have also done this with the Rules module but I'd like to learn if it's possible to do with PHP code in the Views header so I don't split functionality in 2 parts of my project.

  • This can also be easily done using a custom hook but again, if it's possible to do this within the Views header then that's the trick I'm looking for.

Thanks.

1

You could to use hook_form_FORM_ID. Something like this:

function mymodule_form_page_node_form_ater(&$form, &$form_state, $form_id){
   if(current_path() == 'page/where/my/view/is'){
     $form_state['redirect'] = 'page/to/redirect';
   }
}

If you don't want to use a hook, you can do something like this:

 $edit_page['buttons']['submit']['#submit'][] = 'redirect_submit';

Then,

 function redirect_submit($form, &$form_state){
  $form_state['redirect'] = 'path/to/rediect';
 }

Still it is bad practice to inlcude php in your views header. You should implement a custom area handler.

  1. How to create a view area handler
  2. https://api.drupal.org/api/views/handlers!views_handler_area.inc/group/views_area_handlers/7
  3. https://oliverhuynh.wordpress.com/2011/06/23/custom-views-handler-drupal-7-template/
7
  • I appreciate the answer but I've clearly stated in the 'Notes' section of my question that I would like NOT to use custom hooks. Code that can fit in the Views header PHP code(like the rest of my code) is what I'm looking for. – nicholaswmin Mar 1 '15 at 16:44
  • It is very bad practice to write php code in your views header/footer/fields. I strongly recommend to review your approach to this problem. I will adjust my answer – Robin Mar 1 '15 at 16:48
  • Thanks, did not know that. Why do you consider it bad practice? I like the idea of having all the code located within that particular View, if the code is simple enough. – nicholaswmin Mar 1 '15 at 17:06
  • hm, I'm updating the question with the suggested code you gave me. It's still not redirecting for some reason.. – nicholaswmin Mar 1 '15 at 17:34
  • Try redirecting to an internal page of your website – Robin Mar 1 '15 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.