注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

淘尽网 官方博客

淘尽网http://www.tao3w.com做最好的比价网站

 
 
 

日志

 
 
关于我

淘尽网 http://www.tao3w.com 做最好的比价网站,做最好的数据抓取专家。

网易考拉推荐

Drupal 7 custom node view modes  

2013-03-07 17:07:42|  分类: drupal |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
http://www.wunderkraut.com/blog/drupal-7-custom-node-view-modes/2010-12-20

One of the cool new things in Drupal 7 is the ability to easily add new “View modes” for nodes. View modes are available for other entities like comments also, but I’m gonna only cover the nodes for this time. 

Before, Drupal had only two “hard-coded” node view modes, the full node view and the teaser view. These two provided us just enough flexibility for most simple content listings, but quite often we needed to turn to Views in Fields mode to get more flexibility. By flexibility in this case I mean displaying the same content in different ways in different contexts.

In Drupal 7 we can create additional view modes by simply implementing hook_entity_info_alter() in our custom module.

 

/**
* Implements hook_entity_info_alter().
*/
function MYMODULE_entity_info_alter(&$entity_info) {
  $entity_info['node']['view modes']['another_teaser'] = array(
    'label' => t('Another teaser'),
    'custom settings' => TRUE,
  );
}

 

After defining the new view mode, we can go to content type’s “Manage Display” page and select which fields to display plus some additional options for fields, like which image style to use for image fields, for example.

display_fields.jpeg

Next we need to get this view mode used somewhere. We’re gonna create a View for that. Set the View to Style = Unformatted, Row style = Node and select the “Another teaser” view mode from the row style options. Optionally you can create a page display with a path for the View for easier testing.

views-select-view-mode.jpeg

Drupal 7 custom node view modes - jjdoor - 淘尽网 官方博客

Up to this point nothing is that different than creating a view in “fields” mode, and select the desired fields one by one in the view itself. The differences are mostly how we can apply some theming and custom layout for the content.

To get the most out of this we’ll propably want to add a custom node.tpl.php template for this view mode. Custom node template enables easy and flexible theming and we can use standard hook_preprocess_node() function for example to control the variables we have available in the template. To make Drupal use a different .tpl.php file for view mode we need to add a new template suggestion in hook_preprocess_node().

/**
* Implements hook_preprocess_node().
*/
function MYMODULE_preprocess_node(&$vars) {
  if($vars['view_mode'] == 'another_teaser') {
    $vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__another_teaser';
  }
} 

Duplicate standard node.tpl.php for the content type you need, like node--article--another-teaser.tpl.php. (NOTE: use underscores in template suggestions, dashes in the filename).

There are many other ways to get the exactly same outcome, like the Display Suite module, but seems to be an overkill for a simple need what we just covered. While Views in fields mode is still most often the way to go, this opens a completely fresh option if you need to reuse the templates in multiple views, or you need to display every content type in a different way in the same listing without doing gigantic template files for views.

  评论这张
 
阅读(517)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017