- Issue created by @damienmckenna
- πΊπΈUnited States damienmckenna NH, USA
Temporary solution for the body field:
<?php
/**
* Implements hook_token_info().
*/
function mymodule_token_info() {
$info['tokens']['node']['body-wordcount'] = [
'name' => t('Body field word count'),
'description' => t('The word count for the body field.'),
];return $info;
}/**
* Implements hook_tokens().
*/
function mymodule_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];// Node tokens.
if ($type == 'node' && !empty($data['node'])) {
$node = $data['node'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'body-wordcount':
if (isset($node->body->value) && !empty($node->body->value)) {
$text = strip_tags($node->body->value);
$replacements[$original] = str_word_count($text);
}
break;
}
}
}return $replacements;
}The wordcount logic itself is simple, the question is how to make a subtoken for text fields?
- πΊπΈUnited States damienmckenna NH, USA
The Smart Trim has some token logic that could be repurposed to handle what we need.
- πΊπΈUnited States damienmckenna NH, USA
A fixed patch, after some local testing.
- Merge request !102Added -wordcount token for each string, text field. β (Open) created by damienmckenna
- πΊπΈUnited States damienmckenna NH, USA
Related: π Fix tests on 8.x-1.x branch Active