From 0c684adb695dbe203afbb988f8c8c35eb4a2987a Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Fri, 8 Dec 2023 15:25:24 -0400 Subject: [PATCH 1/2] Add in other dynamic/indirect tokens. --- config/install/dgi_members.settings.yml | 3 + config/schema/dgi_members.schema.yml | 7 +++ dgi_members.post_update.php | 17 ++++++ dgi_members.tokens.inc | 75 +++++++++++++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 dgi_members.post_update.php create mode 100644 dgi_members.tokens.inc diff --git a/config/install/dgi_members.settings.yml b/config/install/dgi_members.settings.yml index 7168311..4244327 100644 --- a/config/install/dgi_members.settings.yml +++ b/config/install/dgi_members.settings.yml @@ -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 diff --git a/config/schema/dgi_members.schema.yml b/config/schema/dgi_members.schema.yml index c1a9c23..0dc21a7 100644 --- a/config/schema/dgi_members.schema.yml +++ b/config/schema/dgi_members.schema.yml @@ -1,3 +1,4 @@ +--- dgi_members.settings: type: config_object label: 'DGI Members Settings' @@ -8,3 +9,9 @@ dgi_members.settings: display_non_compound_compound_as_first_member_of_itself: type: boolean label: 'Display compound non-Compound Object as first member of its set.' + member_parameters: + type: sequence + label: 'The URL query parameters for which we will attempt to manage "active member" tokens.' + sequence: + type: string + label: "A URL query parameter of interest." diff --git a/dgi_members.post_update.php b/dgi_members.post_update.php new file mode 100644 index 0000000..e5f83d7 --- /dev/null +++ b/dgi_members.post_update.php @@ -0,0 +1,17 @@ +getEditable('dgi_members.settings'); + $config->set('member_parameters', [ + 'active_member', + ]); + $config->save(TRUE); +} diff --git a/dgi_members.tokens.inc b/dgi_members.tokens.inc new file mode 100644 index 0000000..e43a716 --- /dev/null +++ b/dgi_members.tokens.inc @@ -0,0 +1,75 @@ +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}"; +} From 59473117c6bbb9d76650e088b03369f792ad74db Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Fri, 8 Dec 2023 15:32:44 -0400 Subject: [PATCH 2/2] Coding standards. --- dgi_members.tokens.inc | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/dgi_members.tokens.inc b/dgi_members.tokens.inc index e43a716..1f4d8f7 100644 --- a/dgi_members.tokens.inc +++ b/dgi_members.tokens.inc @@ -70,6 +70,15 @@ function dgi_members_tokens($type, $tokens, array $data, array $options, Bubblea return $output; } -function _dgi_members_get_token_name($parameter) { +/** + * Helper; map URL query parameter to placeholder content. + * + * @param string $parameter + * The URL query parameter to map. + * + * @return string + * The value as it should be in the token placeholder. + */ +function _dgi_members_get_token_name(string $parameter) : string { return "dgi_member__{$parameter}"; }