Hướng dẫn thêm 1 pseudo field trong Drupal 7

Hướng dẫn thêm 1 pseudo field trong Drupal 7

Sometimes you need to display a field that have no input, like a static text or image on each node, or a Facebook like button, or whatever that does not have an input, but you still want to display it as a field, for that you have the hook hook_field_extra_fields.

Hướng dẫn thêm 1 pseudo field trong Drupal 7

To display a pseudo field you just need two functions (these functions I added to a custom module called wk_extra_fields)

<?php
/**
* Implements hook_field_extra_fields().
*/
function wk_extra_fields_field_extra_fields() {
  $extra = array();
    // Just adding the psuedo field to the page node type.
    $extra['node']['page'] = array(
      'display' => array(
        'wk_foo' => array(
          'label' => t('WK: Foo'),
          'description' => t('A foo field.'),
          'weight' => -5,
        ),
      ),
    );
  return $extra;
}

/**
* Implements hook_node_view().
*/
function wk_extra_fields_node_view($node, $view_mode, $langcode) {
  // The field is showed in when using full view mode and on page node type.
  if ($view_mode == 'full' && $node->type == 'page') {
    $node->content['wk_foo'] = array(
      '#markup' => '<div class="wk-foo">Foo!</div>',
    );
  }
}
?>

So we use the function wk_extra_fields_field_extra_fields() to create the pseudo field for the page node type, after that we can activate it in the display tab, and choose where to show the field.

When we view the node, the function wk_extra_fields_node_view() is called and the content is displayed.

Bạn thấy bài viết này như thế nào?: 
Average: 5 (1 vote)
Ảnh của Khanh Hoang

Khanh Hoang - Kenn

Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.

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

 
CEO Facebook vừa kết thúc chuyến thăm Hạ Long

CEO Facebook vừa kết thúc chuyến thăm Hạ Long

Sau chuyến bay bằng trực thăng đến Hạ Long, Mark đã trở về Hà Nội rồi nhé!

Hướng dẫn gửi HTML Emails từ Drupal Webform module

Hướng dẫn gửi HTML Emails từ Drupal Webform module

The Webform module is one of the most popular modules in Drupal.

Hướng dẫn chính cho Drupal 8: Episode 1 - Authoring Experience Improvements

Hướng dẫn chính cho Drupal 8: Episode 1 - Authoring Experience Improvements

Welcome to the first instalment of an 8-part blog series we're calling "The Ultimate Guide to Drupal 8."

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

 

Diet con trung