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

  • Drupal 8 Module Development
  • Daniel Sipos
  • 230字
  • 2021-07-02 15:45:18

Which way to link?

As we saw, we have a number of ways to create links and URL representations, but when it comes to creating a link, which method should we use? There are advantages and disadvantages for each one.

When it comes to URL, as mentioned, it's a good idea to stick to hardcoding routes rather than URIs. However, if you are working with dynamic data, such as user input or stored strings, the other methods are perfectly valid. I recommend that you look at the Url class in detail because you will be using it quite a bit as you develop Drupal 8 modules.

Regarding the actual links, using the service to generate a link means that you are creating a string at your point in the code. This means that it cannot be altered later in the process. On the other hand, using the Link class falls nicely in line with the entire render array rationale of delaying the actual generation to the last possible moment. We will talk more about render arrays later on. So, the choice you make depends on the link you need to generate and your answer to the following question: is the link something that might have to be alterable by other modules/themes? If so, proceed with the render array. Otherwise, you might consider generating if you can inject the service properly.

主站蜘蛛池模板: 碌曲县| 周至县| 丰顺县| 延川县| 梁河县| 凉城县| 罗平县| 修水县| 天津市| 汶川县| 辉南县| 渭南市| 霍邱县| 泸水县| 哈巴河县| 邹城市| 涞水县| 兰溪市| 鸡西市| 锡林浩特市| 崇信县| 利津县| 莱州市| 奈曼旗| 漠河县| 邯郸县| 辽阳县| 富锦市| 克拉玛依市| 鄂尔多斯市| 比如县| 宕昌县| 乌鲁木齐县| 玉屏| 汝阳县| 丰城市| 白玉县| 枞阳县| 克拉玛依市| 璧山县| 北安市|