简体   繁体   English

Drupal 7,有关主题的一般问题

[英]Drupal 7, General question about themeing

i've been having a nightmare trying to understand the Drupal way! 我一直在做噩梦,试图了解Drupal的方式! I have an example here and if someone could show me the correct way to achieve my result I'm hoping it will help me open the door to understanding. 我这里有一个例子,如果有人可以向我展示实现我的结果的正确方法,我希望它可以帮助我打开理解的大门。

So I'm trying to alter the Node creation Date to appear similar to Stack Overflows format. 因此,我试图将“节点创建日期”更改为类似于“堆栈溢出”格式。 EG This is either "Posted x hours ago" or "Posted on 17th Aug at 12:22" etc. EG这可以是“ x小时前发布”或“ 8月17日12:22发布”等。

I have managed to create the desiered effect using the "node.tpl.php" file. 我设法使用“ node.tpl.php”文件创建了所需的效果。 I've achieved this using the node variable "$created" and the following code. 我已经使用节点变量“ $ created”和以下代码实现了这一点。

    $showCreate = round((time() - $created) / 60);
if ($showCreate < 60) {
    $showCreate = $showCreate . "mins ago." ;
} else {
    $showCreate = round($showCreate / 60);
    if ($showCreate > 24) {     
        $createMonth  = format_date($created, 'custom', 'M');
        $createDate   = format_date($created, 'custom', 'd');   
        $createTime   = format_date($created, 'custom', 'H:i');
        switch ($createDate) {
            case 1:
            case 21:
            case 31:
                $createDate = $createDate . "st";
                break;
            case 2:
            case 22:
                $createDate = $createDate . "nd";
                break;
            case 3:
            case 23:
                $createDate = $createDate . "rd";
                break;
            default:
                $createDate = $createDate . "th";
                break;
        }
        $showCreate = $createMonth . " " . $createDate . " at " . $createTime;
    } else {
        $showCreate = $showCreate . "hrs ago." ;    
    }
}

Is this a 'correct' way to achieve this? 这是实现这一目标的“正确”方法吗? or would you use some of the hooks and preprocess functions? 还是会使用某些挂钩和预处理功能? I would prefer to wrap this up into a MOdule so I can apply it directly to my other drupal sites. 我希望将其包装到模块中,以便将其直接应用于其他drupal站点。

Thanks a lot in advance. 非常感谢。

There are a fair few different ways to do this in Drupal 7, I guess it depends on what you're doing. 在Drupal 7中,有几种不同的方法可以做到这一点,我想这取决于您在做什么。

If you're writing a module then you want to use hook_node_view to alter the rendered content. 如果要编写模块, 则要使用hook_node_view更改呈现的内容。

This is some example code from the page above: 这是上面页面中的一些示例代码:

function hook_node_view($node, $view_mode, $langcode) {
  $node->content['my_additional_field'] = array(
    '#markup' => $additional_field, 
    '#weight' => 10, 
    '#theme' => 'mymodule_my_additional_field',
  );
}

If you're writing a theme then you want to use hook_preprocess_node in your theme's template.php file, something like this: 如果要编写主题,则要在主题的template.php文件中使用hook_preprocess_node,如下所示:

function mytheme_preprocess_node(&$vars) {
  $node = $vars['node'];

  $vars['my_created_date'] = my_date_extraction_function($node->created);
}

Then in your node.tpl.php you'll have the variable $my_created_date available, which you can use in place of the original created date. 然后,在您的node.tpl.php中,您将拥有可用的变量$ my_created_date,可以用它代替原始的创建日期。

Obviously you can use your imagination here, anything you add to the $vars array will be available in node.tpl.php and you have full access to the node object in mytheme_preprocess_node(). 显然,您可以在这里发挥您的想象力,添加到$ vars数组中的任何内容都将在node.tpl.php中可用,并且您可以在mytheme_preprocess_node()中完全访问node对象。

If you haven't used hook_node_view or the render API before you might find it easier to use the preprocess_node method as there's less of a learning curve. 如果您尚未使用hook_node_view或render API,则可能会发现使用preprocess_node方法更容易,因为学习曲线较少。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM