Hướng dẫn tạo riêng cho bạn 1 tokens trong Drupal 7

Hướng dẫn tạo riêng cho bạn 1 tokens trong Drupal 7

>> Hướng dẫn thêm 1 pseudo field trong Drupal 7

>> Hướng dẫn testing Drupal data migrations với CasperJS

>> Hướng dẫn sử dụng Entityqueue with Views trong Drupal 7

Tokens are a pretty powerful weapon to have in your arsenal, and they actually come in useful a lot if you remember that they're there.

Hướng dẫn tạo riêng cho bạn 1 tokens trong Drupal 7

If you haven't used them before, tokens are essentially text placeholders - they can be static text, variables, field values, whatever you want really.

The Token API is now part of Drupal 7 core and as it turns out, using it to create your own tokens is super easy - you just need a couple of hooks.

The first hook, hook_token_info(), is used to declare any custom tokens.

/**
 * Implements hook_token_info().
 */
function module_name_token_info() {
  $info = array();
  // Define a new token type.
  $info['types']['my_custom_tokens'] = array(
    'name' => t('My custom tokens'),
    'description' => t('A token type for my custom tokens.'),
  );
  // Define any new tokens.
  $info['tokens']['my_custom_tokens']['my_cool_token'] = array(
    'name' => t('My cool token'),
    'description' => t('A token I use to show that I am cool.'),
  );
  return $info;
}

We are creating our own custom token type here too but if you just want to put your token in an already existing type, you can get a list of the existing tokens and types by calling token_get_info().

Now that our new token is good and defined, we just need to give it some content with hook_tokens().

/**
 * Implements hook_tokens().
 */
function module_name_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();

  if ($type == 'my_custom_tokens') {
    // Loop through the available tokens.
    foreach ($tokens as $name => $original) {
      // Find our custom tokens by name.
      switch ($name) {
        case 'my_cool_token':
          // Work out the value of our token.
          $value = 'Whatever we want the token to be';
          // Give our token it's value!
          $replacements[$original] = $value;
          break;
      }
    }
  }

  return $replacements;
}

That's it! Our token should now be available and appear in the list of tokens wherever tokens are available.

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

 
Những nết nổi bật của phiên bản Joomla 3.0

Những nét nổi bật của phiên bản Joomla 3.0

Joomla là một trong những CMS dùng để thiết kế website thông dụng và mạnh mẽ nhất hiện nay. Ngày 27/09, phiên bản joomla 3.0.0 đã chính thức được ra mắt. Đây là phiên bản được phát hành theo cơ chế STS “Standard Term Support”.

Facebook thanh lọc, hàng loạt fanpage giải trí “chết nhăn răng”

Từ chiều ngày hôm 13/1, cộng đồng admin fanpage Việt Nam nháo nhào khi hàng loạt fanpage lớn bị khóa không rõ lí do. Những fanpage bị block trong đợt thanh lọc này có lượng like từ vài chục nghìn cho tới cả gần triệu lượt like.

Hướng dẫn sử dụng Drush thực hiện automate các task

Hướng dẫn sử dụng Drush thực hiện automate các task

Drush Make is well-known as an advanced tool for Drupal distribution building. But it also can be very useful for those who have never dealt with distributions. 

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

 

Diet con trung