【问题标题】:Add extra div-class/wrap div class around existing div in Drupal在 Drupal 中的现有 div 周围添加额外的 div-class/wrap div 类
【发布时间】:2011-12-15 14:21:08
【问题描述】:

在我的 Drupal 网站上,我有这样的内容:

<div class="form-item form-type-textfield item-1">
<div class="form-item form-type-textfield item-2">
<div class="form-item form-type-textfield item-3">
<div class="form-item form-type-textfield item-4">
...

有什么办法可以得到这样的

<div class="form-item form-type-textfield item-1 added-class">

<div class="wrapped-class">
   <div class="form-item form-type-textfield item-1">
</div>

所以,给一个项目添加一个类或在它周围包装一个类。我玩过hook_preprocess,但没有成功。

hook_form_alter 也不是解决方案,因为您必须为每个表单都这样做... div 类出现在多个页面上,所以我想用一个 code-sn-p 来捕获语句.
有任何想法吗?

【问题讨论】:

    标签: css class drupal


    【解决方案1】:

    我很惊讶 Drupal 没有“外包”这个功能,但是如果您查看 theme_form_element() function,您会发现这些类是静态设置的。

    我能想到在代码中做你想做的唯一方法是对该函数使用主题覆盖:

    function mytheme_form_element($variables) {
    
    }
    

    在该函数中,您需要放置 theme_form_element 函数中的整个代码(我知道这似乎有点矫枉过正,但我​​想不出更好的方法),并将您的新类添加到 $attributes['class'] 数组:

    $attributes['class'] = array('form-item', 'my-other-class');
    

    【讨论】:

    • 如果我的主题叫module,我只需要这样做module_form_element($variable),对吗?还是我需要更换其他东西?
    • 噢,这不是主题名称的好选择! Drupal 中的许多函数都以​​module_ 开头,以至于您很容易在后面遇到命名空间问题。但是,是的,module_form_element 将是正确的函数名称。实现主题覆盖后,请记住清除 Drupal 的缓存 :)
    • 是的,我非常了解 Drupal 命名约定 :) 这只是我想出的第一个词 :-) 我现在正在研究更可靠的解决方案。因为 Drupal 不允许我更改属性...
    【解决方案2】:

    根据您要放置的位置,查看该块/区域/页面的模板文件并创建一个模板来覆盖它。例如,如果您想将该 div 添加到每个节点,我将在我的自定义主题中创建一个 node.tpl.php 并将其添加到那里。正如我所说,这取决于你想把它放在哪里。

    【讨论】:

    • 我在想类似的事情,但我想用代码来做......你没有想到任何功能吗?因为我的已经坏了:)
    猜你喜欢
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    相关资源
    最近更新 更多