Redirect after node delete does not delete the node - redirect

I am implementing the 'form_alter' hook in Drupal 7. I want to redirect the web to a specific node after deleting any node of type 'article'.
It seems that the proper way of doing so is:
function mymodule_form_alter(&$form, &$form_state, $form_id){
switch ($form_id){
case 'node_delete_confirm':
if($form['#node']->type == 'article'){
$form['actions']['submit']['#submit'][] = '_mymodule_redirect';
}
break;
}
}
function _mymodule_redirect($form, &$form_state){
$form_state['redirect'] = 'node/60';
}
When I put this code in my module it does redirect after confirming the node delete but the node is not actually deleted, if I go to the home page it is still alive!
If I remove the code the node is deleted as expected and the webpage is redirected to the frontpage as usual.
What am I doing wrong?
UPDATE: I forced the 'node_delete_confirm_submit' before the redirect action writing the following line before adding my redirect handler:
$form['actions']['submit']['#submit'][] = 'node_delete_confirm_submit';
This solves the problem.

The easiest way to accomplish this task (and not have to use a hook at all) would be to use the Rules module. It's a nice clean way of performing any number of actions on your site, and I know there's a rule for redirecting the user after content of a certain type is deleted.

Just for clarification I repeat the entire correct answer:
/**
* Implements hook_form_alter()
*/
function MYMODULE_form_alter(&$form, &$form_state, $form_id){
switch ($form_id) {
case 'node_delete_confirm':
// replace 'article' in next line with your node type machine name
if($form['#node']->type == 'article') {
$form['actions']['submit']['#submit'][] = 'node_delete_confirm_submit';
$form['actions']['submit']['#submit'][] = '_MYMODULE_redirect';
}
break;
}
}
function _MYODULE_redirect($form, &$form_state){
// replace 'node/123' in next line with node you like redirect to
$form_state['redirect'] = 'node/123';
}
Doing only $form['actions']['submit']['#submit'][] = '_MYMODULE_redirect'; was not enough. Still $form['actions']['submit']['#submit'][] = 'node_delete_confirm_submit'; needed to be triggered. Now the delete and the redirect are both triggered.

Related

yii2 binds redirect to each link after first request

public function actionDone($id)
{
if ($model = $this->findModel($id)) {
$model["status"] = 3;
if ($model->save()) {
return $this->redirect(['test/index']);
}
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
It works only for the first time for each link. After that its just redirects to the 'test/index' without doing anything. Seems like browser (or smth else) remember, that if we open, for example, page site.com/?r=test/done&id=2 it should redirect to 'test/index' anyway.
Why is that? How can I fix it?
I even tried put die(); in the beginning of the method - anyway it redirects to 'test/index' until I use different link with another ID.
Thanks!

How to block url that customer type on browser in codeigniter?

I have created a website, And I don't to allowed customer to type link in browser.
I just want to allow customer click link on the website only.
please provide me a solution !!!
thank for help !!!
use token for in hyperlinks . deny if token wont match to value in session.
try the following:
in controller check if session['token'] is set,
if not set session['token'] to a rendom value.
if token not set or won't match to $_GET['token'] variable redirect to error page.
if token match to get variable (ie. $_GET['token']) then , pass that value to view.
i don't recomend this because doing this might affect the browser cache.
Controller constructor
function __construct()
{
parent::__construct();
$this->load->library('session');
if(!isset($_SESSION['token']))
{
$_SESSION['token'] = rand();
}
if((!$this->input->get('token')) || ($this->input->get('token') != $_SESSION['token']))
{
redirect('error_page');
}
// to pass to vew
$data['token'] = $_SESSION['token'];
$this->load->view('view_page',$data);
}
in view_page.php (view) replace links like this
Gallery link

Implementing Pass-Through with Grails Filters

Please note: Although I'm using the Grails Webflow plugin here, I am pretty sure this is just a generic question that any battle-weary Grails veteran could answer.
Grails 2.4.4 here. I have a need for a Grails Filter that inspects all traffic coming into a particular controller. If the app is in a particular state, I need to redirect traffic to another controller/action. Else, I need the filter to act as a no-op/pass-through filter (meaning it does nothing; just allows the request to pass through it and on to its intended destination):
package filters
class MyAppFilters {
def filters = {
fizzFilter(controller: 'fizz', action: '*') {
before = {
if(AppStateHolder.checkState() == AppState.Blue) {
redirect(controller: 'auth', action: 'unauthorized')
return false
} else {
// Allow request to continue on to its intended controller/action target ('no-op'/pass-through)
???
}
}
}
}
}
I have everything working perfectly for when the app state is AppState.Blue (so, the if-condition). The problem is that when app state isn't "blue", and that else executes, I keep getting infinite redirect errors. I believe Grails Webflow is complicating things, but I can't really fix anything with how it has been implemented.
I've also tried returning true/false from inside the else like so:
} else {
// Allow request to continue on to its intended controller/action target ('no-op'/pass-through)
return false // Also tried 'true'
}
But this produces the same infinite redirect errors. It looks like I need to do some kind of redirect/render/etc. inside this else or webflow will cause problems.
So I'm looking for a way to tell Grails to redirect to whatever was the controller/action off the request. Something like:
} else {
// Allow request to continue on to its intended controller/action target ('no-op'/pass-through)
redirect(controller: req.controller, action: req.action)
return false
}
Is this possible, if so, how (specifically)?

Change in URL to trigger Event in GWT

I am iterating through some data and generating parameterized URL links based on some conditions like this:
finishedHTML.append("<a href=\"http://" + domain + "&service=" + allEvents[eventCounter].EventService +"&day="+ offSet+(-1 * i) +"\"><img src=\"/info.jpg\" alt=\"Info\"/>");
I had the beginning of my application checking for URL parameters when the page was loaded/refreshed, and take some action depending on if the parameters were there and what they were.
However, I added a # to the beginning of the paramters, so the page wouldn't need to be refreshed, but now it's not triggering the function that checks the URL.
My question is how can I trigger an event in GWT when a user clicks a link? Do I need to generate GWT controls, and link a click handler? Is it possible to setup an event that fires when the URL is changed???
UPDATE:
Adam answered part of the initial question, but I can't get the querystring after "#". Is there a better way than the function below:
public static String getQueryString()
{
return Window.Location.getQueryString();
}
In other words, if I enter example.com?service=1 I get service=1 as my querystring. If I enter example.com#?service=1, I get a null value for the querystring..
Use History.addValueChangeHandler( handler ), and create a handler to catch the URL changes.
There's no need for click handlers etc., any change of the "hash" part in the URL will be sufficient.
EDIT:
See this code example - it will parse URLs of the form http://mydomain/my/path#tok1&tok2&tok3
public void onValueChange(ValueChangeEvent<String> event) {
String hash = event.getValue();
if ( hash.length() == 0 ) {
return;
}
String[] historyTokens = hash.split("&",0);
// do stuff according to tokens
}
(Just responding to your Update)
Have you tried
History.getToken()
to get the value after the "#"?

Zend Framework: Checking if route exists from code

in zend framework, is there anyway i can check if a route exists from code?
example
say the following routes/urls are valid (point to controller/action)
/users
/users/1 // /users?id=1
/users/page/1 /users?page=1
/users/tagged/tagname/page/1 /users?tagged=1&page=1
if the user tries to goto /users/nonexistantpage it should fail. soemthing to check if the user request the url, will it fail, but on the code level.
I believe you're looking for the match() method for the Zend router. See if that helps.
It's kinda old question, but I guess this is what you are looking for:
foreach(Zend_Controller_Front::getInstance()->getRouter()->getRoutes() as $route)
{
$route->match($uri);
}
For those using Zend Framework 2, this is very simple.
Let say we want to check if an URI matches against registered router and redirect the user if this is different from the current url.
$goto = 'http://www.mysite.tld/admin';
$request = $this->getRequest();
$request->setUri($goto);
if ($routeToBeMatched = $this->getServiceLocator()->get('Router')->match($request)) {
$currentRouteMatchName = $this->getEvent()->getRouteMatch()->getMatchedRouteName();
if ($routeToBeMatched->getMatchedRouteName() != $currentRouteMatchName) {
return $this->redirect()->toRoute($goto);
}
}
If you use rewrite router (Zend_Controller_Router_Rewrite), it has a method hasRoute($route_name)
Then if you want to check if route exists anywhere in your application you can check it like that:
Zend_Controller_Front::getInstance()->getRouter()->hasRoute("my_route");