Creating a new token needing particular context for block visibility groups
-
drupal 9 add token for nodes to blocks
-
drupal 9 creating new token
-
hook_tokens
only seeingsite
,date
-
run hook_tokens after token module
-
drupal 9 order hooks
Most important thing to know when developing and testing your token hooks is that you will not see any tokens that are not actually present. So when i expected to see every possible token, and only saw site and date, i thought it was because those other tokens were not defined yet and i’d have to move my hook implementation to occur after they were. No, not at all— i cannot see even the types and tokens i have defined, unless they actually get used on the page i am visiting.
Cannot believe i needed this, i’m sure there’s a better way to list config entities but uh not any that are documented or quickly discoverable.
And i trust what Benji says.
$groups_config = \Drupal::service('config.storage')->listAll('block_visibility_groups.block_visibility_group');
block_visibility_groups.block_visibility_group.
- php array_filter use object method name
- php array_filter anonymous function
- array_filter
- array_map str
- php trim each string in array
- php function on each array
- drupal 8 load entities
$groups_config = \Drupal::service('config.storage')->listAll('block_visibility_groups.block_visibility_group.');
$groups = array_map(function($group) { return substr($group, 47); }, $groups_config);
$evaluator = \Drupal::service('block_visibility_groups.group_evaluator');
$active_groups = array_filter($groups, function($group_name) use ($evaluator) {
$group = \Drupal\block_visibility_groups\Entity\BlockVisibilityGroup::load($group_name);
return $evaluator->evaluateGroup($group);
});
// TODO look into using token_render_array or flagging that it should
// work with this when it lands (but do not token module a dependency):
// https://www.drupal.org/project/token/issues/3115486
$replacements[$original] = implode(',', $active_groups);
- drupal 8 chained token multiple entity reference
- defining tokens that can join drupal 9
- drupal 9 field token multiple value
Provide array of primitive property values on multivalue field [#3115486] | Drupal.org
- provide region from module drupal 8 hook_alter_regions