Giải pháp theme hook suggestions và custom view modes cho templates trong Drupal 7

Giải pháp theme hook suggestions và custom view modes cho templates trong Drupal 7

>> Cài đặt vitual box cho Web Development ( Virtual Machines )

>> 3 điều quan trọng để Go live cho Drupal Emmy’s website

>> David Baker chia sẻ 11 suy nghĩ cần thiết cho Drupal team

When it comes to providing custom view modes for entities in Drupal, most developers turn immediately to Display Suite or Entity View Modes. Display Suite is very powerful, but I've found it to be far too "heavy" for most of my use cases. In this case, by "heavy" I mean that the module days waymore than I need it to. It does a lot of things I simply don't want on my site. Entity view modes is a lighter-weight approach to supplying additional view modes, but I found that even it was cumbersome when it came to managing my view mode configuration in an easily-deployable way.

Giải pháp theme_hook_suggestions và custom view modes cho templates trong Drupal 7

I wanted a simpler solution, and I knew it couldn't be that hard to provide this functionality in a custom, streamlined module.

The code snippets below do the following:

  • Define new display modes. As you'll see, this is super easy to do in code.
  • Suggest node templates based on the view mode of the given node. e.g. a teaser template vs. a full display template.
  • Suggest page templates based on the node type being viewed. e.g. alter the page markup for Article nodes.
/**
 * Implements hook_entity_info_alter().
 *
 * Provide front-page feature and sub-feature view modes. Template suggestions
 * are provided in example_preprocess_node().
 */
function example_entity_info_alter(&$entity_info) {
  $entity_info['node']['view modes']['front_feature'] = array(
    'label' => t('Front page feature'),
    'custom settings' => TRUE,
  );
  $entity_info['node']['view modes']['front_sub_feature'] = array(
    'label' => t('Front page sub-feature'),
    'custom settings' => TRUE,
  );
}

/**
 * Implements hook_preprocess_node().
 */
function example_preprocess_node(&$variables) {
  // Provide template suggestions based on the view mode.
  // Example 1: node__front_feature
  // Example 2: node__article__front_feature
  if (!empty($variables['view_mode'])) {
    $variables['theme_hook_suggestions'][] = 'node__' . $variables['view_mode'];
    $variables['theme_hook_suggestions'][] = 'node__' . $variables['type'] . '__' . $variables['view_mode'];
  }
}

/**
 * Implements hook_preprocess_page().
 */
function example_preprocess_page(&$variables) {
  // Provide template suggestions based on the node type.
  // Example 1: page__node__article
  if (!empty($variables['node']->type)) {
    $variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->type;
  }
}

My two cents: In Drupal development, we all-to-often turn immediately to heavy-weight, all-encompassing solutions for extremely simple problems. Simple solutions that leverage Drupal core's great APIs are generally completely sufficient.

Related reading: Looks like I'm not alone in seeking a lean approach to D7 view modes and template suggestions. Check out this post by Brad Czerniak titled Lean Drupal with Content View Modes and Views.

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Tìm kiếm bất động sản

 

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
 10 Homescreen đẹp nhất năm 2011

10 Homescreen đẹp nhất năm 2011

Trang web MyColorScreen - 1 trang web cho phép người dùng chia sẻ homescreen của smartphone và tablet của mình - đã bình chọn ra 10 homescreen đẹp nhất...

Hướng dẫn Drupal 8 Theming Fundamentals phần 1

Hướng dẫn Drupal 8 Theming Fundamentals phần 1

We’re going to begin by building the bare minimum required to get our theme working.

Kỹ thuật Reviewing Code với IDE PHPStorm to inspect code

Kỹ thuật Reviewing Code với IDE PHPStorm to inspect code

This process can be overkill for a one line patch, but once the patch is more than a couple of lines is helps the reviewer a great deal.

Công ty diệt chuột T&C

 

Diet con trung