How to do a redirect in node.html.twig?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty{ margin-bottom:0;
}
up vote
1
down vote
favorite
I'm trying to do a redirect on my node.html.twig
template based on the content type because I'm using views for my content.
I tried this:
{% do redirect('/example') %}
or this:
{% redirect '/example' %}
but nothing works. Any idea?
8 redirect
add a comment |
up vote
1
down vote
favorite
I'm trying to do a redirect on my node.html.twig
template based on the content type because I'm using views for my content.
I tried this:
{% do redirect('/example') %}
or this:
{% redirect '/example' %}
but nothing works. Any idea?
8 redirect
1
You shouldn't (and hopefully can't) perform a redirect in the template layer. This needs to be done much further up the chain
– Clive♦
2 days ago
Did you try just having a Views path set tonode/%nid
to overtake content?
– Kevin
2 days ago
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm trying to do a redirect on my node.html.twig
template based on the content type because I'm using views for my content.
I tried this:
{% do redirect('/example') %}
or this:
{% redirect '/example' %}
but nothing works. Any idea?
8 redirect
I'm trying to do a redirect on my node.html.twig
template based on the content type because I'm using views for my content.
I tried this:
{% do redirect('/example') %}
or this:
{% redirect '/example' %}
but nothing works. Any idea?
8 redirect
8 redirect
edited 2 days ago
Pierre.Vriens
35.2k1335155
35.2k1335155
asked 2 days ago
Mauricio
132
132
1
You shouldn't (and hopefully can't) perform a redirect in the template layer. This needs to be done much further up the chain
– Clive♦
2 days ago
Did you try just having a Views path set tonode/%nid
to overtake content?
– Kevin
2 days ago
add a comment |
1
You shouldn't (and hopefully can't) perform a redirect in the template layer. This needs to be done much further up the chain
– Clive♦
2 days ago
Did you try just having a Views path set tonode/%nid
to overtake content?
– Kevin
2 days ago
1
1
You shouldn't (and hopefully can't) perform a redirect in the template layer. This needs to be done much further up the chain
– Clive♦
2 days ago
You shouldn't (and hopefully can't) perform a redirect in the template layer. This needs to be done much further up the chain
– Clive♦
2 days ago
Did you try just having a Views path set to
node/%nid
to overtake content?– Kevin
2 days ago
Did you try just having a Views path set to
node/%nid
to overtake content?– Kevin
2 days ago
add a comment |
3 Answers
3
active
oldest
votes
up vote
4
down vote
accepted
Don't do it in the Twig file. This is not where that logic belongs. It should be in an Event.
See this similar question: EventSubscriber called on specific node type or path
All that is left is checking the node type and sending a RedirectResponse
.
Gabe Sullice has a Gist that sounds very close to what you are trying to accomplish: https://gist.github.com/gabesullice/96bdae1518d11bb4565e72c33ffcae4c
So your code would fill in something like:
/**
* Determines whether we should send a RedirectResponse for this node.
*/
protected function redirectApplies(NodeInterface $node) : bool {
$redirect_types = ['type_1', 'type_2', 'type_3'];
return in_array($node->getType(), $redirect_types);
}
But if you go to different urls for different types, you will need to adjust it a little more.
Yes, the logic belongs either in the controller or in an event subscriber. Instead of the response event (as in the linked Gist) a faster option would be the request event, see this change record drupal.org/node/2013014.
– 4k4
2 days ago
This method works perfectly, i used and modified a few this code and works finally @kevin
– Mauricio
2 days ago
add a comment |
up vote
0
down vote
Where did you see the redirect functionality? I dont think it's part of standard twig or anything drupal provides: https://www.drupal.org/docs/8/theming/twig/functions-in-twig-templates
You'd be better of doing the redirect in a proprocess function maybe. This might help
Redirect basic page to external url when visiting page
It looks like its a function in CraftCMS but not in Twig itself. docs.craftcms.com/v2/templating/redirect.html
– Kevin
2 days ago
(that is to say, they must have created a custom Twig function and it shows up in google results - it also appears in BranchCMS).
– Kevin
2 days ago
add a comment |
up vote
0
down vote
As @Kevin suggested
i created a custom module based on Gabe Sullice code:
https://gist.github.com/gabesullice/96bdae1518d11bb4565e72c33ffcae4c
My final code redirect to custom url by content type:
namespace Drupalredirect_node_moduleEventSubscriber;
use SymfonyComponentEventDispatcherEventSubscriberInterface;
use SymfonyComponentEventDispatcherEvent;
use DrupalCoreRoutingCurrentRouteMatch;
use DrupalnodeNodeInterface;
use DrupalCoreCacheCacheableRedirectResponse;
use SymfonyComponentHttpFoundationRedirectResponse;
/**
* Class ResponseSubscriber.
*
* @package Drupalredirect_node_module
*/
class ResponseSubscriber implements EventSubscriberInterface {
/**
* DrupalCoreRoutingCurrentRouteMatch definition.
*
* @var DrupalCoreRoutingCurrentRouteMatch
*/
protected $routeMatch;
/**
* Constructor.
*/
public function __construct(CurrentRouteMatch $current_route_match) {
$this->routeMatch = $current_route_match;
}
/**
* {@inheritdoc}
*/
static function getSubscribedEvents() {
$events['kernel.response'] = ['handle'];
return $events;
}
/**
* This method is called whenever the kernel.response event is
* dispatched.
*
* @param GetResponseEvent $event
*/
public function handle(Event $event) {
$route_name = $this->routeMatch->getRouteName();
switch ($route_name) {
case 'entity.node.canonical':
$node = $this->routeMatch->getParameter('node');
if ($this->redirectApplies($node)) {
$event->setResponse(new RedirectResponse($this->getRedirect($node)));
}
}
}
/**
* Determines whether we should send a RedirectResponse for this node.
*/
protected function redirectApplies(NodeInterface $node) {
// One should have logic here returning TRUE or FALSE for whether we should redirect this response.
$redirect_on = ['programas', 'servicios', 'aliados', 'testimonios', 'nosotros', 'nuestro_equipo', 'datos_de_contacto', 'banner', 'seccion'];
return (in_array($node->getType(), $redirect_on));
}
protected function getRedirect(NodeInterface $node) {
// We can cache this response when this is fixed: https://www.drupal.org/node/2573807
// $response = CacheableRedirectResponse::create($url);
// $response->addCacheableDependency($node);
// return $response;
switch ($node->getType()) {
case 'nuestro_equipo':
$url = "/profesionales";
break;
case 'datos_de_contacto':
$url = "/contacto";
break;
case 'nosotros':
$url = "/nosotros";
break;
case 'banner':
$url = "/";
break;
case 'testimonios':
$url = "/testimonios";
break;
case 'aliados':
$url = "/aliados";
break;
default:
$search_chars = array('á','é','í','ó','ú','ñ','Á','É','Í','Ó','Ú','Ñ',' ');
$replace_chars= array('a','e','i','o','u','n','a','e','i','o','u','n','-');
$node_title = str_replace($search_chars, $replace_chars, strtolower( $node->getTitle() ));
$url = "/".$node->getType()."/".$node_title;
break;
}
return $url;
}
}
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Don't do it in the Twig file. This is not where that logic belongs. It should be in an Event.
See this similar question: EventSubscriber called on specific node type or path
All that is left is checking the node type and sending a RedirectResponse
.
Gabe Sullice has a Gist that sounds very close to what you are trying to accomplish: https://gist.github.com/gabesullice/96bdae1518d11bb4565e72c33ffcae4c
So your code would fill in something like:
/**
* Determines whether we should send a RedirectResponse for this node.
*/
protected function redirectApplies(NodeInterface $node) : bool {
$redirect_types = ['type_1', 'type_2', 'type_3'];
return in_array($node->getType(), $redirect_types);
}
But if you go to different urls for different types, you will need to adjust it a little more.
Yes, the logic belongs either in the controller or in an event subscriber. Instead of the response event (as in the linked Gist) a faster option would be the request event, see this change record drupal.org/node/2013014.
– 4k4
2 days ago
This method works perfectly, i used and modified a few this code and works finally @kevin
– Mauricio
2 days ago
add a comment |
up vote
4
down vote
accepted
Don't do it in the Twig file. This is not where that logic belongs. It should be in an Event.
See this similar question: EventSubscriber called on specific node type or path
All that is left is checking the node type and sending a RedirectResponse
.
Gabe Sullice has a Gist that sounds very close to what you are trying to accomplish: https://gist.github.com/gabesullice/96bdae1518d11bb4565e72c33ffcae4c
So your code would fill in something like:
/**
* Determines whether we should send a RedirectResponse for this node.
*/
protected function redirectApplies(NodeInterface $node) : bool {
$redirect_types = ['type_1', 'type_2', 'type_3'];
return in_array($node->getType(), $redirect_types);
}
But if you go to different urls for different types, you will need to adjust it a little more.
Yes, the logic belongs either in the controller or in an event subscriber. Instead of the response event (as in the linked Gist) a faster option would be the request event, see this change record drupal.org/node/2013014.
– 4k4
2 days ago
This method works perfectly, i used and modified a few this code and works finally @kevin
– Mauricio
2 days ago
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Don't do it in the Twig file. This is not where that logic belongs. It should be in an Event.
See this similar question: EventSubscriber called on specific node type or path
All that is left is checking the node type and sending a RedirectResponse
.
Gabe Sullice has a Gist that sounds very close to what you are trying to accomplish: https://gist.github.com/gabesullice/96bdae1518d11bb4565e72c33ffcae4c
So your code would fill in something like:
/**
* Determines whether we should send a RedirectResponse for this node.
*/
protected function redirectApplies(NodeInterface $node) : bool {
$redirect_types = ['type_1', 'type_2', 'type_3'];
return in_array($node->getType(), $redirect_types);
}
But if you go to different urls for different types, you will need to adjust it a little more.
Don't do it in the Twig file. This is not where that logic belongs. It should be in an Event.
See this similar question: EventSubscriber called on specific node type or path
All that is left is checking the node type and sending a RedirectResponse
.
Gabe Sullice has a Gist that sounds very close to what you are trying to accomplish: https://gist.github.com/gabesullice/96bdae1518d11bb4565e72c33ffcae4c
So your code would fill in something like:
/**
* Determines whether we should send a RedirectResponse for this node.
*/
protected function redirectApplies(NodeInterface $node) : bool {
$redirect_types = ['type_1', 'type_2', 'type_3'];
return in_array($node->getType(), $redirect_types);
}
But if you go to different urls for different types, you will need to adjust it a little more.
edited 2 days ago
answered 2 days ago
Kevin
16.9k847105
16.9k847105
Yes, the logic belongs either in the controller or in an event subscriber. Instead of the response event (as in the linked Gist) a faster option would be the request event, see this change record drupal.org/node/2013014.
– 4k4
2 days ago
This method works perfectly, i used and modified a few this code and works finally @kevin
– Mauricio
2 days ago
add a comment |
Yes, the logic belongs either in the controller or in an event subscriber. Instead of the response event (as in the linked Gist) a faster option would be the request event, see this change record drupal.org/node/2013014.
– 4k4
2 days ago
This method works perfectly, i used and modified a few this code and works finally @kevin
– Mauricio
2 days ago
Yes, the logic belongs either in the controller or in an event subscriber. Instead of the response event (as in the linked Gist) a faster option would be the request event, see this change record drupal.org/node/2013014.
– 4k4
2 days ago
Yes, the logic belongs either in the controller or in an event subscriber. Instead of the response event (as in the linked Gist) a faster option would be the request event, see this change record drupal.org/node/2013014.
– 4k4
2 days ago
This method works perfectly, i used and modified a few this code and works finally @kevin
– Mauricio
2 days ago
This method works perfectly, i used and modified a few this code and works finally @kevin
– Mauricio
2 days ago
add a comment |
up vote
0
down vote
Where did you see the redirect functionality? I dont think it's part of standard twig or anything drupal provides: https://www.drupal.org/docs/8/theming/twig/functions-in-twig-templates
You'd be better of doing the redirect in a proprocess function maybe. This might help
Redirect basic page to external url when visiting page
It looks like its a function in CraftCMS but not in Twig itself. docs.craftcms.com/v2/templating/redirect.html
– Kevin
2 days ago
(that is to say, they must have created a custom Twig function and it shows up in google results - it also appears in BranchCMS).
– Kevin
2 days ago
add a comment |
up vote
0
down vote
Where did you see the redirect functionality? I dont think it's part of standard twig or anything drupal provides: https://www.drupal.org/docs/8/theming/twig/functions-in-twig-templates
You'd be better of doing the redirect in a proprocess function maybe. This might help
Redirect basic page to external url when visiting page
It looks like its a function in CraftCMS but not in Twig itself. docs.craftcms.com/v2/templating/redirect.html
– Kevin
2 days ago
(that is to say, they must have created a custom Twig function and it shows up in google results - it also appears in BranchCMS).
– Kevin
2 days ago
add a comment |
up vote
0
down vote
up vote
0
down vote
Where did you see the redirect functionality? I dont think it's part of standard twig or anything drupal provides: https://www.drupal.org/docs/8/theming/twig/functions-in-twig-templates
You'd be better of doing the redirect in a proprocess function maybe. This might help
Redirect basic page to external url when visiting page
Where did you see the redirect functionality? I dont think it's part of standard twig or anything drupal provides: https://www.drupal.org/docs/8/theming/twig/functions-in-twig-templates
You'd be better of doing the redirect in a proprocess function maybe. This might help
Redirect basic page to external url when visiting page
answered 2 days ago
Leigh Mason
1,32846
1,32846
It looks like its a function in CraftCMS but not in Twig itself. docs.craftcms.com/v2/templating/redirect.html
– Kevin
2 days ago
(that is to say, they must have created a custom Twig function and it shows up in google results - it also appears in BranchCMS).
– Kevin
2 days ago
add a comment |
It looks like its a function in CraftCMS but not in Twig itself. docs.craftcms.com/v2/templating/redirect.html
– Kevin
2 days ago
(that is to say, they must have created a custom Twig function and it shows up in google results - it also appears in BranchCMS).
– Kevin
2 days ago
It looks like its a function in CraftCMS but not in Twig itself. docs.craftcms.com/v2/templating/redirect.html
– Kevin
2 days ago
It looks like its a function in CraftCMS but not in Twig itself. docs.craftcms.com/v2/templating/redirect.html
– Kevin
2 days ago
(that is to say, they must have created a custom Twig function and it shows up in google results - it also appears in BranchCMS).
– Kevin
2 days ago
(that is to say, they must have created a custom Twig function and it shows up in google results - it also appears in BranchCMS).
– Kevin
2 days ago
add a comment |
up vote
0
down vote
As @Kevin suggested
i created a custom module based on Gabe Sullice code:
https://gist.github.com/gabesullice/96bdae1518d11bb4565e72c33ffcae4c
My final code redirect to custom url by content type:
namespace Drupalredirect_node_moduleEventSubscriber;
use SymfonyComponentEventDispatcherEventSubscriberInterface;
use SymfonyComponentEventDispatcherEvent;
use DrupalCoreRoutingCurrentRouteMatch;
use DrupalnodeNodeInterface;
use DrupalCoreCacheCacheableRedirectResponse;
use SymfonyComponentHttpFoundationRedirectResponse;
/**
* Class ResponseSubscriber.
*
* @package Drupalredirect_node_module
*/
class ResponseSubscriber implements EventSubscriberInterface {
/**
* DrupalCoreRoutingCurrentRouteMatch definition.
*
* @var DrupalCoreRoutingCurrentRouteMatch
*/
protected $routeMatch;
/**
* Constructor.
*/
public function __construct(CurrentRouteMatch $current_route_match) {
$this->routeMatch = $current_route_match;
}
/**
* {@inheritdoc}
*/
static function getSubscribedEvents() {
$events['kernel.response'] = ['handle'];
return $events;
}
/**
* This method is called whenever the kernel.response event is
* dispatched.
*
* @param GetResponseEvent $event
*/
public function handle(Event $event) {
$route_name = $this->routeMatch->getRouteName();
switch ($route_name) {
case 'entity.node.canonical':
$node = $this->routeMatch->getParameter('node');
if ($this->redirectApplies($node)) {
$event->setResponse(new RedirectResponse($this->getRedirect($node)));
}
}
}
/**
* Determines whether we should send a RedirectResponse for this node.
*/
protected function redirectApplies(NodeInterface $node) {
// One should have logic here returning TRUE or FALSE for whether we should redirect this response.
$redirect_on = ['programas', 'servicios', 'aliados', 'testimonios', 'nosotros', 'nuestro_equipo', 'datos_de_contacto', 'banner', 'seccion'];
return (in_array($node->getType(), $redirect_on));
}
protected function getRedirect(NodeInterface $node) {
// We can cache this response when this is fixed: https://www.drupal.org/node/2573807
// $response = CacheableRedirectResponse::create($url);
// $response->addCacheableDependency($node);
// return $response;
switch ($node->getType()) {
case 'nuestro_equipo':
$url = "/profesionales";
break;
case 'datos_de_contacto':
$url = "/contacto";
break;
case 'nosotros':
$url = "/nosotros";
break;
case 'banner':
$url = "/";
break;
case 'testimonios':
$url = "/testimonios";
break;
case 'aliados':
$url = "/aliados";
break;
default:
$search_chars = array('á','é','í','ó','ú','ñ','Á','É','Í','Ó','Ú','Ñ',' ');
$replace_chars= array('a','e','i','o','u','n','a','e','i','o','u','n','-');
$node_title = str_replace($search_chars, $replace_chars, strtolower( $node->getTitle() ));
$url = "/".$node->getType()."/".$node_title;
break;
}
return $url;
}
}
add a comment |
up vote
0
down vote
As @Kevin suggested
i created a custom module based on Gabe Sullice code:
https://gist.github.com/gabesullice/96bdae1518d11bb4565e72c33ffcae4c
My final code redirect to custom url by content type:
namespace Drupalredirect_node_moduleEventSubscriber;
use SymfonyComponentEventDispatcherEventSubscriberInterface;
use SymfonyComponentEventDispatcherEvent;
use DrupalCoreRoutingCurrentRouteMatch;
use DrupalnodeNodeInterface;
use DrupalCoreCacheCacheableRedirectResponse;
use SymfonyComponentHttpFoundationRedirectResponse;
/**
* Class ResponseSubscriber.
*
* @package Drupalredirect_node_module
*/
class ResponseSubscriber implements EventSubscriberInterface {
/**
* DrupalCoreRoutingCurrentRouteMatch definition.
*
* @var DrupalCoreRoutingCurrentRouteMatch
*/
protected $routeMatch;
/**
* Constructor.
*/
public function __construct(CurrentRouteMatch $current_route_match) {
$this->routeMatch = $current_route_match;
}
/**
* {@inheritdoc}
*/
static function getSubscribedEvents() {
$events['kernel.response'] = ['handle'];
return $events;
}
/**
* This method is called whenever the kernel.response event is
* dispatched.
*
* @param GetResponseEvent $event
*/
public function handle(Event $event) {
$route_name = $this->routeMatch->getRouteName();
switch ($route_name) {
case 'entity.node.canonical':
$node = $this->routeMatch->getParameter('node');
if ($this->redirectApplies($node)) {
$event->setResponse(new RedirectResponse($this->getRedirect($node)));
}
}
}
/**
* Determines whether we should send a RedirectResponse for this node.
*/
protected function redirectApplies(NodeInterface $node) {
// One should have logic here returning TRUE or FALSE for whether we should redirect this response.
$redirect_on = ['programas', 'servicios', 'aliados', 'testimonios', 'nosotros', 'nuestro_equipo', 'datos_de_contacto', 'banner', 'seccion'];
return (in_array($node->getType(), $redirect_on));
}
protected function getRedirect(NodeInterface $node) {
// We can cache this response when this is fixed: https://www.drupal.org/node/2573807
// $response = CacheableRedirectResponse::create($url);
// $response->addCacheableDependency($node);
// return $response;
switch ($node->getType()) {
case 'nuestro_equipo':
$url = "/profesionales";
break;
case 'datos_de_contacto':
$url = "/contacto";
break;
case 'nosotros':
$url = "/nosotros";
break;
case 'banner':
$url = "/";
break;
case 'testimonios':
$url = "/testimonios";
break;
case 'aliados':
$url = "/aliados";
break;
default:
$search_chars = array('á','é','í','ó','ú','ñ','Á','É','Í','Ó','Ú','Ñ',' ');
$replace_chars= array('a','e','i','o','u','n','a','e','i','o','u','n','-');
$node_title = str_replace($search_chars, $replace_chars, strtolower( $node->getTitle() ));
$url = "/".$node->getType()."/".$node_title;
break;
}
return $url;
}
}
add a comment |
up vote
0
down vote
up vote
0
down vote
As @Kevin suggested
i created a custom module based on Gabe Sullice code:
https://gist.github.com/gabesullice/96bdae1518d11bb4565e72c33ffcae4c
My final code redirect to custom url by content type:
namespace Drupalredirect_node_moduleEventSubscriber;
use SymfonyComponentEventDispatcherEventSubscriberInterface;
use SymfonyComponentEventDispatcherEvent;
use DrupalCoreRoutingCurrentRouteMatch;
use DrupalnodeNodeInterface;
use DrupalCoreCacheCacheableRedirectResponse;
use SymfonyComponentHttpFoundationRedirectResponse;
/**
* Class ResponseSubscriber.
*
* @package Drupalredirect_node_module
*/
class ResponseSubscriber implements EventSubscriberInterface {
/**
* DrupalCoreRoutingCurrentRouteMatch definition.
*
* @var DrupalCoreRoutingCurrentRouteMatch
*/
protected $routeMatch;
/**
* Constructor.
*/
public function __construct(CurrentRouteMatch $current_route_match) {
$this->routeMatch = $current_route_match;
}
/**
* {@inheritdoc}
*/
static function getSubscribedEvents() {
$events['kernel.response'] = ['handle'];
return $events;
}
/**
* This method is called whenever the kernel.response event is
* dispatched.
*
* @param GetResponseEvent $event
*/
public function handle(Event $event) {
$route_name = $this->routeMatch->getRouteName();
switch ($route_name) {
case 'entity.node.canonical':
$node = $this->routeMatch->getParameter('node');
if ($this->redirectApplies($node)) {
$event->setResponse(new RedirectResponse($this->getRedirect($node)));
}
}
}
/**
* Determines whether we should send a RedirectResponse for this node.
*/
protected function redirectApplies(NodeInterface $node) {
// One should have logic here returning TRUE or FALSE for whether we should redirect this response.
$redirect_on = ['programas', 'servicios', 'aliados', 'testimonios', 'nosotros', 'nuestro_equipo', 'datos_de_contacto', 'banner', 'seccion'];
return (in_array($node->getType(), $redirect_on));
}
protected function getRedirect(NodeInterface $node) {
// We can cache this response when this is fixed: https://www.drupal.org/node/2573807
// $response = CacheableRedirectResponse::create($url);
// $response->addCacheableDependency($node);
// return $response;
switch ($node->getType()) {
case 'nuestro_equipo':
$url = "/profesionales";
break;
case 'datos_de_contacto':
$url = "/contacto";
break;
case 'nosotros':
$url = "/nosotros";
break;
case 'banner':
$url = "/";
break;
case 'testimonios':
$url = "/testimonios";
break;
case 'aliados':
$url = "/aliados";
break;
default:
$search_chars = array('á','é','í','ó','ú','ñ','Á','É','Í','Ó','Ú','Ñ',' ');
$replace_chars= array('a','e','i','o','u','n','a','e','i','o','u','n','-');
$node_title = str_replace($search_chars, $replace_chars, strtolower( $node->getTitle() ));
$url = "/".$node->getType()."/".$node_title;
break;
}
return $url;
}
}
As @Kevin suggested
i created a custom module based on Gabe Sullice code:
https://gist.github.com/gabesullice/96bdae1518d11bb4565e72c33ffcae4c
My final code redirect to custom url by content type:
namespace Drupalredirect_node_moduleEventSubscriber;
use SymfonyComponentEventDispatcherEventSubscriberInterface;
use SymfonyComponentEventDispatcherEvent;
use DrupalCoreRoutingCurrentRouteMatch;
use DrupalnodeNodeInterface;
use DrupalCoreCacheCacheableRedirectResponse;
use SymfonyComponentHttpFoundationRedirectResponse;
/**
* Class ResponseSubscriber.
*
* @package Drupalredirect_node_module
*/
class ResponseSubscriber implements EventSubscriberInterface {
/**
* DrupalCoreRoutingCurrentRouteMatch definition.
*
* @var DrupalCoreRoutingCurrentRouteMatch
*/
protected $routeMatch;
/**
* Constructor.
*/
public function __construct(CurrentRouteMatch $current_route_match) {
$this->routeMatch = $current_route_match;
}
/**
* {@inheritdoc}
*/
static function getSubscribedEvents() {
$events['kernel.response'] = ['handle'];
return $events;
}
/**
* This method is called whenever the kernel.response event is
* dispatched.
*
* @param GetResponseEvent $event
*/
public function handle(Event $event) {
$route_name = $this->routeMatch->getRouteName();
switch ($route_name) {
case 'entity.node.canonical':
$node = $this->routeMatch->getParameter('node');
if ($this->redirectApplies($node)) {
$event->setResponse(new RedirectResponse($this->getRedirect($node)));
}
}
}
/**
* Determines whether we should send a RedirectResponse for this node.
*/
protected function redirectApplies(NodeInterface $node) {
// One should have logic here returning TRUE or FALSE for whether we should redirect this response.
$redirect_on = ['programas', 'servicios', 'aliados', 'testimonios', 'nosotros', 'nuestro_equipo', 'datos_de_contacto', 'banner', 'seccion'];
return (in_array($node->getType(), $redirect_on));
}
protected function getRedirect(NodeInterface $node) {
// We can cache this response when this is fixed: https://www.drupal.org/node/2573807
// $response = CacheableRedirectResponse::create($url);
// $response->addCacheableDependency($node);
// return $response;
switch ($node->getType()) {
case 'nuestro_equipo':
$url = "/profesionales";
break;
case 'datos_de_contacto':
$url = "/contacto";
break;
case 'nosotros':
$url = "/nosotros";
break;
case 'banner':
$url = "/";
break;
case 'testimonios':
$url = "/testimonios";
break;
case 'aliados':
$url = "/aliados";
break;
default:
$search_chars = array('á','é','í','ó','ú','ñ','Á','É','Í','Ó','Ú','Ñ',' ');
$replace_chars= array('a','e','i','o','u','n','a','e','i','o','u','n','-');
$node_title = str_replace($search_chars, $replace_chars, strtolower( $node->getTitle() ));
$url = "/".$node->getType()."/".$node_title;
break;
}
return $url;
}
}
answered 2 days ago
Mauricio
132
132
add a comment |
add a comment |
Thanks for contributing an answer to Drupal Answers!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f273233%2fhow-to-do-a-redirect-in-node-html-twig%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You shouldn't (and hopefully can't) perform a redirect in the template layer. This needs to be done much further up the chain
– Clive♦
2 days ago
Did you try just having a Views path set to
node/%nid
to overtake content?– Kevin
2 days ago