oo: geting the uh-oh’s out of object oriented drupal 8
DESCRIPTION
3 HelloKen extends Hello My Name is Ken Gilbert Achieve Internet +2 Years Drupalist Outlander Native Coachella Valley-ian (Palm Springs) New Father HusbandTRANSCRIPT
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
3
HelloKen extends HelloMy Name is Ken Gilbert
• Achieve Internet +2 Years• Drupalist• Outlander
• Native Coachella Valley-ian• (Palm Springs)
• New Father Husband
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
Where we beganSteep Curve But Doable
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
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!
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
OO: ProgrammingThe Drop Off
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.
OO Rabbit Hole
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/
13
Let’s see scary Drupal 8 stuff
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
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.
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.
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
18
Drupal 8Stay on the path
• Create PageExampleController in the ‘src/Controller’ directory.
19
Drupal 8Stay on the path
• Add our “methods”. Which are just functions for our class.
20
Drupal 8Stay on the path
• Add our “methods”. Just functions for our class.
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
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.
23
Drupal 8Stay on the path
• Lets visit our pages!
24
I see the pages…and the menus?
25
Drupal 8More yaml
• Create page_example.links.menu.yml
26
Drupal 8Menus yey!
• Lets look at those menu items on the admin screen.
27
How about a form?
28
Drupal 8Stay on the path
• New file PageExampleForm.php in page_example/src/Form
29
Methods for our formForm Id and Build Form
30
Methods for our formValidate and Submit
31
Lets look at our pageBut first we need a route...oy
32
Now can we see it?Yes…. Now you can.
Lets go look at our page!
33
Just the form pleaseNot all forms are pages.
34
Lets look at these “worms”
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!
36
You said OO is good? Tell me more
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.
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.
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.
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
41
QuestionsGo.
?
42
ThanksGo.
:)
43
ReferencesGo.
• http://www.oodesign.com/• https://en.wikipedia.org/wiki/PHPDoc • https://www.drupal.org/developing/api/8/render/arrays • https://docs.acquia.com/articles/building-drupal-8-modules• http://php.net/manual/en/language.oop5.magic.php