Future Proofing Your Drupal Skills
Topics
- Terminologies
-
Module Info
- Adding new menu.
- Access Control.
- Local Tasks.
- Upcasting.
- Writing new custom form.
- Writing system settings form.
Drupal 8 Demo module showing all of the above
Terminologies
- Dependency Injection
- Service Container / Dependency Injection Container
- Routing
- Controllers
- ConfigFactory
Dependency Injection is a 25 $ term for a 5 ¢ concept
Lets take a look at some Wrong code to understand this.
OBJECT ORIENTED
INJECTING THE THINGS REQUIRED BY THE CLASS
THE LESS YOUR CODE KNOWS THE MORE REUSABLE IT IS
ALL OUR NOTIFIER CLASS NEEDS TO KNOW IS IT NEEDS SOMETHING ON WHICH IT HAS TO CALL THE SEND METHOD.
Learn More about DI
DrupalCon Portland (Kat Bailey)
Service Container
{_tags, arguments, class}
Examples: {Logger, Mailer, Url Generator, Cache backend}
A Service Container (or dependency injection container) is simply a PHP object that manages the instantiation of services (i.e. objects)
Things are about to get geeky!
Module Info file
Drupal 7
{d7.info}
Drupal 8
{d8.info.yml}
Routing
- hook_default_menu_links() might replace hook_menu(https://drupal.org/node/2047633 )[WIP]
- Routes
- d8.routing.yml
- path to controller/form/...mappings
- Access Rules
- Upcasting
- Local tasks
Routes
{d7.module}
Routes
{d8_demo.module}
{d8_demo.routing.yml}
What? Why so long?
- Page callbacks => Controllers
- namespace Drupal/lib/<module_name/Controller/<Controller classname.php
Controllers
Namespacing & Folder Structure
- lib/Drupal/<module_name>/Controller/<classname.php
- The Drupal/hello part of the directory path is needed to comply with PSR-0, a standard that says that the complete class name must be represented on the file system.
Drupal/<module_name>/Controller to keep all the Controllers related to <module_name> grouped in the same place.
- lib directory is used to keep all the php classes seperate from other module/yml files.
Folder Structure
Namespacing
- Keeps it reusable in other project with risk factor for conflict = 0. Drupal/<module_name>.
Drupal 8 Controller
{d8DemoController.php}
Route Access
- Controlled via yml file iteslf.
- _permission
- _<access callback>
- _options: _access_mode (ALL/ANY)
Defining _access_check_admin
- Register a new service using d8_demo.services.yml
- tagged with access_check so Drupal can find it when it loads all access checks
- Convention to create a unique service name starting with access_check.
d8DemoAccessCheck
Extends
AccessCheckInterface
Reusable Requirements
- Allow everyone
_access: 'TRUE'
- Check if user is logged in
_user_is_logged_in: 'TRUE'
- Check if a user has a permission
_permission: 'my permission name'
- Check if a user has a role
_role: 'role1+role2' (AND)
_role: 'role1, role2' (OR)
Menu Local Tasks(Drupal 7)
{d7_demo.module}
Menu Local tasks(Drupal 8)
{d8_demo.local_tasks.yml}
Upcasting
Drupal 8
$items['node/%nid']
{d7_demo.module}
- Convert {param} into anything
- Entities are converted by default. To convert anything else, use ParamConverterManager Class
Drupal 8 ParamConverter
{d8DemoController.php}
{d8_demo.routing.yml}
Forms
- Form Api stays the same in Drupal 8. However, forms are now Classes coz of d8's Object-oriented nature.
- Base Classes in core:
- FormBase
- ConfigFormBase
- ConfirmFormBase
- BulkFormBase
- ViewsFormBase
- FieldInstanceFormBase
FormBase Example
ConfigFormBase Example
Forms Helper Methods
Configuration Management
variable_set()
variable_get()
How do i save my configurations now?
Where does this data get saved now?
- Drupal\Core\Config\ConfigFactory
Plugin System
- Info hooks => Plugins
- hook_info => annotation based discovery
- Why annotation based?
- Uses php's tokenizer which allows the files to be parsed as text.
- Memory used is released once the file is read.
- Loading the file in as php would mean that allocated memory was required until the request was finalised.
- All info hooks as plugins.
Blocks as Plugins
Module/hook system functions replaced with module handler service(https://drupal.org/node/1894902)
Changes to commonly used Drupal functions/hooks
- hook_init removed(https://drupal.org/node/2013014)
- drupal_goto has been removed(https://drupal.org/node/2023537)e.g., drupal_goto($url)=>new RedirectResponse($url);
- hook_boot has been removed(https://drupal.org/node/1909596)
- $_GET[‘q'] has been removed(https://drupal.org/node/1659562)
- drupal_*_form functions replaced with formBuilder service. e.g., drupal_get_from => \Drupal::formBuilder()->getForm()
- and many more...(https://drupal.org/list-changes)
References
Demo code is available at https://github.com/piyuesh23/Drupal8-demo
Thank You
Drupal 8
By Rahul Shinde
Drupal 8
- 653