|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2020 Google Inc. |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU General Public License |
| 8 | + * version 2 as published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 18 | + * MA 02110-1301, USA. |
| 19 | + */ |
| 20 | + |
| 21 | +use Drupal\apigee_edge\Entity\DeveloperAppInterface; |
| 22 | +use Drupal\apigee_edge_actions\Event\EdgeEntityEventEdge; |
| 23 | +use Drupal\apigee_edge\Entity\AppInterface; |
| 24 | +use Drupal\apigee_edge_actions\Plugin\RulesAction\SystemMailToUsersOfRole; |
| 25 | +use Drupal\Core\Entity\EntityInterface; |
| 26 | + |
| 27 | +/** |
| 28 | + * Implements hook_entity_insert(). |
| 29 | + */ |
| 30 | +function apigee_edge_actions_entity_insert(EntityInterface $entity) { |
| 31 | + _apigee_edge_actions_dispatch_entity_event($entity, 'insert'); |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Implements hook_entity_delete(). |
| 36 | + */ |
| 37 | +function apigee_edge_actions_entity_delete(EntityInterface $entity) { |
| 38 | + _apigee_edge_actions_dispatch_entity_event($entity, 'delete'); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Implements hook_entity_update(). |
| 43 | + */ |
| 44 | +function apigee_edge_actions_entity_update(EntityInterface $entity) { |
| 45 | + _apigee_edge_actions_dispatch_entity_event($entity, 'update'); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Implements hook_archiver_info_alter(). |
| 50 | + */ |
| 51 | +function apigee_edge_actions_rules_action_info_alter(&$info) { |
| 52 | + // Override the class for this rule action to handle param upcasting. |
| 53 | + // @see https://www.drupal.org/project/rules/issues/2800749 |
| 54 | + $info['rules_email_to_users_of_role']['class'] = SystemMailToUsersOfRole::class; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Helper to dispatch an entity event. |
| 59 | + * |
| 60 | + * @param \Drupal\Core\Entity\EntityInterface $entity |
| 61 | + * The entity. |
| 62 | + * @param string $event_name |
| 63 | + * The event name. |
| 64 | + * |
| 65 | + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException |
| 66 | + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException |
| 67 | + */ |
| 68 | +function _apigee_edge_actions_dispatch_entity_event(EntityInterface $entity, string $event_name) { |
| 69 | + if (!Drupal::service('apigee_edge_actions.edge_entity_type_manager') |
| 70 | + ->isFieldableEdgeEntityType($entity->getEntityType())) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + $dispatched_event_name = "apigee_edge_actions_entity_$event_name:{$entity->getEntityTypeId()}"; |
| 75 | + |
| 76 | + $arguments = [ |
| 77 | + $entity->getEntityTypeId() => $entity |
| 78 | + ]; |
| 79 | + |
| 80 | + // Note: Refactor this to plugins if more entity types requires custom |
| 81 | + // arguments. |
| 82 | + if ($entity instanceof AppInterface) { |
| 83 | + if ($entity instanceof DeveloperAppInterface) { |
| 84 | + // $entity->getCreatedBy() is deprecated, so to get the developer Drupal |
| 85 | + // account we need to load the developer by UUID, then load the user by |
| 86 | + // email. |
| 87 | + // Note: $entity->getAppOwner() returns a developer UUID, which is |
| 88 | + // different from a user's UUID, so we load the developer first and then |
| 89 | + // the account. |
| 90 | + $developer = Drupal::entityTypeManager() |
| 91 | + ->getStorage('developer') |
| 92 | + ->load($entity->getAppOwner()); |
| 93 | + $user_id = $developer->getEmail(); |
| 94 | + } |
| 95 | + else { |
| 96 | + /** @var \Drupal\apigee_edge_teams\Entity\TeamAppInterface $entity */ |
| 97 | + // For TeamApps, getAppOwner() is a team name, not a developer or email, |
| 98 | + // and we cannot rely on getCreatedBy() as it is deprecated, so we |
| 99 | + // default to the current user for the developer. |
| 100 | + $user_id = Drupal::currentUser()->getEmail(); |
| 101 | + |
| 102 | + // Add the team. |
| 103 | + $team = Drupal::entityTypeManager() |
| 104 | + ->getStorage('team') |
| 105 | + ->load($entity->getAppOwner()); |
| 106 | + $arguments['team'] = $team; |
| 107 | + } |
| 108 | + |
| 109 | + // Add the developer. |
| 110 | + $arguments['developer'] = user_load_by_mail($user_id); |
| 111 | + } |
| 112 | + |
| 113 | + if ($event_name === 'update') { |
| 114 | + $arguments["{$entity->getEntityTypeId()}_unchanged"] = $entity->original; |
| 115 | + } |
| 116 | + |
| 117 | + /** @var \Drupal\apigee_edge\Entity\EdgeEntityInterface $entity */ |
| 118 | + Drupal::service('event_dispatcher') |
| 119 | + ->dispatch($dispatched_event_name, new EdgeEntityEventEdge($entity, $arguments)); |
| 120 | +} |
0 commit comments