官术网_书友最值得收藏!

The HelloWorldSalutation service

Now that we have a general idea as to what a service is, let's create one to see all this in practice.

As I mentioned earlier, I want my greetings to be more dynamic, that is, I want the salutation to depend on the time of day. So, we will create a (HelloWorldSalutation) class that is responsible for doing that and place it in the /src folder (our module's namespace root in a file naturally called HelloWorldSalutation.php):

namespace Drupal\hello_world;

use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
* Prepares the salutation to the world.
*/
class HelloWorldSalutation {

use StringTranslationTrait;

/**
* Returns the salutation
*/
public function getSalutation() {
$time = new \DateTime();
if ((int) $time->format('G') >= 00 && (int) $time->format('G') < 12) {
return $this->t('Good morning world');
}

if ((int) $time->format('G') >= 12 && (int) $time->format('G') < 18) {
return $this->t('Good afternoon world');
}

if ((int) $time->format('G') >= 18) {
return $this->t('Good evening world');
}
}
}
From now on I will not always mention the file name a particular class goes into. So you can safely assume one file per class, named after the class itself.

By now, I assume that the namespace business is clear, so I won't explain it again. Let's see what else we did here. First, we used the StringTranslationTrait in order to expose the translation function (I will explain this later on). Second, we created a rudimentary method that returns a different greeting depending on the time of day. This could probably have been done better, but for the purposes of this example, it works just fine.

In this example I used the native PHP function time() to get the current time. And that's ok. But you should know that Drupal has its very own Drupal\Component\Datetime\Time service that we can use to get the current time. It also has additional methods for requesting time specific information, so make sure you check it out and use when appropriate.

Now that we have our class, it's time to define it as a service. We don't want to be going new HelloWorldSalutation() all over our code base, but instead, register it with the Service Container and use it from there as a dependency. How do we do that?

First, we will need, yet again, a YAML file: hello_world.services.yml. This file starts with the services key, under which will be all the service definitions of our module. So, our file will look like this (for now):

services:
hello_world.salutation:
class: Drupal\hello_world\HelloWorldSalutation

This is the simplest possible service definition you can have. You give it a name (hello_world.salutation) and map it to a class to be instantiated. It is a standard practice to have the service name start with your module name.

Once we clear the cache, the service will get registered with the Service Container and will be available to use.

If there is any reason to believe that you will have more than one salutation service, you should create an interface this class can implement. This way, you'll be able to always type hint that interface instead of the class and make the implementations swappable.
主站蜘蛛池模板: 桑植县| 女性| 兴文县| 长治市| 阆中市| 景德镇市| 白城市| 蒲城县| 洪泽县| 和田市| 肇庆市| 梁平县| 恭城| 凤阳县| 胶州市| 东平县| 浏阳市| 乌苏市| 平陆县| 金坛市| 延安市| 苗栗市| 庄河市| 凌海市| 延寿县| 湖北省| 巫山县| 义马市| 全椒县| 大悟县| 寿光市| 遂溪县| 舞阳县| 蒙阴县| 黑山县| 闻喜县| 子洲县| 阳山县| 磐安县| 临城县| 乐都县|