DrupalCon Prague 2013

Cómo funcionan los plugins de Drupal 8 y cómo convertir los hooks de Drupal 7

Peter Wolanin  · 


Extracto de la transcripción automática del vídeo realizada por YouTube.

ok so it's five o'clock let's start thank you all for coming i'm gratified to see that you all are interested and didn't ever luck too much with a recession stay i'm peter linen laughs umme people NN non drupal org in IRC i've been

involved using drupal since people 47 and involved on and off with core development drupal five six seven and now eight and today i want to talk to you I really about a little bit of a paradigm shift in Drupal a were moving to use a plug-in system as a way

to discover functionality instead of using our used in previous versions which was an info hook but gave you a list of data here's some way what we're going to talk about so just imma give you some background white people eight hopefully this will

be stuff you guys have heard before but make sure at least the the terms are in your head before I start using them and tell you what a plug-in is as then give you some code examples show back and forth between a group of seven in Drupal 8 as how a Drupal

7 hook an implementation was converted into a plug-in then move on to a simple example but one that you'll probably use in your modules which is displaying a tab otherwise known as a local task on a page then talk about our slightly more advanced but not

really more advanced examples implemented custom block and implementing a custom text filter and finally just touch on very briefly some considerations if you need to define your own plug-in for a module that you're porting to Drupal 8 so my clicker works

there you go all right so Drupal 8 background this you need to know these things i'm just going to run through them so you've heard the terms if you haven't performed so drupal 8 we're making a great sense of use of a dependence from You Tina

or DIC also the container or service container that's just an object that contains other objects instances of them and those are basically stateless services they tell you things like the current request the current user how way to generate URLs so that

when I talk about a service it's something that you can request from this container service container you should also know that we have a new routing system and this really the fundamental concept you need to know about this new routing system is we have

allowed names and the route name is the unique ID in Drupal 7 and before we remember but there was a system half and that was basically unique ID now it's a machine name but really works much the same so that route name that machine name connects you to

a path pattern could actually to a call back or set of callbacks to provide potentially title at the content for the page the access control so all the things that you did expected to use in a Drupal 7 many other are being provided through this named route

in Drupal 8 Andrew play also makes extensive use of namespace classes so if case you haven't seen this already this is a PHP 53 feature we're making sense of use of it that's what the class names are going to look like all over Drupal 8 so they

start with a section called Drupal because that's the name of the vendor the name of module in this case search and then some descriptive sections or a plug-in and it's a block plug-in and it's in search bloc plug-in and that whole string together

is the actual class name and i'll come back to why we're using that later but again so we're name spaces or names to have clauses that's a that's how it looks in code um so what is a plug-in plugins encapsulate some functionality and I

do it's a reusable functionality plugins always a class so one plugin neither one unique ID of a plug-in maps to one class and generally that class is going to implement one or more specific interfaces so it's reusable functional behind one or more

standard interfaces so plugins combine in Drupal 7 what was an info hook they see a listing of things together with the implementation of those things so for an example in Drupal 7 I did lots of work research so hook search info if you've used it and you

know it returns your list of modules and the modules are declaring that they have hook search execute so it's the combination of that info hook together with the implementation in hook search execute or an executive I'm going to come back to later

in this talk is hook block info but Park info is the list of all the blocks that all of the modules provide in the system and each module revised box need to have a hook block view to render box and would mean a hook or configure to tell you lots of luck save

to save the configuration so again we have an intro hook that lists the things and then we had separate functions those functions might not even be in the same file or if they were the same power might be a bit for pod the file are they rarely organized together

in a coherent way that you could go from understanding this info hooks array to actually seeing what the implementation was there is now you had a basic grep through the code to find it so Drupal 8 hooks evolved in a way from the concepts in Drupal 7 and before

that are used by C tools and views they both had plugins plugins systems that we use to are widely but we'll see that we use using different mechanisms to discover those plugins than were used in Drupal 7 another feature of Drupal 8 is that every plug-in

system generally has a plugin manager and this is the plugin manager service so it's registered to that service container basically anywhere in your drupal code that you need to access these plugins you can say give me that manager and the manager then

can give you the list of plugins or can you be an instance for one specific plug-in you're interested in each plug-in has a unique ID and often that's going to be in its definition will see a simple example that a little bit I may also be generated

derivative and now that we're going to talk about to a bit of us what is concept you should be aware of and the obvious example is views blocks so if use doesn't write a new class each time you want to declare a block instead what it's going to

do is create a derivative so it has one class that has the implementation but for each block you can you create through views that's going to create a plug-in derivative that has a different unique ID so its basic combination of a base identifier and then

a unique derivative identify er sorry I'm going to say that delivers you should just be aware but that's a feature of the plug-in system for a given plug-in ID and get a unique ID that maps to a single class so any instance of a plug-in with that plugin

idea is going to use that class but the plugin ID instance is further specified by the combination of the class and some configuration values and those configuration values are potentially coming from a config entity this is something I will talk about later

but they see you can imagine this is just configuration so we have a class we have a configuration together they make a specific instance of the plug-in that you can do something with so yes probably have seen this place before the drop was always moving and

that means the code in Drupal 8 is not going to be compatible with Drupal 7 but your data your content can be upgraded between drew provisions um so let's just see what that looks like how the code changing between Drupal 7 and Drupal 8 and the NIP I want

to start with was hook image tool kits so this is in a sense of classic info hook again many of those info box don't actually have info on their name but that's what it is is it's returning an array of data and this is the system module version

of that hook and it's just telling you about the GD library the GD library generally is compiled into PHP in Drupal 7 will return to unique key this just GD you see their title human readable and then we actually had to check if this library always available

through PHP so sometimes you PHP without it as you see we have a basically using a function check here to set this available flag in this info or a this return from this info hook even though it's not named info so that's true / 7 Drupal 8 you actually

end up doing something very similar so Drupal 8 instead of doing the module invoke all to find all the implementations we asked the manager for the list of implementations or a list of available tool kits and the manager has a method here just get available

tool kits and inside that it's actually using something was pretty common to all managers which is get definitions and you think of get definitions is really a one-to-one mapping from the info Hulk in Drupal 7 it's getting back arrays of information

and then we're just iterating over those we have IDs and for each of those possible IDs we're checking a method to see if the library is actually about available so you see there's really a wonder when mapping in a sense between the Drupal 7 code

we've got a list of all of the things we call the function his teeth libraries actually available 2.8 we're basically do the same thing the plugin manager is discovering all the possible plugins and validating that each one is actually available on

our version of PHP if you look at a particular function of the image library now this would have been organized potentially in an include file but there's no real requirement as to how you would have organized this function compared to the info hook you

see here that we're actually using another layer of indirection in Drupal 7 because we have to call back into your toolkit method so we actually get the right underlying function call right on the line tool kit but really you never just pass get an image

object and call it a function on it in Drupal 8 it's actually a little clearer because this class knows that it's dealing with the GD library so there's no extra level of indirection that's needed for the users party we know that we're

using the GD library and you'll see something to come back to later that there's an annotation on this class at the top you see that add image toolkit and that's saying that this class is an image 20 k plugin and you'll see that that ID equals

[ ... ]

Nota: se han omitido las otras 5.058 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.