I am converting a simple module from Drupal 7 to Drupal 8 and I used the Drupal Module Upgrader to do this. The code has been reformatted and some @FIXME notes have been added, I have no experience developing modules though and I'm not sure what to do.
When I search for more info concerning my problem I found this link https://www.drupal.org/node/2346779 but I'm not sure how to rewrite my code snippet exactly.
This is the .module file.
/**
* @file
* The littlurl module does little urls.
*/
/**
* Implements hook_menu().
*/
function littlurl_menu() {
$items['n'] = array(
'title' => 'Forward n to node',
'access arguments' => array('access content'),
'page callback' => 'littlurl_forward',
'type' => MENU_CALLBACK,
);
$items['u'] = array(
'title' => 'Forward u to user',
'access arguments' => array('access user profiles'),
'page callback' => 'littlurl_forward',
'type' => MENU_CALLBACK,
);
$items['t'] = array(
'title' => 'Forward t to taxonomy/term',
'access arguments' => array('access content'),
'page callback' => 'littlurl_forward',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Page callback to forward people along their merry way.
*/
function littlurl_forward() {
// Get the elements of the URL.
$args = explode('/', $_GET['q']);
// Get the elements of the query string.
$query_string = $_GET;
unset($query_string['q']);
if (count($args) > 1) {
switch ($args[0]) {
case 'u':
$args[0] = 'user';
break;
case 't':
$args[0] = 'taxonomy/term';
break;
case 'n':
default:
$args[0] = 'node';
break;
}
// It's easier to use url than deal with drupal_goto's arguments.
// @FIXME
// url() expects a route name or an external URI.
// $destination_url = url(implode('/', $args), array('query' => $query_string, 'absolute' => TRUE));
drupal_goto($destination_url);
}
else {
// This should never happen.
drupal_goto('<front>');
}
}
This is the Controller.php file. It has the same code the .module file has. Should I remove the redundant code snipped from the .module file?
/**
* @file
* Contains \Drupal\littlurl\Controller\DefaultController.
*/
namespace Drupal\littlurl\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
* Default controller for the littlurl module.
*/
class DefaultController extends ControllerBase {
public function littlurl_forward() {
// Get the elements of the URL.
$args = explode('/', $_GET['q']);
// Get the elements of the query string.
$query_string = $_GET;
unset($query_string['q']);
if (count($args) > 1) {
switch ($args[0]) {
case 'u':
$args[0] = 'user';
break;
case 't':
$args[0] = 'taxonomy/term';
break;
case 'n':
default:
$args[0] = 'node';
break;
}
// It's easier to use url than deal with drupal_goto's arguments.
// @FIXME
// url() expects a route name or an external URI.
// $destination_url = url(implode('/', $args), array('query' => $query_string, 'absolute' => TRUE));
drupal_goto($destination_url);
}
else {
// This should never happen.
drupal_goto('<front>');
}
}
}
This is a routing.yml file. It seems to be linking to the controller.
littlurl.forward:
path: /t
defaults:
_title: 'Forward t to taxonomy/term'
_controller: '\Drupal\littlurl\Controller\DefaultController::littlurl_forward'
requirements:
_permission: 'access content'