Skip to content

Commit

Permalink
Merge branch 'DGIR-115' of https://github.com/MorganDawe/dgi_members
Browse files Browse the repository at this point in the history
…into DGIR-115
  • Loading branch information
MorganDawe committed Feb 13, 2024
2 parents b529456 + 20d5a56 commit 3722127
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/install/dgi_members.settings.yml
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
7 changes: 7 additions & 0 deletions config/schema/dgi_members.schema.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
dgi_members.settings:
type: config_object
label: 'DGI Members Settings'
Expand All @@ -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."
17 changes: 17 additions & 0 deletions dgi_members.post_update.php
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);
}
84 changes: 84 additions & 0 deletions dgi_members.tokens.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?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;
}

/**
* 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}";
}

0 comments on commit 3722127

Please sign in to comment.