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
![Page 1: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/1.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/2.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/3.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/4.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/5.jpg)
Where we beganSteep Curve But Doable
![Page 6: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/6.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/7.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/8.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/9.jpg)
OO: ProgrammingThe Drop Off
![Page 10: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/10.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/11.jpg)
OO Rabbit Hole
![Page 12: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/12.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/13.jpg)
13
Let’s see scary Drupal 8 stuff
![Page 14: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/14.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/15.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/16.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/17.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/18.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/19.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/20.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/21.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/22.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/23.jpg)
23
Drupal 8Stay on the path
• Lets visit our pages!
![Page 24: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/24.jpg)
24
I see the pages…and the menus?
![Page 25: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/25.jpg)
25
Drupal 8More yaml
• Create page_example.links.menu.yml
![Page 26: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/26.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/27.jpg)
27
How about a form?
![Page 28: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/28.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/29.jpg)
29
Methods for our formForm Id and Build Form
![Page 30: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/30.jpg)
30
Methods for our formValidate and Submit
![Page 31: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/31.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/32.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/33.jpg)
33
Just the form pleaseNot all forms are pages.
![Page 34: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/34.jpg)
34
Lets look at these “worms”
![Page 35: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/35.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/36.jpg)
36
You said OO is good? Tell me more
![Page 37: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/37.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/38.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/39.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/40.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/41.jpg)
41
QuestionsGo.
?
![Page 42: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/42.jpg)
42
ThanksGo.
:)
![Page 43: OO: Geting the Uh-oh’s out of Object Oriented Drupal 8](https://reader036.vdocument.in/reader036/viewer/2022070616/5a4d1bec7f8b9ab0599e4961/html5/thumbnails/43.jpg)
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