Hướng dẫn mở rộng để thêm field vào entity / node display trong Drupal 7

Hướng dẫn mở rộng để thêm field vào entity / node display trong Drupal 7

I had a case recently, where I needed to add custom data to the node display and wanted this data to behave like a field, however the data itself didn't belong to a field. By "behaving like a field" I mean you can that field at node display settings and able to control it's visibility, label and weight by dragging and dropping that field.

So, as you may have undestood, hook_preprocess_node / node_view_alter approach alone wasn't enough.

But we do Drupal right? Then there should be a clever way to do what we want and it is here: hook_field_extra_fields() comes for help!

hook_field_extra_fields() (docs: https://api.drupal.org/api/drupal/modules!field!field.api.php/function/h...) exposes "pseudo-field" components on fieldable entities. Neat!

Here's how it works, let's say we want to expose a welcoming text message as a field for a node, here's how we do that:

/**
 * Implements MODULE_NAME_field_extra_fields().
 */
function hook_field_extra_fields() {
  $extra['node']['article']['welcome_message'] = array(
    'label' => t('Welcome message'),
    'description' => t('A welcome message'),
    'weight' => 0,
  );
  return $extra;
}

As you see in example above, we used hook_field_extra_fields() to define an extra field for an enity type of 'node' and 'article' bundle (content type). You can actually choose any other type of entity that's available on your system (think user, taxonomy_term, profile2, etc).

Now if you'll clear your cache and go to display settings for Node -> Article you should see 'A welcome message' field available.

Ok the last bit is to actually force our "extra" field to output some data, we do this in hook_node_view:

/**
 * Implements hook_node_view().
 */
function MODULE_NAME_node_view($node, $view_mode, $langcode) {
  // Only show the field for node of article type
  if ($node->type == 'article') {
    $node->content['welcome_message'] = array(
      '#markup' => 'Hello and welcome to our Drupal site!',
    );
  }
}

That should be all. Now you should see a welcome message on your node oage. Please note, if you're adding an extra field to another entity type (like, taxonomy_term for example), you should do the last bit in this entity's _view() hook.

UPDATE: I put code snippets for this tutorial at dropbucket.org here: http://dropbucket.org/node/1398

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

 
Giới thiệu ví dụ đơn giản AJAX Example trong Drupal 6, Drupal 7

Giới thiệu ví dụ đơn giản AJAX Example trong Drupal 6, Drupal 7

It should go without saying but I find many of those asking for help on ajax are not sure what to do with all the hooks and such necessary to implement an ajax callback.

Các nhà nghiên cứu phá thành công khóa mã hóa an toàn bằng âm thanh

Các nhà nghiên cứu phá thành công khóa mã hóa an toàn bằng âm thanh

Mới đây, các nhà nghiên cứu tại đại học Tel Aviv và viện khoa học Weizmann (Israel) đã thành công trong việc giải mã một trong những thuật toán mã hóa an toàn nhất hiện nay là RSA 4096 bit bằng cách dùng microphone để lắng nghe một chiếc máy tính khi nó giải mã dữ liệu đã mã hóa.

ZAGGkeys FLEX – bàn phím không dây siêu đẹp cho máy tính bảng

ZAGGkeys FLEX – bàn phím không dây siêu đẹp cho máy tính bảng

Được ra đời với mục đích dành riêng cho máy tính bảng, thiết bị này còn sở hữu pin tự sạc cho thời gian sử dụng lâu hơn.

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

 

Diet con trung