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

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.

主站蜘蛛池模板: 连云港市| 白河县| 长春市| 尉氏县| 屯门区| 德清县| 仪陇县| 准格尔旗| 双柏县| 洞头县| 和平县| 阿瓦提县| 都江堰市| 二连浩特市| 信丰县| 西宁市| 黔南| 启东市| 湘潭市| 错那县| 永宁县| 阳西县| 珠海市| 叶城县| 九龙城区| 丰宁| 高碑店市| 山西省| 锦屏县| 时尚| 汽车| 芒康县| 抚松县| 额济纳旗| 常熟市| 湟中县| 陵川县| 马山县| 洮南市| 水城县| 晋中市|