43
OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

Embed Size (px)

DESCRIPTION

3 HelloKen extends Hello My Name is Ken Gilbert Achieve Internet +2 Years Drupalist Outlander Native Coachella Valley-ian (Palm Springs) New Father Husband

Citation preview

Page 1: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

Page 2: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

2

HelloMy Name is Dagobeto Aceves

• Achieve Internet +5 Years• Drupal Developer• Native San Diegan• New Father

Page 3: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

3

HelloKen extends HelloMy Name is Ken Gilbert

• Achieve Internet +2 Years• Drupalist• Outlander

• Native Coachella Valley-ian• (Palm Springs)

• New Father Husband

Page 4: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

4

AgendaMy Name is Dagobeto Aceves

• Drupal and how it got to OO• Glance OO Concepts• OO Drupal

• A look at a Drupal OO example - building a simple page callback and a simple form.

• A line by line analysis of code.• Associating example code with OO concepts.• A line by line analysis of a form callback.

• Scaffolding with Console• Q&A

Page 5: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

Where we beganSteep Curve But Doable

Page 6: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

6

Drupal of OldGlobal global everwhere

• Wild West of Code• Global Variables

• $user• $base_path• More

• https://api.drupal.org/api/drupal/globals• Global Functions

• l()• t()• More

• https://api.drupal.org/api/drupal/7

Page 7: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

7

Every Can Drupal Easy to use

• Positives:• Global variables can be used everywhere• Global functions can be executed anywhere• Module functions can be executed everywhere• Everyone can Drupal!

Page 8: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

8

Every Can Drupal Easy to use

• Negatives:• Global variables can be used everywhere• Global functions can be executed anywhere• Module functions can be executed everywhere• Requires more User memory

Page 9: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

OO: ProgrammingThe Drop Off

Page 10: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

10

10,000 ft viewThese topics are lager than the appear.

• New and Exciting Symfony Framework• New backend handling incoming requests

• PSR-4• PHP Standard Recommendation• Specification for autoloading classes from file

paths• Object Oriented Programming

• The rabbit hole.• Decoupled and Reusable Components

• Parts of the code that handle data don’t deal with how its displayed and vice-versa.

Page 11: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

OO Rabbit Hole

Page 12: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

12

Object Disoriented$OO->listAllTopics()

• Data Abstraction • Represent features without including details

• Data Encapsulation• Wrapping data and associated function in one

single unit.• Inheritance

• Capability of one class to inherit properties from other class

• Objects and Classes• Templates for creating Objects.

• OO Design Patterns• Creation patterns

• Singleton• Factory• Etc…

• Behavioral Design Patterns• Command• Observer• Etc…

• So many more… http://www.oodesign.com/

Page 13: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

13

Let’s see scary Drupal 8 stuff

Page 14: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

14

Drupal OOStay on the path

• Creating a simple page callback• Drupal 7 you needed:

• Create a folder “page_example” in {DOCROOT}/sites/all/modules/custom

• *.module• *.info• Implement hook_menu• Define the page callback function and maybe

pass in some arguments to your callback

Page 15: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

15

Drupal 8Stay on the path

• Creating a simple page callback• Create a folder “page_example” in

{DOCROOT}/modules/custom• *.info.yml• *.routing.yml• PageExampleController.php

• YML (rhymes with camel) is not OO PHP. Nifty human-readable data serialization language.• YML content easily maps high-level language

data types such as lists, associative arrays.

Page 16: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

16

Drupal 8Stay on the path

• Create a ‘page_example’ directory• Create a ‘page_example.info.yml’ file with this

content.

• You now have a module you can enable. Really! That’s all you need.

• Lets enable it. It doesn’t do anything but lets get to that next.

Page 17: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

17

Drupal 8Stay on the path

• Create a ‘page_example.routing.yml’ file with this content.

• Replace hook_menu with a serialized array. Can you spot the path? The page_callback? The Access Permissions?

• See more on routing https://www.drupal.org/node/2092643

Page 18: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

18

Drupal 8Stay on the path

• Create PageExampleController in the ‘src/Controller’ directory.

Page 19: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

19

Drupal 8Stay on the path

• Add our “methods”. Which are just functions for our class.

Page 20: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

20

Drupal 8Stay on the path

• Add our “methods”. Just functions for our class.

Page 21: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

21

PHP Magic – Becoming an OO MasterHidden Give Me Methods

• __construct()• When a class object is created, do some setup

first.• __destruct()

• When a class object is destroyed, do some cleanup first.

• __toString()• When a class object is printed as a string, print

this instead of showing me a nasty PHP error.• And many more

• http://php.net/manual/en/language.oop5.magic.php

Page 22: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

22

Drupal 8Stay on the path

• Add our “methods”. Just functions for our class.

• https://www.drupal.org/developing/api/8/render/arrays More on render arrays.

Page 23: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

23

Drupal 8Stay on the path

• Lets visit our pages!

Page 24: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

24

I see the pages…and the menus?

Page 25: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

25

Drupal 8More yaml

• Create page_example.links.menu.yml

Page 26: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

26

Drupal 8Menus yey!

• Lets look at those menu items on the admin screen.

Page 27: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

27

How about a form?

Page 28: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

28

Drupal 8Stay on the path

• New file PageExampleForm.php in page_example/src/Form

Page 29: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

29

Methods for our formForm Id and Build Form

Page 30: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

30

Methods for our formValidate and Submit

Page 31: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

31

Lets look at our pageBut first we need a route...oy

Page 32: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

32

Now can we see it?Yes…. Now you can.

Lets go look at our page!

Page 33: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

33

Just the form pleaseNot all forms are pages.

Page 34: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

34

Lets look at these “worms”

Page 35: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

35

Lets take closer lookWoms man... Worms!

• Of the code written you can ONLY delete the function validateForm. Everything else is necessary..

• Forgot to type in:• Array? – it breaks• FormStateInterface? – bork• One of the ‘use’s? – bork bork bork

• The errors will be cryptic until you learn more OO but the gist of all the errors are either:

• You’re missing something that you told me to use.

• You’re missing something that what you’ll telling me to use requires.

• Lets go break the code to see what I mean!

Page 36: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

36

You said OO is good? Tell me more

Page 37: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

37

Lets take closer look again• Type Declarations• Including the FormBase makes sense since we are

inheriting from that class.• Which variables and methods can you use?• http://php.net/manual/en/

language.oop5.visibility.php• FormStateInterface seems like something you just

should have known... But really how would you have? You got me there. But there’s ways around remembering.

Page 38: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

38

Improved Support

• Drupal Console• Patterns can be automated so you don’t have to

remember what to include, let the machines remember for you!

• Improved Performance• Things are there only when you need them.

• Standard Code Structure• Forms go in forms• Controllers in controllers• Blocks are PlugIn we didn’t talk about this one

but it would be the next step.• IDE Candy

• Improved support form IDEs because we’re following a standard now. No more wild west.

Page 39: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

39

Improved Support

• Drupal Consolegenerate generate:authentication:provider Generate an Authentication Provider generate:command Generate commands for the console. generate:controller Generate & Register a controller generate:doc:dash Generate the DrupalConsole.docset package for Dash generate:doc:gitbook Generate documentations for Commands generate:entity:bundle Generate a new content type (node / entity bundle) generate:entity:config Generate a new config entity generate:entity:content Generate a new content entity generate:event:subscriber Generate an event subscriber generate:form Generate a new "FormBase" generate:form:alter Generate an implementation of hook_form_alter() or hook_form_FORM_ID_alter generate:form:config Generate a new "ConfigFormBase" generate:module Generate a module. generate:permissions Generate module permissions generate:plugin:block Generate a plugin block generate:plugin:condition Generate a plugin condition. generate:plugin:rulesaction Generate a plugin rule action generate:plugin:type:annotation Generate a plugin type with annotation discovery generate:plugin:type:yaml Generate a plugin type with Yaml discovery generate:plugin:views:field Generate a custom plugin view field. generate:service Generate service generate:theme Generate a theme.

Page 40: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

40

What did we see

• Example Controllers using basic Class• Creating and using namespaces• Scope Resolution Operator ‘::’ for calling methods

from classes• Basic Inheritence using Extend• Basic Concept of an Interfaces and implementing• Type Declarations• Automated support for developing

Page 41: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

41

QuestionsGo.

?

Page 42: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8

42

ThanksGo.

:)