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

Blocks

Blocks in Drupal 8 are plugins. However, the blocks you create in the UI are content entities and the placement of both in the block layout are configuration entities. So, the block system is a good example of how entities and plugins work hand in hand in Drupal 8. We will talk in more detail about plugin types and entities later in the book.

The block system in Drupal 8 is a great shift from its predecessor. Before, you had to implement two obligatory hooks plus two optional hooks if you wanted the block to have a configuration, and the latter was always saved somewhere that had nothing to do with the block itself. In Drupal 8, we work with a simple plugin class that can be made container-aware (that is, we can inject dependencies into it) and we can store configuration in a logical fashion.

So, how do we create a custom block plugin? All we need is one class, placed in the right namespace—Drupal\module_name\Plugin\Block. In this case (with plugins), the folder naming is important. The plugin discoverability is dependent on the plugin type itself, and this one has the Plugin\Block namespace bit in it. But enough talk, let's create a simple block that just renders the same as our Controller did previously, and I will explain things along the way.

主站蜘蛛池模板: 白银市| 海阳市| 迭部县| 开封市| 云南省| 宁河县| 哈尔滨市| 昭觉县| 开平市| 栾川县| 太仓市| 阿瓦提县| 文昌市| 磐安县| 清原| 株洲县| 南平市| 广元市| 九龙县| 襄垣县| 含山县| 镇沅| 屏南县| 阳山县| 陇西县| 东至县| 荣成市| 若尔盖县| 游戏| 公主岭市| 东明县| 灵宝市| 陆丰市| 二手房| 长岛县| 奉新县| 晋城| 长阳| 桐城市| 上高县| 任丘市|