คุณจะทำอย่างไรเกี่ยวกับการสร้างโทเค็นโดยทางโปรแกรม ฉันต้องการเพิ่มโทเค็นที่กำหนดเองสำหรับโมดูลของฉัน
คุณจะทำอย่างไรเกี่ยวกับการสร้างโทเค็นโดยทางโปรแกรม ฉันต้องการเพิ่มโทเค็นที่กำหนดเองสำหรับโมดูลของฉัน
คำตอบ:
ใน Drupal 6 hook_token_values()คุณใช้
เบ็ดนี้จะช่วยให้คุณสร้างโทเค็น คุณสามารถสร้างพวกเขาในขอบเขตส่วนกลางหรือคุณสามารถใช้วัตถุเช่นโหนดหรือผู้ใช้ในการเพาะค่า
คุณควรใช้hook_token_list()เพื่ออธิบายว่าโทเค็นของคุณคืออะไร
token.apiเอกสารค่อนข้างชัดเจน
function my_user_token_values($type, $object = NULL, $options = array()) {
  if ($type == 'user') {
    $user = $object;
    $tokens['name']      = $user->name;
    $tokens['mail']      = $user->mail;
    return $tokens;
  }
}
ฉันจะไม่ X โพสต์ทุกสิ่ง แต่ควรให้ความคิดระดับสูงแก่คุณ
ใน Drupal 7 รหัสสำหรับจัดการโทเค็นเป็นส่วนหนึ่งของโมดูลหลักของ Drupal
hooks โมดูลโทเค็นจำเป็นต้องใช้คือ:
โมดูลอื่น ๆ สามารถปรับเปลี่ยนการใช้งานโทเค็นให้จากโมดูลที่ใช้hook_token_info_alter ()และhook_tokens_alter ()
แตกต่างจากโมดูล Token รหัสในแกน Drupal อนุญาตให้สร้างเนื้อหาของโทเค็นเมื่อจำเป็นเท่านั้นเท่านั้น ใน Drupal 6 โมดูลโทเค็นจะขอให้โมดูลที่ใช้โทเค็นค่าทั้งหมดสำหรับโทเค็นของพวกเขาโดยใช้hook_token_values(); นี่หมายความว่าโมดูลสามารถคำนวณค่าสำหรับโทเค็นซึ่งไม่จำเป็นสำหรับโทเค็นที่ถูกแทนที่ ใน Drupal 7 การดำเนินการของhook_tokens()รับ$tokensอาร์เรย์ของโทเค็นที่จะถูกแทนที่เป็นอาร์กิวเมนต์ โมดูลนั้นสามารถคำนวณค่าของโทเค็นโดยที่รู้ว่ามันจะถูกใช้
ฟังก์ชันที่ใช้ใน Drupal 7 เพื่อแทนที่โทเค็นด้วยค่าคือtoken_replace ()ซึ่งเป็นฟังก์ชันเดียวที่ใช้เพื่อแทนที่โทเค็นด้วยค่าของพวกเขา
ข้อแตกต่างอื่น ๆ ระหว่างโมดูล Token สำหรับ Drupal 6 และรหัสใน Drupal 7 คือ:
hook_tokens()พารามิเตอร์รับที่บอกกับตะขอเมื่อเนื้อหาของโทเค็นจะต้องมีการฆ่าเชื้อ; เมื่อค่าโทเค็นไม่จำเป็นต้องได้รับการปรุงแต่งเนื้อหาไม่ได้ส่งผ่านไปยังฟังก์ชั่นหรือcheck_plain()filter_xss()ผมอยากจะเพิ่มโทเค็นใหม่กับข้อมูลเว็บไซต์ส่วนของสัญญาณเรียกชื่อเมือง นี่คือวิธีที่ฉันทำใน Drupal 7
 /**
 * Implements hook_token_info().
 */
function my_module_token_info() {
  // Add tokens.
  $site['city_name'] = array(
    'name' => t('Token Name'),
    'description' => t('Token Description'),
  );
  return array(
    'tokens' => array(
      'site' => $site,
    ),
  );
}
/**
 * Implements hook_tokens().
 */
function my_module_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
 if ($type == 'site') {
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'city_name':
          $city_name = variable_get('city_name');
          $replacements[$original] = $sanitize ? check_plain($city_name) : $city_name;
          break;
      }
    }
  }
  // Return the replacements.
  return $replacements;
}
              [site:city_name]. ตรวจสอบให้แน่ใจว่าคุณล้างแคชหรือรีสตาร์ท memcached หากใช้
                    $sanitizeในตัวอย่างข้างต้นไม่ได้ถูกกำหนดดังนั้นคุณจะได้รับNotice: Undefined variableสิ่งนั้น
                    สำหรับ Drupal 8 ตัวอย่างการใช้วัตถุโหนด:
คุณสามารถใส่โทเค็นในโมดูลของคุณที่ mymodule.tokens.inc โดยใช้ hook_token_info () เพื่อลงทะเบียนและ hook_tokens () สำหรับข้อมูลการแทนที่
หากคุณต้องการสร้างโทเค็นที่กำหนดเองสำหรับประเภทโทเค็นที่มีอยู่เช่นสำหรับโหนดคุณจะต้องใส่โทเค็นของคุณไว้ในระบบย่อยภายใน hook_token_info () อ้างถึง node.tokens.inc ในโมดูลโหนดเพื่อดูสิ่งที่คุณกำลังสร้าง
mymodule.tokens.inc:
<?php
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\image\Entity\ImageStyle;
/**
 * Implements hook_token_info().
 */
function mymodule_token_info() {
  $info = array();
  $info['tokens']['node']['custom_title'] = [
    'name' => t("Custom Title"),
    'description' => t("a custom node title token"),
  ];
  // Return them.
  return $info;
}
/**
 * Implements hook_tokens().
 */
function mymodule_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = array();
  if ($type == 'node') {
    foreach ($tokens as $name => $original) {
      // Find the desired token by name
      switch ($name) {
        case '$data['node']':
          $node = $data['node'];
          $replacements[$original] = $node->label();
          break;
      }
    }
  }
  // Return the replacements.
  return $replacements;
}
              สำหรับ Drupal 8
// We need to include the needed class for tokens.
use Drupal\Core\Render\BubbleableMetadata;
/**
 * Implements hook_token_info().
 */
function modulename_token_info() {
  $info = array();
  // Add any new tokens.
  $info['tokens']['customtokentype']['customtoken'] = t('Telling drupal that you define custom token');
  // Return them.
  return $info;
}
/**
 * Implements hook_tokens().
 */
function modulename_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = array();
  $simple = $data["customanything"];
  if ($type == 'customtokentype') {
    foreach ($tokens as $name => $original) {
      // Find the desired token by name
      switch ($name) {
        case 'customtoken':
          $new = $simple;
          $replacements[$original] = $new;
          break;
      }
    }
  }   
  // Return the replacements.
  return $replacements;
}
การรับค่าโทเค็นในฟังก์ชั่นของคุณต้องใช้รหัสที่คล้ายกับรหัสต่อไปนี้
$token = \Drupal::token();
$message_html = "hello my custom token is replaced see it here [customtokentype:customtoken]";
// Token data.
$data = array('customanything' => $tosendtotokens);
$message_html = $token->replace($message_html, $data);
              newและเป็นsimpleอย่างไร