future proofing your drupal skills

42
Future Proofing your Drupal Skills Piyuesh kumar QED42

Upload: piyuesh23

Post on 10-May-2015

162 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Future Proofing Your Drupal Skills

Future Proofing your Drupal Skills

Piyuesh kumarQED42

Page 2: Future Proofing Your Drupal Skills

• TerminologiesModule InfoRoutes {Adding new menu / Access Control / Local Tasks / Upcasting.}Forms {Writing new custom form / Writing system settings form.}Configuration System.More Changes for developers.Drupal 8 Demo module showing all of the above

Topics

2

Page 3: Future Proofing Your Drupal Skills

• Dependency InjectionService Container / Dependency Injection ContainerRoutingControllersConfigFactory

Terminologies

3

Page 4: Future Proofing Your Drupal Skills

Dependency Injection is a 25 $ term for a 5 ¢

concept

4

Page 5: Future Proofing Your Drupal Skills

Lets take a look at some Wrong code to understand this.

5

Page 6: Future Proofing Your Drupal Skills

OBJECT ORIENTED WRONG CODE

6

Page 7: Future Proofing Your Drupal Skills

INJECTING ITEMS REQUIRED BY THE CLASS

7

Page 8: Future Proofing Your Drupal Skills

THE LESS YOUR CODE KNOWS ABOUT THINGS

IT NEED THE MORE REUSABLE IT IS

8

Page 9: Future Proofing Your Drupal Skills

ALL OUR NOTIFIER CLASS NEEDS TO KNOW IS IT NEEDS SOMETHING ON WHICH IT HAS TO CALL THE SEND METHOD.

9

Page 10: Future Proofing Your Drupal Skills

Learn More about DI

DrupalCon Portland (Kat Bailey)

https://www.youtube.com/watch?v=kocJ6pn9kEc

10

Page 11: Future Proofing Your Drupal Skills

Service ContainerA Service Container (or dependency injection container) is simply a PHP object that manages the instantiation of services (i.e. objects)

{d8_demo.services.yml}

11

Page 12: Future Proofing Your Drupal Skills

12

Page 13: Future Proofing Your Drupal Skills

13

Things are about to get geeky!

Page 14: Future Proofing Your Drupal Skills

Module Info file

14

Page 15: Future Proofing Your Drupal Skills

Drupal 7

{d7.info}

15

Page 16: Future Proofing Your Drupal Skills

Drupal 8

{d8.info.yml}

16

Page 17: Future Proofing Your Drupal Skills

Routing

17

hook_menu replaced by hook_default_menu_links()(https://drupal.org/node/2047633 )

Page 18: Future Proofing Your Drupal Skills

Routes

{d7.module}

18

Page 19: Future Proofing Your Drupal Skills

Routes{d8_demo.module}

19

{d8_demo.routing.yml}

Page 20: Future Proofing Your Drupal Skills

Controllers•Page callbacks => Controllers

•namespace Drupal/lib/<module_name/Controller/<Controller classname.php

20

What? Why so long?

Page 21: Future Proofing Your Drupal Skills

Namespacing & Folder Structure

Name Spacing• Keeps it reusable in other project with risk factor for conflict = 0. Drupal/<module_name>.

• Drupal/<module_name>/Controller to keep all the Controllers related to <module_name> grouped in the same place.

Folder Structure• lib/Drupal/<module_name>/Controller/<classname.php

• lib directory is used to keep all the php classes seperate from other module/yml files.

• 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.

21

Page 22: Future Proofing Your Drupal Skills

Drupal 8 Controller

{d8DemoController.php}

22

Page 23: Future Proofing Your Drupal Skills

Route Access• Controlled via yml file iteslf.• _permission• _<access callback>• _options: _access_mode (ALL/ANY)

23

Page 24: Future Proofing Your Drupal Skills

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.

24

Page 25: Future Proofing Your Drupal Skills

d8DemoAccessCheck

Extends

AccessInterface

25

Page 26: Future Proofing Your Drupal Skills

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)

26

Page 27: Future Proofing Your Drupal Skills

Menu Local Tasks(Drupal 7){d7_demo.module}

27

Page 28: Future Proofing Your Drupal Skills

Menu Local tasks(Drupal 8)

{d8_demo.local_tasks.yml}

28

Page 29: Future Proofing Your Drupal Skills

Upcasting{d7_demo.module}

Drupal 8

29

Page 30: Future Proofing Your Drupal Skills

Drupal 8 ParamConverter

30

{d8DemoController.php}

{d8_demo.routing.yml}

Page 31: Future Proofing Your Drupal Skills

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

31

Page 32: Future Proofing Your Drupal Skills

FormBase/ConfigFormBase

Example

32

Page 33: Future Proofing Your Drupal Skills

Forms Helper Methods

33

Page 34: Future Proofing Your Drupal Skills

Configuration Management

34

variable_get()

variable_del()

variable_set()

Page 35: Future Proofing Your Drupal Skills

How do i save my configurations now?• Drupal\Core\Config\ConfigFactory

35

• Where does this data get saved now?

Page 36: Future Proofing Your Drupal Skills

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.36

Page 37: Future Proofing Your Drupal Skills

Block as Plugins

37

Page 38: Future Proofing Your Drupal Skills

Module/hook system functions replaced with module handler service(https://drupal.org/node/1894902

)38

Page 39: Future Proofing Your Drupal Skills

39

Page 40: Future Proofing Your Drupal Skills

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)

40

Page 41: Future Proofing Your Drupal Skills

ReferencesDemo code is available at

https://github.com/piyuesh23/Drupal8-demo

• https://drupal.org/list-changes

• http://previousnext.com.au/blog/controlling-access-drupal-8-routes-access-checks

• https://drupal.org/node/1800686

• DrupalCon Portland Videos (http://www.youtube.com/playlist?list=PLpeDXSh4nHjRlZKs7cj2L_kLI5osP5ERc)

41

Page 42: Future Proofing Your Drupal Skills

THANK YOU!!

42