From 157fc8e1a45841f9d8ade2ec1c88a7eac1cb691d Mon Sep 17 00:00:00 2001 From: William Hearn Date: Mon, 11 Apr 2016 10:38:14 -0400 Subject: [PATCH] UUID Redirect diff from latest stable 1.0-alpha1. --- uuid_redirect.api.php | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ uuid_redirect.module | 54 ++++++++++++++++++++++++++++++++-------- 2 files changed, 112 insertions(+), 10 deletions(-) create mode 100644 uuid_redirect.api.php diff --git a/uuid_redirect.api.php b/uuid_redirect.api.php new file mode 100644 index 0000000..b2356e6 --- /dev/null +++ b/uuid_redirect.api.php @@ -0,0 +1,68 @@ +{$bundle_key}) && !in_array($entity->{$bundle_key}, $menu_info['bundle restrictions'])) { + return TRUE; + } + } + } +} + +/** + * @} End of "addtogroup hooks". + */ diff --git a/uuid_redirect.module b/uuid_redirect.module index 50b5f6f..9248fd3 100644 --- a/uuid_redirect.module +++ b/uuid_redirect.module @@ -33,6 +33,20 @@ function uuid_redirect_menu() { } /** + * Implements hook_module_implements_alter(). + */ +function uuid_redirect_module_implements_alter(&$implementations, $hook) { + // Our hook_menu_alter() implementation needs to run after any others, since + // we are trying to completely take over certain URLs and redirect them to + // another site. + if ($hook == 'menu_alter') { + $group = $implementations['uuid_redirect']; + unset($implementations['uuid_redirect']); + $implementations['uuid_redirect'] = $group; + } +} + +/** * Implements hook_menu_alter(). * * Alter any menu callbacks that need to be redirected to an external URL. @@ -134,19 +148,17 @@ function uuid_redirect_to_external_site() { } $entity_types_by_load_function = _uuid_redirect_entity_types_by_load_function(); if (isset($entity_types_by_load_function[$info['load function']])) { + // Allow modules to skip the redirect for this entity; see, for + // example, uuid_redirect_uuid_redirect_skip_redirect(). For + // consistency we always invoke the hook for all entities in the path, + // but if any module indicates that the redirect should be skipped, we + // honor that regardless of what happens later. $entity_type = $entity_types_by_load_function[$info['load function']]; - $entity_info = entity_get_info($entity_type); - // Do not redirect if we are only redirecting certain bundles and this - // isn't one of them. - if (!empty($info['bundle restrictions']) && isset($entity_info['entity keys']['bundle'])) { - $bundle_key = $entity_info['entity keys']['bundle']; - if (isset($entity->{$bundle_key}) && !in_array($entity->{$bundle_key}, $info['bundle restrictions'])) { - $skip_redirect = TRUE; - break; - } - } + $responses = module_invoke_all('uuid_redirect_skip_redirect', $entity_type, $entity, $info); + $skip_redirect = $skip_redirect || in_array(TRUE, $responses, TRUE); // Perform the appropriate UUID replacement. $uuid_key_type = $info['revision'] ? 'revision uuid' : 'uuid'; + $entity_info = entity_get_info($entity_type); if (isset($entity_info['entity keys'][$uuid_key_type])) { $uuid_key = $entity_info['entity keys'][$uuid_key_type]; if (isset($entity->{$uuid_key})) { @@ -173,6 +185,23 @@ function uuid_redirect_to_external_site() { } /** + * Implements hook_uuid_redirect_skip_redirect(). + */ +function uuid_redirect_uuid_redirect_skip_redirect($entity_type, $entity, $menu_info) { + // Do not redirect if we are only redirecting certain bundles and this isn't + // one of them. + if (!empty($menu_info['bundle restrictions'])) { + $entity_info = entity_get_info($entity_type); + if (isset($entity_info['entity keys']['bundle'])) { + $bundle_key = $entity_info['entity keys']['bundle']; + if (isset($entity->{$bundle_key}) && !in_array($entity->{$bundle_key}, $menu_info['bundle restrictions'])) { + return TRUE; + } + } + } +} + +/** * Implements hook_menu_get_item_alter(). * * Replace any UUIDs in the current path with entity IDs, and, if any @@ -238,5 +267,10 @@ function _uuid_redirect_entity_types_by_load_function() { foreach (entity_get_info() as $type => $info) { $entity_types[$info['load hook']] = $type; } + // Special case for taxonomy terms, since they have an additional menu load + // function which is not found in hook_entity_info(). + if (isset($entity_types['taxonomy_vocabulary_load']) && !isset($entity_types['taxonomy_vocabulary_machine_name_load'])) { + $entity_types['taxonomy_vocabulary_machine_name_load'] = $entity_types['taxonomy_vocabulary_load']; + } return $entity_types; } -- 2.3.8 (Apple Git-58)