- Drupal 9 Module Development
- Daniel Sipos Antonio De Marco
- 391字
- 2021-06-11 18:36:06
Attributes
In the previous three examples of theme hooks, we encountered the concept of attributes in the context of using them to render HTML elements. Attributes here are understood in the same way as with HTML. For example, class, id, style, and href are all HTML element attributes. Why is this important?
The reusability of theme hooks makes it so that we cannot hardcode all our HTML attributes in the Twig template files. We can have some, including classes, but we will always need to allow business logic to inform the theme hook of certain attribute values it needs printed on the HTML element. For example, an active class on a link. This is why we have this concept of attributes.
Most theme hooks you'll see have attributes in some form or another, with the variable usually being called $attributes, $wrapper_attributes, or something of that nature. Also, this variable always needs to be a multidimensional array with the attribute data you want passed. The keys in this array are the name of the attribute, whereas the value is the attribute value. If the value can have multiple items, such as classes, it will also be an array. Consider the following example:
$attributes = [
'id' => 'my-id',
'class' => ['class-one', 'class-two'],
'data-custom' => 'my custom data value'
];
As you can see, we have some common attributes, but you can also make up your own as needed (usually in the form of data attributes). However, in no way is this mandatory, and you can add only the ones you actually need. Do always, though, read the documentation on the theme hook to see how they are used and which elements are actually going to get them.
From an API point of view, Drupal handles attributes via a handy class called Attribute. You'll note that many template preprocessors will take that array and construct a new Attribute object for manipulating them with more ease. Additionally, such an object is also renderable because it implements the MarkupInterface and Twig will know directly how to transform it into a string. And to make dealing with attribute collections easier, a helper class called AttributeHelper was introduced.
So, keep that in mind if you are writing your own theme hooks and need to handle attributes with more class (pun intended).
- 互聯(lián)網(wǎng)+微運(yùn)營(yíng):微運(yùn)營(yíng)管理及實(shí)戰(zhàn)全攻略
- 電子商務(wù)視覺設(shè)計(jì)(視頻指導(dǎo)版)
- 微視,這么玩才賺錢!
- 重新定義互聯(lián)網(wǎng)電商:打造開放共享網(wǎng)絡(luò)新生態(tài)
- 引爆社群:移動(dòng)互聯(lián)網(wǎng)時(shí)代的新4C法則
- 網(wǎng)絡(luò)整合營(yíng)銷:外貿(mào)篇
- 網(wǎng)絡(luò)人的未來:移動(dòng)互聯(lián)網(wǎng)和大數(shù)據(jù)時(shí)代的100個(gè)預(yù)言
- 中國(guó)電子商務(wù)之都互聯(lián)網(wǎng)經(jīng)濟(jì)發(fā)展報(bào)告(2010年)
- 移動(dòng)電商:商業(yè)分析+模式案例+應(yīng)用實(shí)戰(zhàn)
- APP運(yùn)營(yíng)推廣:搶占移動(dòng)互聯(lián)網(wǎng)入口、引爆下載量、留住用戶
- 電商這么玩最賺錢
- 淘寶天貓網(wǎng)店設(shè)計(jì)從入門到精通:店鋪裝修、廣告海報(bào)、修圖修片、架構(gòu)布局、配色應(yīng)用、設(shè)計(jì)模版
- 贏在運(yùn)營(yíng):微店引流、推廣、管理一本通
- 字里行間的商業(yè)秘密:軟文營(yíng)銷(升級(jí)案例版)
- 中國(guó)金融新生態(tài):全面解析互聯(lián)網(wǎng)金融