我看到很多人发帖询问如何为OG首页定制主题.我自己也想对drupal4.7版本的旧式OG首页进行升级,但是我还不能找到一种像样的方式。文档中(至少我看到的是)看起来仅仅告诉我们如何为实际的group(组)节点类型定制主题(例如node-og.tpl.php),或者在默认的og_ghp_ron主题加点过滤器。这些都不是本文要讨论的。(译者注,这里所讲的首页,是小组的首页,而不是站点的首页,一个站点可以有n多个小组,每个小组有一个自己的首页面)。
让我们假定你想为你的OG首页展示的所有帖子定制主题的话。你想让这些在OG展示的帖子与它们独立的节点页面以及teaser列表页面有所区别。而对于所有的小组首页采用相同的主题方式。这是我所要讲解的(如果你想让每个小组的首页都不同的话,你仍然可以使用这个代码,或者你还可以使用views)。我觉得应该有更好的方式,我也听到有人说,这很容易,但是没有看到过讲解如何去实现的帖子。所以我只能采用我的笨方法了。
在本教程中,我们将定你的og节点类型叫做'og'。如果你不知道你的叫做什么,可以访问admin/og/og,并点击"group home page"。这里所选的就是你的og节点类型。
还有,你需要先备份你的站点,这样当你觉得不对劲的时候,还可以恢复原貌。
我想使用一种通用的方式,因为我不想,当我添加一个新的小组时,需要更新我的节点文件。我也不想让小组的创建者为小组创建一个路径别名。(当然,如果只有一个人能够创建小组,而且小组的数量也不多的话,你也可以手工的在小组编辑表单页面设置小组的路径别名,而不用安装pathauto模块;这取决于你)。
我首先要做的是安装Pathauto模块。Pathauto允许你为你的任何内容创建用户友好的url路径。按照Pathauto模块的readme里面的说明,进行安装,完成以后,访问admin/settings/pathauto,点击"node path settings"。如果你仅仅想为你的小组添加别名的话,那么在节点部分的顶部,删除"Default path pattern (applies to all node types with blank patterns below)"(默认路径模式(适用于所有的节点类型,如果下面的模式为空的话))下面的东西。
如果你没有设置这一项的话,所有的内容都将采用默认路径模式来设置别名。Pathauto是一个非常方便的模块,如果你想采用默认的模式的话,我鼓励你好好的读一下配置页面的说明文字。对于本教程,就放心把它删除了吧。
现在,在同一部分里面,找到"Pattern for all og paths"(根据你的og节点类型名会有所不同)。在下面的输入框中,输入:groups/[title-raw]。
把页面往下拉,选中复选框"Bulk generate aliases for nodes that are not aliased"(为已有的没有别名的节点批量生成别名)。保存。现在你的所有小组首页面应该都有一个自动的用户友好的别名了。好极了。
现在,我们已经找到了一种方式,帮助节点识别我们是否处于小组首页面,现在就可以编辑我们的模板文件了。进入你的主题文件夹下面,编辑名为node.tpl.php的文件。
与普通的node.tpl.php文件唯一不同的是,我们需要检查pathauto别名,来看看我们是不是处在小组首页面上。然而,我们仅需要URL的第一个参数(也就是"groups"部分),所以我们还需要使用PHP字符串函数对别名进行截取。下面的是我写的代码片断,最后为完整的代码。下面是如何决定我们是否处于小组首页面:
<?php
if (arg(0) == 'node' && is_numeric(arg(1))) {
$url_alias = drupal_get_path_alias('node/'.arg(1));
} elseif (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
$url_alias = drupal_get_path_alias('taxonomy/term/'.arg(2));
}
// We need to only take the first argument of the aliased path, so define the separator
$slash = '/';
// find out if the separator exists in the returned alias
$pos = strpos($url_alias, $slash);
// if the separator is not present, then just look at the URL alias; if it is present, strip the first argument only
if ($pos === false) {
$string = $url_alias;
} else {
$string = substr($url_alias, 0, $pos);
}
// if the first argument of the aliased path == 'groups', then we know it's a group home page
if ($string == 'groups'){ ?>
接下来,我们将格式化小组首页面的节点。在这里,我仅需要一个带有链接的标题,以及作者,发表日期,所以代码如下:
<?php if ($page == 0) { ?>
<div class="content"><a href="<?php print $node_url; ?>"><?php print $title?></a><span class="submitted"><?php print " ".$user_name. " " . $date; ?></span></div>
<?php }; ?>
接下来,如果别名不是'groups'的话,我将提供一个普通的teaser视图(view):
<?php
// if the first argument of the aliased path wasn't 'groups', then it's a normal teaser view
}else{ ?>
<div class="node<?php if ($sticky) { print " sticky"; } ?><?php if (!$status) { print " node-unpublished"; } ?>">
<?php if ($picture) { print $picture; }?>
<?php if ($page == 0) { ?>
<h2 class="title"><a href="<?php print $node_url; ?>"><?php print $title?></a></h2>
<?php }; ?>
<span class="submitted"><?php print $submitted?></span> <span class="taxonomy"><?php print $terms?></span>
<div class="content"><?php print $content?></div>
<div class="clr">
<?php if ($links): ?>
<div class="links"><?php print $links; ?></div>
<?php endif; ?>
</div>
</div>
<?php } ?>
接着,让我们添加独立的节点视图(view),从而完成我们的node.tpl.php文件。
下面是完整的可用的代码
(你可以根据需要自己定制外观):
<?php if ($teaser): ?>
<?php
// we just get the drupal path args so that we can look up the path alias
if (arg(0) == 'node' && is_numeric(arg(1))) {
$url_alias = drupal_get_path_alias('node/'.arg(1));
} elseif (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
$url_alias = drupal_get_path_alias('taxonomy/term/'.arg(2));
}
// We need to only take the first argument of the aliased path, so define the separator
$slash = '/';
// find out if the separator exists in the returned alias
$pos = strpos($url_alias, $slash);
// if the separator is not present, then just look at the URL alias; if it is present, strip the first argument only
if ($pos === false) {
$string = $url_alias;
} else {
$string = substr($url_alias, 0, $pos);
}
// if the first argument of the aliased path == 'groups', then we know it's a group home page
if ($string == 'groups'){
// below this is where you need to put your node formatting for the og home page ?>
<?php if ($page == 0) { ?>
<div class="content"><a href="<?php print $node_url; ?>"><?php print $title?></a><span class="submitted"><?php print " ".$user_name. " " . $date; ?></span></div>
<?php }; ?>
<?php
// if the first argument of the aliased path wasn't 'groups', then it's a normal teaser view
}else{
// below this is where you need to put your normal node teaser formatting ?>
<div class="node<?php if ($sticky) { print " sticky"; } ?><?php if (!$status) { print " node-unpublished"; } ?>">
<?php if ($picture) { print $picture; }?>
<?php if ($page == 0) { ?>
<h2 class="title"><a href="<?php print $node_url; ?>"><?php print $title?></a></h2>
<?php }; ?>
<span class="submitted"><?php print $submitted?></span> <span class="taxonomy"><?php print $terms?></span>
<div class="content"><?php print $content?></div>
<div class="clr">
<?php if ($links): ?>
<div class="links"><?php print $links; ?></div>
<?php endif; ?>
</div>
</div>
<?php } ?>
<?php else:
// here is where your full node formatting goes ?>
<div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?> clear-block">
<?php print $picture ?>
<?php if ($page == 0): ?>
<h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h2>
<?php endif; ?>
<div class="meta">
<?php if ($submitted): ?>
<span class="submitted"><?php print $submitted ?></span>
<?php endif; ?>
<?php if ($terms): ?>
<span class="terms"><?php print $terms ?></span>
<?php endif;?>
</div>
<div class="content">
<?php print $content ?>
</div>
<?php
if ($links) {
print $links;
}
?>
</div>
<?php endif; ?>
Hopefully that will help save somebody time.
希望本文对读者能有所帮助。