? purl-canonical-url.patch
Index: purl.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/purl/purl.module,v
retrieving revision 1.1.2.43
diff -u -p -r1.1.2.43 purl.module
--- purl.module	27 May 2010 02:14:21 -0000	1.1.2.43
+++ purl.module	15 Jun 2010 20:13:49 -0000
@@ -136,6 +136,42 @@ function purl_init() {
 }
 
 /**
+ * Implementation of hook_preprocess_page(). Add a canonical link tag if one is set.
+ *
+ * This is done here rather than through a drupal_set_html_head() call to detect whether
+ * other modules - e.g., nodewords - have already set one.
+ */
+function purl_preprocess_page(&$vars) {
+  if ($tag = purl_canonical()) {
+    // Ensure there is not already a canonical tag set.
+    if (strpos($vars['head'], '<link rel="canonical"') === FALSE) {
+      $url = url($tag['path'], $tag['options']);
+      $vars['head'] = '<link rel="canonical" href="' . $url . '" />' . "\n" . $vars['head'];
+    }
+  }
+}
+
+/**
+ * Set or get a canonical URL for the current path.
+ *
+ * @param $tag
+ *   Array of data on the canonical url of the current page. Keys
+ *   are 'path' (the path of the page) and 'options' (array of options
+ *   to be passed to url()). Usually options will include a purl key, e.g.
+ *   <code>array('purl' => array('id' => $id, 'provider' => $provider))</code>
+ * @return Array representing previously set cononical tag, if any.
+ */
+function purl_canonical($tag = NULL) {
+  static $cache;
+
+  if (!empty($tag)) {
+    $cache = $tag;
+  }
+
+  return $cache;
+}
+
+/**
  * Remove the language prefix for a given path.
  * PURL implements this itself as language_initialize() directly affects
  * $_GET['q'] and cannot be reused.
