![]() * \Drupal\path_alias\AliasManager $aliasManager */ ![]() If you are writing code in Drupal it is also good practice to include docblock comments around this line so that your IDE can translate what type of object the $aliasManager variable contains. This does exactly the same thing as the above example, but in a single line of code $alias = \Drupal::service('path_alias.manager')->getAliasByPath($path) $alias = $aliasManager->getAliasByPath($path) Īs the service returns an object we can chain together the method calls and do the alias lookup in one line, like this. $aliasManager = \Drupal::service('path_alias.manager') We use the \Drupal::service() method to get an instantiated AliasManager object and then use a function in that object to translate the path to the alias. The service can be used the in following way. There are other ways to do this, especially if you have the full Node object, but this is used outside of that situation. This is useful if you have the node ID and want to find the correct path to the node so you can print it out. This means that given a path like "/node/123" you can translate this to an alias in the form of "/page/some-page". This service wraps the Drupal\path_alias\AliasManager class in Drupal and allows developers access to find an alias for a given path. I have just mentioned a handful here, but there are plenty more services available in Drupal 9.Ī good example of a service that is often used is the alias manager service. If you want access to configuration, the internal cron system, path and routing, the rendering process, translations, queues, cache and even date calculations then you can use a Drupal service to do that. There are many different services in Drupal, that govern everything. This allows you to get on with the work at hand without having to worry about where to get this or that object from and what parameters its constructor needs. Indeed, most of the complexity of services is deliberately hidden away from you. The good news is that using Drupal services is pretty simple. Since I gave that talk around Drupal 8 I have updated the examples to be in line with Drupal 9. ![]() I thought I would write it up as a couple of articles. I actually gave this article as a talk at DrupalCamp London 2018, but I have found myself referring to the slides quite often since then. If you have seen that construct around the internet but don't know what it means then I hope to clear things up a little. It is also bad practice to use this construct in certain situations, which I'll let into later on. This is helpful if you are familiar with Drupal services, but if you aren't then this doesn't tell you much. Before I understood them, I saw a lot of people online attempting to help by just pointing people to one service or another using this sort of construct. I first came across services when I started using Drupal 8 and it took me a little while to get my head around what they are and what they do. ![]() It's a little hard to know where they come from if you aren't used to the how they work. When you look at some Drupal source code you are likely to see objects being created out of apparent thin air. They can, however, be a little difficult for newcomers to the system to understand, especially if they are coming from Drupal 7 or other non-object oriented systems. They are powerful and are used all over Drupal to do pretty much everything. Services are a way to wrap objects and use dependency injection to produce a common interface. Drupal 8 and 9 are built upon services, with many parts of the system available through dependency injection, so it's important to understand the concepts. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |