Drupal 7: example.info
name = Example
description = An example module.
core = 7.x
files[] = example.test
dependencies[] = user
Drupal 8: example.info.yml
name: Example
description: An example module.
core: 8.x
dependencies: - user
# Note: New property required as of Drupal 8!
type: module
Creating MENU
Drupal 7: hook_menu()
example.module
<?php
/**
* Implements hook_menu().
*/
function example_menu() {
$items['hello'] = array(
'title' => 'Hello world',
'page callback' => '_example_page',
'access callback' => 'user_access',
'access arguments' => 'access content',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Page callback: greets the user.
*/
function _example_page() {
return array('#markup’ => t('Hello world.'));
}
?>
Drupal 8: Routes + Controllers
example.routing.yml
/* note: modulename.filepurpose , modulename is required next purpose can be anything which is understandable */
example.controller:
path: '/hello' /* the path which will be used to display output */
defaults:
_content: '\Drupal\example\Controller\Hello::content' /* controller with class hello and method content */
requirements:
_permission: 'access content'
Note: In route file
src/Controller/Hello.php /* the file name has to be same as class name used */
<?php
namespace Drupal\example\Controller; /* same as defined in yml _content */
use Drupal\Core\Controller\ControllerBase; /* required to use controller */
/
**
* Greets the user.
*/
class Hello extends ControllerBase {
public function content() { /* function name defined in yml */
return array('#markup' => $this->t('Hello world.'));
}
}
?>
Creating MENU with argument in url
route yml
modulename.controller_argument:
path: '/example/{nodeID}'
defaults:
_controller: 'Drupal\modulename\Controller\Hello::getargument'
requirements:
_permission: 'access content'
<?php
namespace Drupal\example\Controller; /* same as defined in yml _content */
use Drupal\Core\Controller\ControllerBase; /* required to use controller */
/
**
* Greets the user.
*/
class Hello extends ControllerBase {
?>
namespace Drupal\example\Controller; /* same as defined in yml _content */
use Drupal\Core\Controller\ControllerBase; /* required to use controller */
/
**
* Greets the user.
*/
class Hello extends ControllerBase {
public function getargument($nodeID) {
return array(
'#markup' => $nodeID,
);
}
}?>
No comments:
Post a Comment