创建自定义令牌

时间:2020-05-11 07:39:58

标签: drupal-8

我是Drupal的新手。我使用Drupal8。我将详细描述: 1.我们具有材料类型-“汽车模型”。其中有一个字段“ field_car_model”,该字段引用了分类词典“汽车的品牌和型号”。分类词典-3个级别,

例如:

Hyundai
Creta
I generation (2018-2020)
I use the Automatic Label module to automatically create a title in new material.
In the "Car Model" in the title prescribed:
[node: field_car_model: entity: root] [node: field_car_model: entity: parent] [node: field_car_model: entity: name]

结果,当创建一个新节点“ Car Model”时,我得到了标题: 现代Creta I一代(2018-2020) 我需要没有(2018-2020)的头衔-现代Creta I一代。 由于在创建新内容时继续工作非常不方便,因此无法在术语本身中删除发行年份。 决定编写一个自定义令牌,该令牌会在接收到分类术语名称时使用例如preg_match函数删除(2018-2020)。我写了test_token模块:

/ **
* Implements hook_token_info ().
* /
function test_token_token_info () {
$ node ['field_car_model: entity: name-without-years'] = [
'name' => t ('Title without years of car model'),
'description' => t ('Returns the title without years of car model.'),
];
return [
'tokens' => ['node' => $ node],
];
}

/ **
* Implements hook_tokens ().
* /
function test_token_tokens ($ type, $ tokens, array $ data, array $ options, \ Drupal \ Core \ Render \ BubbleableMetadata $ bubbleable_metadata) {
$ replacements = [];
if ($ type == 'node' &&! empty ($ data ['node'])) {
/ ** @var \ Drupal \ node \ NodeInterface $ node * /
$ node = $ data ['node'];

foreach ($ tokens as $ name => $ original) {
switch ($ name) {
case 'field_car_model: entity: name-without-years':
$ replacements [$ original] = 'new test token';
break;
}
}
}

return $ replacements;
}

很明显,在这种情况下,我只是为文本分配了最后一个值。 我缺乏对Drupal的了解。许多示例都使用简单的示例,例如日期格式或文本分配。实际上有两个问题: 1.如何将令牌插入任何类型,而不仅限于类型节点?我必须在考虑层次结构的情况下选择此令牌,以便它处于同一级别,其中标准令牌为[节点:field_car_model:实体:名称] 我认为是这样,但知识不足:

function test_token_token_info () {
$ node ['name-without-years'] = [
'name' => t ('Title without years of car model'),
'description' => t ('Returns the title without years of car model.'),
];

return [
'tokens' => ['node' => 'field_car_model' => 'entity' => $ node],
];
}

也许是这样(根据drupal.org上Drupal 7的描述):

$ node ['field_car_model'] ['entity'] ['name-without-years'] = [
'name' => t ('Title without years of car model'),
'description' => t ('Returns the title without years of car model.'),
];
return [
'tokens' => ['node' => $ node],
];

https://i.stack.imgur.com/dIfRY.jpg 必要 https://i.stack.imgur.com/bHUNc.jpg

当然,我在这里读过:https://opensenselabs.com/blogs/tech/how-create-custom-token-drupal-8-但是,作为一个初学者,我还是不明白-如果有令牌链,那就太困难了。我也在国外网站上读过它-但是同样,最常见的情况是简单的案例,如果更难理解,我就会理解。

有人可以写下我该如何获得术语分类法的名称并从中删除发布年份????

0 个答案:

没有答案