在Drupal 7中为特定内容类型的图像字段添加类

时间:2012-07-05 17:07:15

标签: drupal

目前我正在使用this answer中建议的代码。以下是:

function simalr_preprocess_image(&$variables) {
    if ($variables['style_name'] == 'request-background') {
    $variables['attributes']['class'][] = 'pixastic';
    $variables['attributes']['class'][] = 'pixastic-blurfast(amount=1)';
    }
}

除了我在没有带有'request-background'样式的图像的页面上收到以下错误消息之外,这个工作正常:

  

注意:未定义的索引:simalr_preprocess_image()中的style_name(/var/www/vhosts/simalr.com/httpdocs/sites/all/themes/simalr/template.php第46行)。

我只希望在特定内容类型(即“请求”)上使用这段代码。我必须以哪种方式调整template.php文件中的代码才能在只有特定内容类型的页面上使用它?

1 个答案:

答案 0 :(得分:0)

您仍然可以使用代码,但使用isset功能。这将删除警告。

如果您只想为特定内容类型执行此操作,请在drupal中使用menu_get_object函数。如果节点是节点页面,则此函数将为您返回节点。

示例:

$node = menu_get_object();

if ($node->type == 'story') {
  // TODO
}

希望这有帮助。

相关问题