Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "OO: Geting the Uh-oh’s out of Object Oriented Drupal 8."— Presentation transcript:

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

2 2 Hello My Name is Dagobeto Aceves Achieve Internet +5 Years Drupal Developer Native San Diegan New Father

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

4 4 Agenda My 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

5 Where we began Steep Curve But Doable

6 6 Drupal of Old Global 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 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 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

9 OO: Programming The Drop Off

10 10 10,000 ft view These 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.

11 OO Rabbit Hole

12 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 13 Let’s see scary Drupal 8 stuff

14 14 Drupal OO Stay 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 15 Drupal 8 Stay 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 16 Drupal 8 Stay 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 17 Drupal 8 Stay 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/2092643https://www.drupal.org/node/2092643

18 18 Drupal 8 Stay on the path Create PageExampleController in the ‘src/Controller’ directory.

19 19 Drupal 8 Stay on the path Add our “methods”. Which are just functions for our class.

20 20 Drupal 8 Stay on the path Add our “methods”. Just functions for our class.

21 21 PHP Magic – Becoming an OO Master Hidden 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 22 Drupal 8 Stay on the path Add our “methods”. Just functions for our class. https://www.drupal.org/developing/api/8/render/arrays More on render arrays.https://www.drupal.org/developing/api/8/render/arrays

23 23 Drupal 8 Stay on the path Lets visit our pages!

24 24 I see the pages…and the menus?

25 25 Drupal 8 More yaml Create page_example.links.menu.yml

26 26 Drupal 8 Menus yey! Lets look at those menu items on the admin screen.

27 27 How about a form?

28 28 Drupal 8 Stay on the path New file PageExampleForm.php in page_example/src/Form

29 29 Methods for our form Form Id and Build Form

30 30 Methods for our form Validate and Submit

31 31 Lets look at our page But first we need a route...oy

32 32 Now can we see it? Yes…. Now you can. Lets go look at our page!

33 33 Just the form please Not all forms are pages.

34 34 Lets look at these “worms”

35 35 Lets take closer look Woms 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 36 You said OO is good? Tell me more

37 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 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 39 Improved Support Drupal Console generate 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 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 41 Questions Go. ?

42 42 Thanks Go. :)

43 43 References Go. 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


Download ppt "OO: Geting the Uh-oh’s out of Object Oriented Drupal 8."

Similar presentations


Ads by Google