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

 
Điện thoại BlackBerry Q10 dự kiến có mặt tại Việt Nam vào ngày 18/6

Điện thoại BlackBerry Q10 dự kiến có mặt tại Việt Nam vào ngày 18/6

Điện thoại thông minh BlackBerry Q10 dự kiến có mặt tại Việt Nam vào ngày 18/6 và được cung cấp thông qua các đơn vị bán lẻ được ủy quyền.

thuật Ngữ SEO

Tất cả các thuật Ngữ SEO thông dụng

Có rất nhiều những thuật ngữ SEO. Tuy nhiên, trong bài viết này chỉ đề cập tới những thuật ngữ SEO thường được sử dụng nhất. Những giải thích đơn giản, ngắn gọn và dễ hiểu giành cho người mới bắt đầu tìm hiểu và nghiên cứu SEO.

Drupal 8 đứng đầu về Mobile CMS platform

Drupal 8 đứng đầu về Mobile CMS platform

I took that as a challenge to help make Drupal the leading mobile CMS platform. Certainly, there are some fantastic contributed modules that already make Drupal a great starting point for mobile solutions

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

 

Diet con trung