2017-08-28 53 views
0

我有一個建立菜單的模塊。我想添加css類到模塊生成的<a>標籤。我使用dpm()來找到我需要添加我的類的正確數組。這是Drupal:如何正確添加一個'類'數組到一個元素的'屬性'數組?

['#localized_options']['attributes']

已經有一個[title]排列在那裏,但沒有數組類。

我試着將我的班了幾個不同的方式,因爲這樣的:

$item['#localized_options']['attributes']['class'] = "some-styles"; 

$item['#localized_options']['attributes']['class'][] = "some-styles"; 

$item['#localized_options']['attributes'] = array('class' => "some-styles"); 

但我不斷收到錯誤:

Fatal error: Cannot use string offset as an array

會有人一個知道我應該怎麼做這個?

回答

1

根據Form api,屬性應該以#前綴開頭。 所以我認爲,正確的做法是:

$item['#localized_options']['#attributes']['class'][] = 'some-styles'; 

參考: https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7.x#attributes

PS:不使用

$item['#localized_options']['#attributes'] = array('class' => "some-styles"); 

否則,你將complitely擦除#attributes只是增加你的CSS樣式!這是錯誤的,因爲許多其他模塊可能會添加自己的#屬性屬性!

相關問題