From 7eace29ae19c59800bc3f2b6d0f9944a7070b3bf Mon Sep 17 00:00:00 2001
From: James Harvey <james@webmarkdesigns.com.au>
Date: Thu, 6 Sep 2012 12:48:08 +1000
Subject: [PATCH] Issue #1517348 by skwashd, recidive, jamesharv: Add UUID
 submodule.

---
 redirect_uuid/redirect_uuid.info    |  6 +++
 redirect_uuid/redirect_uuid.install | 38 +++++++++++++++++++
 redirect_uuid/redirect_uuid.module  | 76 +++++++++++++++++++++++++++++++++++++
 3 files changed, 120 insertions(+)
 create mode 100644 redirect_uuid/redirect_uuid.info
 create mode 100644 redirect_uuid/redirect_uuid.install
 create mode 100644 redirect_uuid/redirect_uuid.module

diff --git a/redirect_uuid/redirect_uuid.info b/redirect_uuid/redirect_uuid.info
new file mode 100644
index 0000000..1d9b5ae
--- /dev/null
+++ b/redirect_uuid/redirect_uuid.info
@@ -0,0 +1,6 @@
+name = Redirect UUID
+description = Add UUIDs to redirects which makes them deployable via deploy.
+core = 7.x
+
+dependencies[] = redirect
+dependencies[] = uuid
diff --git a/redirect_uuid/redirect_uuid.install b/redirect_uuid/redirect_uuid.install
new file mode 100644
index 0000000..80b0e35
--- /dev/null
+++ b/redirect_uuid/redirect_uuid.install
@@ -0,0 +1,38 @@
+<?php
+
+/**
+ * @file
+ * Redirect UUID installation and schema hooks.
+ */
+
+/**
+ * Implements hook_schema_alter().
+ */
+function redirect_uuid_schema_alter(&$schema = array()) {
+  $field = uuid_schema_field_definition();
+  $schema['redirect']['fields']['uuid'] = $field;
+}
+
+/**
+ * Implements hook_install().
+ */
+function redirect_uuid_install() {
+  $field = uuid_schema_field_definition();
+  db_add_field('redirect', 'uuid', $field);
+  db_add_index('redirect', 'uuid', array('uuid'));
+}
+
+/**
+ * Implements hook_enable().
+ */
+function redirect_uuid_enable() {
+  uuid_sync_all();
+}
+
+/**
+ * Implements hook_uninstall().
+ */
+function redirect_uuid_uninstall() {
+  db_drop_field('redirect', 'uuid');
+  db_drop_index('redirect', 'uuid');
+}
diff --git a/redirect_uuid/redirect_uuid.module b/redirect_uuid/redirect_uuid.module
new file mode 100644
index 0000000..a8a8477
--- /dev/null
+++ b/redirect_uuid/redirect_uuid.module
@@ -0,0 +1,76 @@
+<?php
+
+/**
+ * @file
+ * Implement UUID/Entity hooks for integrating with deploy and other modules.
+ */
+
+module_load_include('inc', 'uuid', 'uuid');
+
+/**
+ * Implements hook_entity_info_alter().
+ */
+function redirect_uuid_entity_info_alter(&$entity_info) {
+  // Set the properties bellow for redirect entity so UUIDs get created on sync.
+  $entity_info['redirect']['uuid'] = TRUE;
+  $entity_info['redirect']['entity keys']['uuid'] = 'uuid';
+  $entity_info['redirect']['creation callback'] = 'entity_metadata_create_object';
+  $entity_info['redirect']['save callback'] = 'redirect_save';
+  $entity_info['redirect']['deletion callback'] = 'redirect_delete';
+}
+
+/**
+ * Implements hook_entity_uuid_load().
+ */
+function redirect_uuid_entity_uuid_load(&$entities, $entity_type) {
+  if ($entity_type == 'redirect') {
+    foreach ($entities as &$entity) {
+      $m = array();
+      if (preg_match('#^node/(\d+)$#', $entity->redirect, $m)) {
+        $dummy_node = (object) array('nid' => $m[1]);
+        entity_property_id_to_uuid($dummy_node, 'node', 'nid');
+        $entity->redirect = "node/{$dummy_node->nid}";
+      }
+      if (preg_match('#^node/(\d+)$#', $entity->source, $m)) {
+        $dummy_node = (object) array('nid' => $m[1]);
+        entity_property_id_to_uuid($dummy_node, 'node', 'nid');
+        $entity->source = "node/{$dummy_node->nid}";
+      }
+    }
+  }
+}
+
+/**
+ * Implements hook_entity_uuid_presave().
+ */
+function redirect_uuid_entity_uuid_presave(&$entity, $entity_type) {
+  if ($entity_type == 'redirect') {
+    $pattern = '#^node/(' . UUID_PATTERN . ')$#';
+    $m = array();
+    if (preg_match($pattern, $entity->redirect, $m)) {
+      $dummy_node = (object) array('nid' => $m[1]);
+      entity_property_uuid_to_id($dummy_node, 'node', 'nid');
+      $entity->redirect = "node/{$dummy_node->nid}";
+    }
+    if (preg_match($pattern, $entity->source, $m)) {
+      $dummy_node = (object) array('nid' => $m[1]);
+      entity_property_uuid_to_id($dummy_node, 'node', 'nid');
+      $entity->source = "node/{$dummy_node->nid}";
+    }
+  }
+}
+
+/**
+ * Implements hook_form_FORM_ID_alter().
+ */
+function redirect_uuid_form_redirect_edit_form_alter(&$form, &$form_state, $form_id) {
+  if (isset($form_state['build_info']['args'][0])) {
+    $redirect = $form_state['build_info']['args'][0];
+    if (is_object($redirect) && !empty($redirect->uuid)) {
+      $form['uuid'] = array(
+        '#type' => 'value',
+        '#value' => $redirect->uuid,
+      );
+    }
+  }
+}
-- 
1.7.12

