Added hreflang-tags to other (language) versions of a page
authorMarcel Werk <burntime@woltlab.com>
Tue, 14 Jun 2016 09:29:56 +0000 (11:29 +0200)
committerMarcel Werk <burntime@woltlab.com>
Tue, 14 Jun 2016 09:29:56 +0000 (11:29 +0200)
com.woltlab.wcf/templates/cms.tpl
wcfsetup/install/files/lib/data/page/Page.class.php
wcfsetup/install/files/lib/data/page/PageLanguage.class.php [new file with mode: 0644]

index 6d9651ea4ea3a1f6cb473487e6f5186adcc8b960..55111b784af989a41b7b26af58ea8b4c5edc03af 100644 (file)
@@ -1,5 +1,13 @@
 {capture assign='headContent'}
        <link rel="canonical" href="{$canonicalURL}">
+       
+       {if $page->isMultilingual}
+               {foreach from=$page->getPageLanguages() item='pageLanguage'}
+                       {if $pageLanguage->getLanguage()}
+                               <link rel="alternate" hreflang="{$pageLanguage->getLanguage()->languageCode}" href="{$pageLanguage->getLink()}">
+                       {/if}
+               {/foreach}
+       {/if}
 {/capture}
 
 {include file='header'}
index 8ed5c581e4113659e9c2855b1b05191d96f3e994..6f172b556d65b3e5a4e39631b243ee0c7e3bbd28 100644 (file)
@@ -297,6 +297,27 @@ class Page extends DatabaseObject implements ILinkableObject, ITitledObject {
                return '';
        }
        
+       /**
+        * Returns the languages of this page.
+        * 
+        * @return PageLanguage[]
+        */
+       public function getPageLanguages() {
+               $pageLanguages = [];
+               if ($this->isMultilingual) {
+                       $sql = "SELECT  languageID
+                               FROM    wcf" . WCF_N . "_page_content
+                               WHERE   pageID = ?";
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       $statement->execute([$this->pageID]);
+                       while ($languageID = $statement->fetchColumn()) {
+                               $pageLanguages[] = new PageLanguage($this->pageID, $languageID);
+                       }
+               }
+               
+               return $pageLanguages;
+       }
+       
        /**
         * Returns the page with the given identifier.
         * 
diff --git a/wcfsetup/install/files/lib/data/page/PageLanguage.class.php b/wcfsetup/install/files/lib/data/page/PageLanguage.class.php
new file mode 100644 (file)
index 0000000..1c85cdf
--- /dev/null
@@ -0,0 +1,59 @@
+<?php
+namespace wcf\data\page;
+use wcf\data\language\Language;
+use wcf\system\language\LanguageFactory;
+use wcf\system\request\LinkHandler;
+
+/**
+ * Represents a page language.
+ *
+ * @author     Marcel Werk
+ * @copyright  2001-2016 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    com.woltlab.wcf
+ * @subpackage data.page
+ * @category   Community Framework
+ * @since      2.2
+ */
+class PageLanguage {
+       /**
+        * language id
+        * @var integer
+        */
+       protected $languageID;
+       
+       /**
+        * page id
+        * @var integer
+        */
+       protected $pageID;
+       
+       /**
+        * Creates a new PageLanguage object.
+        *
+        * @param       integer         $pageID
+        * @param       integer         $languageID
+        */
+       public function __construct($pageID, $languageID) {
+               $this->pageID = $pageID;
+               $this->languageID = $languageID;
+       }
+       
+       /**
+        * Returns the link to this version of the page.
+        *
+        * @return      string
+        */
+       public function getLink() {
+               return LinkHandler::getInstance()->getCmsLink($this->pageID, $this->languageID);
+       }
+       
+       /**
+        * Returns the language of this version of the page.
+        *
+        * @return      Language
+        */
+       public function getLanguage() {
+               return LanguageFactory::getInstance()->getLanguage($this->languageID);
+       }
+}