It’s time to get start with Drupal 8! The biggest change in this major update is that Drupal 8 is now with the Symfony PHP framework. So Drupal developer could work with those components such as ClassLoader, Routing and DependencyInjection which are provided by the Symfony framework.
Ok! Stop talking and start coding~ =D
The following example is a simplified version from the screencast recorded by Joe Shindelar @ Drupalize.Me.
1. Drupal 8 now has a new folders and files hierarchy. Now all core files are inside the core folder while the contributed/custom modules and themes folders are now located at the Drupal webroot.
2. Inside the modules folder, create the custom folder where we store all the custom modules. Also a contrib folder for storing other contributed modules which are available online.
3. Create our first Drupal 8 module folder and name it hello_world inside modules/custom.
4. Starting from Drupal 8, we could use YAML for the .info file instead of the traditional .ini format. So inside the modules/custom/hello_world folder, create hello_world.info.yml as follow.
hello_world.info.yml
name: Hello World description: A simple hello world module. core: 8.x package: Eureka type: module dependencies: - node - block
5. Create the modules/custom/hello_world/hello_world.module as well to implement the hook_menu(). You could compare the deprecated and the new implementations. Some of the parameters are now handled in the module controller and the routing.yml.
hello_world.module
<?php /** * @file * A simple hello world module. */ /** * Deprecated hook_menu() implementation. * It is still usable in Drupal 8. */ /* function hello_world_menu() { return array( 'hello' => array( 'title' => 'Hello World', 'page callback' => 'hello_world_page', 'access callback' => 'user_access', 'access arguments' => array('view content'), 'file' => 'hello.pages.inc', ), ); } */ /** * Implementation of hook_menu(). */ function hello_world_menu() { return array( 'hello' => array( 'title' => 'Hello World', 'route_name' => 'hello_world_page', ), ); }
6. In the past, we may have different .inc files for different functions. Now, with Symfony, we adopt the Object Oriented Programming practice and we should recognize those .inc files into the module Controller Class. In addition, in order to auto load it, we have to follow the PSR-0 name spacing for the class path. So we create modules/custom/hello_world/lib/Drupal/hello_world/Controller/HelloWorldController.php.
HelloWorldController.php
<?php namespace Drupal\hello_world\Controller; use Drupal\Core\Controller\ControllerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; Class HelloWorldController implements ControllerInterface { // Implementation of ControllerInterface::create method. public static function create(ContainerInterface $container) { return new static($container->get('module_handler')); } // Hello World page callback. public function helloWorldPage() { $build = array( '#type' => 'markup', '#markup' => t('Hello World!'), ); return $build; } }
6. Finally, we need to create the modules/custom/hello_world/hello_world.routing.yml for mapping the route_name to the page callback.
hello_world.routing.yml
hello_world_page: pattern: 'hello-world' defaults: _content: 'Drupal\hello_world\Controller\HelloWorldController::helloWorldPage' requirements: _permission: 'view content'
8. Check if your Hello World page work by visiting /hello-world.
Done =)
Reference:
You lost my interest when you started using YML config files! 😛
LikeLike
Why not? haha~ what’s bothering you using YML?
LikeLike
The additional complexity caused by it. Especially if not all modules follow the YML format, and some choose to use the PHP, XML, or INI format. Let’s keep it simple.
LikeLike
i think it just need some times to move the standard from .ini to .yml in this transition period. in the future, i guess only yml is allowed.
LikeLike