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

  • 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.

主站蜘蛛池模板: 淳安县| 修武县| 临沭县| 安乡县| 大悟县| 林甸县| 吉林省| 镇江市| 中西区| 南溪县| 恩平市| 江西省| 麻阳| 清镇市| 民权县| 靖宇县| 东乡族自治县| 永吉县| 宁津县| 武山县| 苍梧县| 侯马市| 治县。| 屯昌县| 曲周县| 宝应县| 鹿邑县| 崇仁县| 达孜县| 曲麻莱县| 大化| 泸水县| 江安县| 会昌县| 枣庄市| 武夷山市| 文成县| 石城县| 大余县| 大同市| 墨玉县|