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

 
Diệt ruồi muỗi trong chăn nuôi tháng 06 năm 2016

Diệt ruồi muỗi trong chăn nuôi tháng 06 năm 2016

Sản xuất "thịt sạch" và tạo vùng chăn nuôi an toàn không chỉ có ý nghĩa tăng tính cạnh tranh, hội nhập mà còn có ý nghĩa thiết thực

D8 to D3: Using Drupal for data visualization

D3 JavaScript library : Làm Drupal for data visualization

One of the exciting features that’s on its way in Drupal 8 is the ability to use Drupal as a RESTful web service. This means that Drupal 8 core allows you to expose data to external applications in a number of standardized formats

Seo top

Báo giá quảng cáo Google SEO

Hiện Express Magazine đang thực hiện chương trình khuyến mãi dịch vụ ( SEO Miễn Phí ) top Google cho khách hàng nào có nhu cầu muốn quảng bá website qua dịch vụ seo trên google

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

 

Diet con trung