Thủ thuật để tạo Custom Display Suite Field trong Drupal 7

Thủ thuật để tạo Custom Display Suite Field trong Drupal 7

Thủ thuật để tạo Custom Display Suite Field trong Drupal 7A few months ago I posted about how to create a custom Panels pane, a critical reference for anyone who uses Panels layouts. The other part of the toolkit for quick and awesome layouts is the Display Suite module. With DS you can create new “Display modes” for your content, to be reused around the site. For example, on one recent site I had four standard ways to display my nodes: Full, Teaser, Mini-Teaser, and Search Result. DS made this configuration a cinch.

But just as in Panels you sometimes need a pane that isn’t provided out of the box, in Display Suite you sometimes want to add a field that isn’t really a field on your content. In a recent site build, I used this capability to include information from the Organic Groups a user belongs to on his profile as it appears in search results.

DS offers some ability to create this kind of custom field through the UI, but I’m talking about more complicated outcomes where you need/want to use custom code instead. This is actually even easier than custom panels panes.

In our example, we will display the user’s name, but backwards. Obviously you can do much more complex things with this, but it’s nice to have a simple example!

Declare your fields

First we have to tell Display Suite about our new custom field. We do this with hook_ds_fields_info().

<?php

//@file: Add a custom suite to display suite for Users.

/**
 * Implements hook_ds_fields_info().
 * Declare my custom field.
 */
function mymodule_ds_fields_info($entity_type) {
  $fields = array();

  if ($entity_type == 'user') {
    $fields['backwards_username'] = array(
      'title' => t('Backwards Username'),
      'field_type' => DS_FIELD_TYPE_FUNCTION,
      'function' => 'mymodule_backwards_username',
    );
  return array($entity_type => $fields);
  }
  return;
}

Any guesses whathappens next? That’s right, we have to write our render function under the name we just declared. You can put anything here, really anything renderable at all.

/**
 * Render function for the Backwards Username field.
 */
function mymodule_backwards_username($field) {
  if (isset($field['entity']->name)) {
    return check_plain(strrev($field['entity']->name));
  }
}

That’s it. So simple, you’ll wonder why you ever did it any other way!

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ả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.

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

 
iPhone, iPad cũng bị cài sẵn phần mềm gián điệp

iPhone, iPad cũng bị cài sẵn phần mềm gián điệp

Apple xác nhận thông tin những phiên bản iOS trước đây đều hỗ trợ phần mềm “gián điệp” Carrier IQ.

Sức mạnh của Contextual Filters trong Drupal Views của Drupal 8

Sức mạnh của Contextual Filters trong Drupal Views của Drupal 8

The small endpoint I built returns an array of cars in sale, with relative information about each car

Chrome 17 - Nhanh và an toàn hơn

Chrome 17 - Nhanh và an toàn hơn

Tháng 2 có vẻ là một tháng bận rộn với Google, không lâu sau khi trình làng phiên bản Chrome cho nền tảng Android, Google đã tiếp tục phát hành phiên bản chính thức của Chrome 17 với nhiều cải tiến thực sự hiệu quả, cả về tốc độ lẫn tính năng.

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

 

Diet con trung