 My name is Raul. So I'm going to discuss about Drupal 8 module development. So, yeah, I'm an open source advocate from Philippines. And I'm doing Drupal since 2013. And active volunteer in Drupal, Filipinas. So, yeah, I'm a customer success engineer in Pansyan. So, before I begin working on them, I was a backend Drupal developer. And, yeah. So, may topic for today is about Drupal 8 coding standards. So, in Drupal 8, some are added new coding standards are added. As we know that Drupal is using Simpuni. And it's already object oriented programming. So, Drupal 8 file structure. What are the new structure of Drupal 8? So, yeah. And then Drupal 8 API overview. So I'm going to just pick some of the most used API that I'm going to use later on creating our first module. Then PHP namespace. So, if you're not familiar yet in PHP namespace, I'm going to discuss what's namespace in Drupal 8. And then routes and controllers. So, routes and controllers also new in Drupal 8. So, since it's using Simpuni. Then the last is creating a custom block and form. So, we will create a custom block and form. So, Drupal 8 coding standard. So, just an overview for Drupal 8. So, I just said it earlier. Object oriented programming. And then it uses Simpuni. And we're going to discuss how did clear classes. And then namespaces. Identing at whitespaces. And naming convention, visibility, type hinting, and chaining. So, there's a lot of new in Drupal 8. So, I'm just pick some of the topics that I'm going to discuss later. So, object oriented programming. As you know that Drupal 8 is already object oriented programming. It also uses an MVC pattern. So, a model view controller. So, at Drupal 8 most use now objects and classes. There's a buzzword on that Drupal 8. And, yeah. It's not like at Drupal 74 that's more on procedural programming language. So, in Drupal 8, it's more on focus on object oriented programming. So, more on objects and classes. So, yeah. Drupal 8 also uses Simpuni. So, Simpuni is a set of decoupled and reusable components on which the best PHP applications are built. Such as Drupal, PHP, VVB, Easy Publish, and also, I know that vulnerable also uses Simpuni. So, declaring classes. Best practices include having one class or interface or trade per file. So, it means that when creating a class, best practices is just to include one class. So, that file should name for the class such that the file name or, for example, is who interface would be who interface. So, if you change that, if there's a different for the file name at the class name, that would not work. So, yeah. It's a best practice to name the file. File name as same as the class name. So, this is a new on Drupal 8. It's auto load classes. So, it use keyword use. So, I'm going to show it later. So, this is an example of a wrong declaration of class. So, here in this example, I add example block. So, I add S. So, in Drupal way, it should be same as the file name and the same as the class name. So, I forget to include that also that if you're declaring a class, it's a best practice to include only one class. So, for example, if you include the two class, that's not advisable. So, here's the example of namespace. So, in namespace, we like to create some alias on that folder. So, for example, Drupal slash example slash plugin slash block. The example is the module and then the plugin is another sub directory and the block is also a folder. So, we auto load the class block base. So, block base is a core and Drupal 8. It's a core class that you can extend for you to be able to use the methods of that class. So, in this example, in best practice in coding standard, we always put a next line before the method or, for example, it's used to make the code more readable. So, leave an empty line between start of class interface definition and property method definition. Next is, same here. So, we just leave space. We add the new line. So, this is same as also as in Drupal 7. So, you see that there's some of coding standards that are still applicable on Drupal 8. So, for example, leave an empty line between an end of property definition and start method. So, see the new line there. So, naming convention. Classes and interface should use upper camel naming. It means that the first letter should be capital. Then the next word should be also capital. So, as the upper camel, see that the first letter is capital U and then the second word has capital C. Then methods and class property should use lower camel naming in Drupal 8. Properties of configuration and it is our except of this convention. Those properties are allowed to use underscores. So, for example, you have a variable or methods that, for example, get name. So, the first letter of that method is small g. Then the second word is capital N. So, get name, something like that. So, if an acronym is used in class or method, they make it camel case 2. So, for example, so, for example, this one. So, in this example, on the right side, it shows that the classic examples extend black base. So, as you notice, the first letter of the word is in a small capital. It's in a small case. So, that's wrong. So, the correct way to declare a class is this one. In Drupal way, we capital the first word and the second word is also capital. So, visibility. All methods and properties of classes must specify their visibility, public, protected, or private. So, it is a PHP force style. War declaration must not be used. So, it means that for public means that the variable function scope availability and error on a class. So, that public visibility must be avoid. Then, on private, it means that the variable function scope only available on a class only. So, it means that the private methods or properties are not available on another class. And then, protected. This one means that the variable function scope visible in all classes that extend current class including the parent class. So, for example, you have variables or functions that's visible on class. And then you extend that class and then you can still use the variable function on that extended class. So, it's like you inherit the property or the variable function. Do you have any questions? So, please let me know if I talk fast. So, here's these examples. The wrong one is it says that avoid to use the var so, instead, use the public, protected or private. But it will depend on your use case on when you're going to use that visibility or it's like an access modifier or something. So, type hinting. So, PHP supports optional type specification for function and method parameters or the code type hinting. So, it does make type required as passing an object that does not confirm to the type will result in fatal error. So, it is a practice to specify the interface instead of the of the class. So, it's more easy for us to debug where's the errors coming from because if you use the class instead of the interface, it will be more hard for us to find where's the errors comes from. So, it's not easy to track down. So, this is an example of type hinting. So, as you can see how it calls or implements the function. On the parameter, you see that the Garfield, the cat is the class. And then on the drupal way, the cat can interface the other side dog. That's the interface. So, in parameter, it's a good practice to use interface instead of class. So, it's more easy for us to debug or on your code. So, chaining. I also saw this on drupal 7. So, I think it's in drupal 8. It's also still used. So, PHP allows objects returned from functions and methods to be called immediately. It's known as fluent interface. So, in unchain version, you'll see that master query stream. So, as you know this that the table is enclosed on braces and then you could also pass a parameter that's the node ID. And then you could also declare it or set it on a variable then call the fetch field function or object. The result's value, if you dump that it will return as an array object. Then in chained version as you know this is a one line code. So, this is almost the same. But in this code you did create a new variable and then call it. So, it's chained. So, dv query is a function then followed by the other function fetch field. So, as a general rule a method should return this and thus be chainable. In any case where there is no other logical return value common example are those methods that same set some state or property on the object. It's better in those cases to return this rather than through false or null. So, for example, instead of return a true value return this, dollar sign this. So, in the case where you have a fluid interface for class, the code spans more than one line. The method calls should be indented with two spaces. So, I have this example of fluid interface. So, you see that db select and other function condition and then execute. So, we're going to discuss about that Drupal 8 file structure. So, here's the new Drupal 8 structure. Core modules are moved inside core modules while contributed and custom module can be placed inside the root directory. So, as you notice that inside the core modules miscellaneous folder profiles or other or core modules are moved in the core while the modules are taken out on the root project. So, you could place your contributed and custom modules. So, you can still also place your costumer contributed modules inside the site's all modules but in practice you should place your contribute and custom modules inside the modules in root project directory. So, one new things that also added there is the vendor vendor directory that includes some components from symphony. So, in some cases and only if someone also encounter some issue with the for example, you updated some modules and you're not using a composer before and then you try to create a composer. When you apply that on that composer workflow or create a use composer will sometimes you'll counter some merge conflict. So, if you're going to create a project if you're going to stick using composer I suggest stick on using composer composer all the way because if you use manual installation or download the folder and then drop it on the modules folder in the next time you use composer. So, it will give conflict on your development. So, we are pushed to use composer so we're not going to more use on drash. So, in Drupal 8 we are forced to use composer. So, Drupal 8 APR overview. So, I just include some of the most used API in Drupal 8. So, some of the API there is you're going to be familiarize because you're also using it on Drupal 7 before. So, configuration API. So, the configuration API provides a central space for modules to store configuration data. This data can be simple configuration like your site name or more complex information managed with configuration entity such as views and content types. So, does anyone here familiar with the system configuration when you return perform? So, it is like a counterpart of it in Drupal 8. So, actually there are two types of configuration API. The one is configuration API and the other one is the configuration entities API. So, it use content types or entities. Key difference is that config API is a singleton use case. A singleton is where there can be only a single instance of this configuration. A good example will be the site name. Then, while the configuration entity API should be used to store multiple sets of configuration. So, for example, is node types, views, vocabularies and fields. Those are default in Drupal 8 as well in Drupal 7. So, why use configuration API? So, configuration is a place to store information that you would want to synchronize from development to production. So, this information is often created during site building and is not typically generated by regular users during normal site cooperation. They have any questions? So, next is the black API. So, blacks in the Drupal 8's made up of two separate two APIs or black plugin API which is standalone reusable API and then black entity API which is Drupal 8 specific use case of black placement and visibility control. So, as anyone here familiar with the bin module, it's also in entity black. So, in Drupal 8 it's already in the core. So, you have a option to create something similar like a bin bin black. So, you could also add more fields there. So, actually in Drupal 8 you don't need almost to code more. But all you need is already on a Drupal 8 but sometimes if you have some cases or use case that you need to have more complex modules. So, that's the time that you need to create your own custom module. So, if you are a Drupal 7 developer before and then you jump on Drupal 8. So, it's a kind of a big another learning curve that you need to learn object oriented programming and then MVC and last object some of the the new works for the Drupal 8. Now, back back on a block API. So, this is an example. So, can you see the code? So, here I created a class. Then I extend the block base Drupal core class. Then I use the namespace keyword to include the plugin blocks. So, the plugin blocks that I use is the that's the custom class that I created. So, use namespace to avoid code collision. So, for example, you have two two plugins that I use that same class. So, you could segregate the you could use the same class, but you need to have a different for example, sub-direct reasoning. So, from here what I did is I call the function build. So, the build method is come from a block base. So, you could use the method that comes from block base. When I extend that, then I call the Drupal current user. So, if you are familiar with the user load, something like that. So, if you are Drupal 7 developer and you see this line of code. So, you could find it weird and yeah. So, here you could see that you call that the backslash Drupal. And then if you call on, then you call the method. That's how you call the method. So, it's also chain method. So, as you see, you could see that there's a get display name. So, if you're going to just load this part and then if you dump the name, you could see a bunch of results there. So, it will return a lot of information. So, in this I just return the name of the current user and then, yeah. And then return the mark up. So, here's another new new keyword in Drupal 8. You will notice the lot of dollar sign this. And then, followed by the t function. So, it is also to translate. And then you could also add the parameter and the place folder. Just like the same in the Drupal 7. So, it's also written on Drupal 8. And then in the block, you'll see the hello admin. The user name is the add name. So, form API. In Drupal 7, you also use the form API. If you have some custom form and if you don't want to use web form or the default form in Drupal. So, you could also build your own form in Drupal 8. So, it's in the different in Drupal in creating a form. So, you need to implement or call the class form interface. So, that class name holds some of the methods that you need in building form. So, some of the methods that you can get on that interface, the build form and the validate form. And submit form. So, in Drupal 7.4, we use hooks a lot. So, in Drupal 8, we're using more on class and its methods. So, the submit forms returns when you call the submit forms, it will generate array that comes from the form build form. So, when a form is requested, it's defined as a renderable array of 10 referred to as a form API array or simply form. So, just like the same as when it returns a array of form. So, for example, for this one. I think it's just this one. So, for example, in the build form, as you notice, it's almost the same format on how you figure an array of fields. But, you will notice in Drupal 8, instead of the parenthesis, you will notice that it's become a square bracket. So, I wish I could assume that image. Later, I will show you that how it looks like, looks that the class. So, you could see it in more detail. So, here, you have field first name and then last name and then you save configuration. So, just like the same in Drupal 7 but different how you called it on Drupal 8. Then, routes and controller. Does anyone try to create or have been used the routes and controller before? So, yeah. So, this is the new way on how you could create your own menu. So, in before in Drupal 7, we have this hook menu and then we defined the URL, the name of the menu. Here, in the routes and controller in Drupal 8, it's more different. So, you have here the routing.yaml file. So, you define there the permission, access permission of the menu of the URL. So, I will show you that later. So, here you could have the request post and get and then it handles by HTTP kernels. So, HTTP kernels comes from on a symphony and then it will return a response JSON. And then you will get the response from the controller. So, the data comes from the controller. The controller generates the response. So, it's response when generating the content of the data. So, creating your first custom module. As you notice, there's a new extension on that info file. Before in Drupal 7, we only used example.info. In Drupal 8, we're now using the .yaml file. So, the text not only shows in modules it also visible on teams. So, you'll see a lot of .yaml file in teams also. So, in declaring a name, so, it's a key-end value here. So, just like the same as your declaring in Drupal 7. So, you have the name and then the name of your module. So, here in example is example module, then the type of module and then the description of the module or what your module does. And then you have to also declare what core that your module is using and then the package. So, in this example I use custom. So, all modules that has a package custom if you search it on admin modules, you will see that all that modules comes from the package custom. So, here in creating a Drupal 8 module what you only need is that .yaml file. So, you define the information of your modules and then it will instantly shows up on your admin modules page. So, create example black class. So, this is the one that I show earlier. So, here's the new structure of declaring block. So, you have some nested directory. So, SRC, plugin block and then your file, example block that's your class name also. So, that's one thing on Drupal 8 plugins. So, it's more organized on declaring your plugins or your modules than before. Excuse me. Is plugin only particular to block? Actually, you could have more plugins not just only the block. You could also have widgets, field widgets something for example before. You declare different way on Drupal 7 on creating widgets. So, plugins like blocks field widgets are types of plugins. So, you could see that instantly once you declare your block. So, it will show on the place block. It will show on your structure blocks. Here, you could also add your entity block instead of your custom. So, in my example, you will see your custom block shows up. So, once placed, it will show up on your home page where you set your block. So, for example, inside bar. It will show up there. So, here is creating custom form. You need to auto load the class config formbase and then form state interface. So, that's the class that you need when you're going to create custom form. So, for you to be able to use some of the methods of that class. So, from here we extend the config formbase. So, what we need to be creating a custom form is get editable config names and then get form ID, then build form, then submit form. So, here in implementing get editable config names. So, we get the configuration name. Then, that starts the config form settings. If you're familiar with the variable stables. So, you will find it similar to that in Rufa 7 config. So, it's similar to system config return system config metal. So, implement get form ID. So, here you will need to create or return the unique form ID. So, in my example, I return contact form. So, this must be unique. So, here. So, in build form here you will declare a refill. So, you will see here we initialize the config this config that holds the settings of your form. So, it holds the variables similar to variable stables. So, you will we call the config our sign yet. So, that pulls the default value of the field. If you don't have a value set there, it will just show as empty or you could also set a place holder. Then, you return the build form. So, here you call the parent and then the method build form. And then, you need to also create the function submit form. In this function you call and set the value of the of the form field. So, for example, here we pull the value of form state object and then we call the get value. And then we call the first name that's the that's also a story in the admin settings. So, basically if you're familiar with the variable underscore get and the variable underscore set. So, this is the equivalent in Rupal 8. And then you call, you change it on the save method. Yeah, I think that's it. I should also include the database. But I don't have time to include it. So, yeah, I would love to include it, but it's quite busy last week. So, I think I'll share it next time probably or I'll push it on my github account and then I'll share it on our Facebook group sometime later. So, do you have any questions about creating Rupal 8 custom modules? I know it's for the new in Rupal 8. It's quite difficult to grasp how you create a Rupal 8 custom modules because you need to learn OOP object oriented programming. You need to know the how it works and then object and then routing and controller and then there's also factory patterns that I also read while creating that slide. So, yeah, it's a quite a long long journey in learning Rupal 8. So, yeah, I think I'm still learning on Rupal 8 a lot. We have any questions? I think we're good now. So, thank you for listening.