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

Blocks

Custom blocks in Drupal 8 are plugins. Finally, we encounter our first plugin type. For the sake of full disclosure, the content blocks that you create through the UI to place in a region and the custom blocks that are placed in a region are content entities. So, the block system is a good example of how entities and plugins work hand in hand in Drupal 8, and not to make matters too complex for you, configuration entities (about which we will learn in a later chapter) also play a big role here.

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 easily? 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. 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.

主站蜘蛛池模板: 民县| 监利县| 韩城市| 开原市| 泗水县| 中阳县| 娄底市| 晋中市| 萨迦县| 晋江市| 星座| 灵台县| 莱芜市| 工布江达县| 务川| 清流县| 阳江市| 大关县| 盖州市| 新郑市| 陆良县| 东城区| 桐城市| 临泉县| 望江县| 宝山区| 东海县| 武义县| 鄂温| 塘沽区| 苗栗市| 昌吉市| 闸北区| 敖汉旗| 礼泉县| 若尔盖县| 阳新县| 含山县| 临洮县| 浙江省| 轮台县|