-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add in other dynamic/indirect tokens.
- Loading branch information
1 parent
c5e16c1
commit 0c684ad
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
--- | ||
allow_compound_display_for_non_compound: FALSE | ||
display_non_compound_compound_as_first_member_of_itself: FALSE | ||
member_parameters: | ||
- active_member |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Post-update hooks. | ||
*/ | ||
|
||
/** | ||
* Implements hook_post_update_NAME(). | ||
*/ | ||
function dgi_members_post_update_add_params(&$sandbox) { | ||
$config = \Drupal::configFactory()->getEditable('dgi_members.settings'); | ||
$config->set('member_parameters', [ | ||
'active_member', | ||
]); | ||
$config->save(TRUE); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Token hooks. | ||
*/ | ||
|
||
use Drupal\Core\Render\BubbleableMetadata; | ||
|
||
/** | ||
* Implements hook_token_info(). | ||
*/ | ||
function dgi_members_token_info() { | ||
$config = \Drupal::config('dgi_members.settings'); | ||
|
||
$parameter_tokens = []; | ||
foreach ($config->get('member_parameters') as $parameter) { | ||
$parameter_tokens[_dgi_members_get_token_name($parameter)] = [ | ||
'name' => t('Active member from @parameter (dgi_members/Compound)', [ | ||
'@parameter' => $parameter, | ||
]), | ||
'type' => 'node', | ||
]; | ||
} | ||
|
||
return [ | ||
'tokens' => [ | ||
'node' => $parameter_tokens, | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Implements hook_tokens(). | ||
*/ | ||
function dgi_members_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) { | ||
if ($type != 'node') { | ||
return; | ||
} | ||
|
||
$output = []; | ||
|
||
$config = \Drupal::config('dgi_members.settings'); | ||
/** @var \Drupal\dgi_members\DgiMembersEntityOperationsInterface $member_service */ | ||
$member_service = \Drupal::service('dgi_members.entity_service'); | ||
foreach ($config->get('member_parameters') as $parameter) { | ||
$mapped = _dgi_members_get_token_name($parameter); | ||
$prefix = "{$mapped}:"; | ||
$to_chain = []; | ||
foreach ($tokens as $key => $value) { | ||
if (str_starts_with($key, $prefix)) { | ||
$to_chain[substr($key, strlen($prefix))] = $value; | ||
} | ||
} | ||
|
||
if ($to_chain) { | ||
$bubbleable_metadata->addCacheContexts([ | ||
"url.query_args:{$parameter}", | ||
]); | ||
$output += \Drupal::token()->generate( | ||
$type, | ||
$to_chain, | ||
['node' => $member_service->retrieveActiveMember($parameter)] + $data, | ||
$options, | ||
$bubbleable_metadata, | ||
); | ||
} | ||
} | ||
|
||
return $output; | ||
} | ||
|
||
function _dgi_members_get_token_name($parameter) { | ||
return "dgi_member__{$parameter}"; | ||
} |