|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file |
| 5 | + * Implements tokens for apigee_edge_teams module. |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * Copyright 2020 Google Inc. |
| 10 | + * |
| 11 | + * This program is free software; you can redistribute it and/or |
| 12 | + * modify it under the terms of the GNU General Public License |
| 13 | + * version 2 as published by the Free Software Foundation. |
| 14 | + * |
| 15 | + * This program is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU General Public License |
| 21 | + * along with this program; if not, write to the Free Software |
| 22 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 23 | + * MA 02110-1301, USA. |
| 24 | + */ |
| 25 | + |
| 26 | +use Drupal\Core\Render\BubbleableMetadata; |
| 27 | +use Drupal\Core\Url; |
| 28 | + |
| 29 | +/** |
| 30 | + * Implements hook_token_info_alter(). |
| 31 | + */ |
| 32 | +function apigee_edge_teams_token_info_alter(&$info) { |
| 33 | + if (!empty($info['tokens']['team_invitation'])) { |
| 34 | + $info['tokens']['team_invitation']['team_name'] = [ |
| 35 | + 'name' => t('Team name'), |
| 36 | + 'description' => t('The name of the team.'), |
| 37 | + 'type' => 'team_invitation', |
| 38 | + ]; |
| 39 | + $info['tokens']['team_invitation']['url_accept'] = [ |
| 40 | + 'name' => t('Accept URL'), |
| 41 | + 'description' => t('The invitation accept url.'), |
| 42 | + 'type' => 'team_invitation', |
| 43 | + ]; |
| 44 | + $info['tokens']['team_invitation']['url_decline'] = [ |
| 45 | + 'name' => t('Decline URL'), |
| 46 | + 'description' => t('The invitation decline url.'), |
| 47 | + 'type' => 'team_invitation', |
| 48 | + ]; |
| 49 | + $info['tokens']['team_invitation']['url_register'] = [ |
| 50 | + 'name' => t('Registration URL'), |
| 51 | + 'description' => t('A url to register an account with a redirect to accept the team invitation.'), |
| 52 | + 'type' => 'team_invitation', |
| 53 | + ]; |
| 54 | + $info['tokens']['team_invitation']['expiry_days'] = [ |
| 55 | + 'name' => t('Expiry days'), |
| 56 | + 'description' => t('The expiry days for the team invitation.'), |
| 57 | + 'type' => 'team_invitation', |
| 58 | + ]; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Implements hook_tokens(). |
| 64 | + */ |
| 65 | +function apigee_edge_teams_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) { |
| 66 | + if (!isset($data['team_invitation'])) { |
| 67 | + return []; |
| 68 | + } |
| 69 | + |
| 70 | + /** @var \Drupal\apigee_edge_teams\Entity\TeamInvitationInterface $team_invitation */ |
| 71 | + $team_invitation = $data['team_invitation']; |
| 72 | + $team = $team_invitation->getTeam(); |
| 73 | + $replacements = []; |
| 74 | + |
| 75 | + foreach ($tokens as $field_name => $original) { |
| 76 | + switch ($field_name) { |
| 77 | + case "team_name": |
| 78 | + $replacements[$original] = $team->label(); |
| 79 | + break; |
| 80 | + |
| 81 | + case "url_accept": |
| 82 | + $replacements[$original] = $team_invitation->toUrl('accept-form', ['absolute' => TRUE])->setRouteParameter('team', $team->id())->toString(); |
| 83 | + break; |
| 84 | + |
| 85 | + case "url_decline": |
| 86 | + $replacements[$original] = $team_invitation->toUrl('decline-form', ['absolute' => TRUE])->setRouteParameter('team', $team->id())->toString(); |
| 87 | + break; |
| 88 | + |
| 89 | + case "url_register": |
| 90 | + $query = ['destination' => $team_invitation->toUrl('accept-form')->setRouteParameter('team', $team->id())->toString()]; |
| 91 | + $replacements[$original] = Url::fromRoute('user.register', [], ['query' => $query, 'absolute' => TRUE])->toString(); |
| 92 | + break; |
| 93 | + |
| 94 | + case "expiry_days": |
| 95 | + $replacements[$original] = Drupal::config('apigee_edge_teams.team_settings')->get('team_invitation_expiry_days'); |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return $replacements; |
| 101 | +} |
0 commit comments