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

 
Amazon gỡ bỏ quyền root dành cho người dùng Kindle Fire

Amazon gỡ bỏ quyền root dành cho người dùng Kindle Fire

Bản cập nhật phần mềm 6.2 mới cho Kindle Fire gỡ bỏ quyền truy cập root dành cho người dùng, tuy nhiên người dùng vẫn có thể tiến hành root lại

Panda Cloud Antivirus, Virus

Panda Cloud Antivirus: Bảo vệ máy tính nhẹ nhàng mà hiệu quả

Có rất nhiều chương trình antivirus khác nhau cho bạn lựa chọn, nhưng để tìm kiếm một chương trình antivirus vừa nhỏ gọn,...

Thiết kế website Drupal - vấn đề bảo mật

Thiết kế website Drupal - vấn đề bảo mật

Vấn đề bảo mật của một trang web là được đề cập rất nhiều trong giai đoạn hiện nay, các cuộc tấn công từ nhóm hacker Mũ Đen gây ra đã gây ra những tổn thất lớn lao

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

 

Diet con trung