Hi i done this as follows :
in RouteSubscriber.php
:
<?php
namespace Drupal\module_name\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
/**
* Listens to the dynamic route events.
*/
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
protected function alterRoutes(RouteCollection $collection) {
// Change path '/node/edit' to custom controller action.
if ($route = $collection->get('entity.node.edit_form')) {
$route->setDefault('_controller', '\Drupal\module_name\Controller\MyController::alter_edit_route');
}
}
}
and in MyController.php
:
/**
* Function to redirect the edit route to custom route
*/
public function alter_edit_route(){
$node_id = \Drupal::routeMatch()->getRawParameter('node');
//get the content type using node id
$entityObj = entity_load('node',$node_id);
$bundle = $entityObj->bundle();
if($bundle=="custom_service_name"){
//redirect to custom service list page
$response = new RedirectResponse('/module_name/custom_service/'.$node_id);
$response->send();
return;
}
else return $this->redirect('entity.node.edit_form', ['node' => $node_id]);
}
NB:- A problem here is that its redirecting to the custom url for the specific content type
. But when taking other content type nodes
its hanging with the too many redirections.
Welcoming your insights on this . thanks .
UPDATE
Okay , here its how i resolved the problem . For the custom content type node its using the redirectResponse to redirect to custom page . For other content type nodes its creating the specific content type form and load the values to it for the specific node id . So in MyController.php
/**
* Function to redirect the edit route to custom route
*/
public function alter_edit_route(){
$node_id = \Drupal::routeMatch()->getRawParameter('node');
//get the content type using node id
$entityObj = entity_load('node',$node_id);
$bundle = $entityObj->bundle();
if($bundle=="custom_service_name"){
//redirect to custom service list page
$response = new RedirectResponse('/module_name/custom_service/'.$node_id);
$response->send();
}
else {
//programmatically load the node edit page with value
$node = \Drupal::entityTypeManager()->getStorage('node')->load($node_id);
$form = \Drupal::entityTypeManager()
->getFormObject('node', 'default')
->setEntity($node);
return \Drupal::formBuilder()->getForm($form);
}
}
_entity_form
and add a_controller
key. You might need to adjust other route settings.