Browse Source

add a method for generating pagination meta links

These are hints, not directives.

For more information see
https://support.google.com/webmasters/answer/1663744
AD7six 11 years ago
parent
commit
935181285e

+ 51 - 0
src/View/Helper/PaginatorHelper.php

@@ -73,6 +73,7 @@ class PaginatorHelper extends Helper {
 			'sortDesc' => '<a class="desc" href="{{url}}">{{text}}</a>',
 			'sortAscLocked' => '<a class="asc locked" href="{{url}}">{{text}}</a>',
 			'sortDescLocked' => '<a class="desc locked" href="{{url}}">{{text}}</a>',
+			'metaLink' => '<link rel="{{type}}" href="{{url}}">',
 		]
 	];
 
@@ -809,6 +810,56 @@ class PaginatorHelper extends Helper {
 		}
 		return $out;
 	}
+/**
+ * Returns the meta-links for a paginated result set
+ *
+ * `echo $this->Paginator->meta();`
+ *
+ * Echos the links directly, will output nothing of there is neither a previous nor next page.
+ *
+ * `$this->Paginator->meta(['block' => true]);`
+ *
+ * Will append the output of the meta function to the named block - if true is passed the "meta"
+ * block is used
+ *
+ * ### Options:
+ *
+ * - `model` The model to use defaults to PaginatorHelper::defaultModel()
+ * - `block` The block name to append the output to, or false/absenst to return as a string
+ *
+ * @param array $options Array of options
+ * @return string|null meta links
+ */
+	public function meta($options = []) {
+		$model = isset($options['model']) ? $options['model'] : null;
+		$params = $this->params($model);
+		$links = [];
+
+		if ($this->hasPrev()) {
+			$links[] = $this->templater()->format('metaLink', [
+				'type' => 'prev',
+				'url' => $this->generateUrl(['page' => $params['page'] - 1], null, true)
+			]);
+		}
+
+		if ($this->hasNext()) {
+			$links[] = $this->templater()->format('metaLink', [
+				'type' => 'next',
+				'url' => $this->generateUrl(['page' => $params['page'] + 1], null, true)
+			]);
+		}
+
+		$out = implode($links);
+
+		if (empty($options['block'])) {
+			return $out;
+		}
+
+		if ($options['block'] === true) {
+			$options['block'] = __FUNCTION__;
+		}
+		$this->_View->append($options['block'], $out);
+	}
 
 /**
  * Event listeners.

+ 102 - 0
tests/TestCase/View/Helper/PaginatorHelperTest.php

@@ -2021,4 +2021,106 @@ class PaginatorHelperTest extends TestCase {
 		$expected = '0 of 1';
 		$this->assertEquals($expected, $result);
 	}
+
+/**
+ * Verify that no next and prev links are created for single page results
+ *
+ * @return void
+ */
+	public function testMetaPage0() {
+		$this->Paginator->request['paging'] = array(
+			'Article' => array(
+				'page' => 1,
+				'prevPage' => false,
+				'nextPage' => false,
+				'pageCount' => 1,
+			)
+		);
+
+		$expected = '';
+		$result = $this->Paginator->meta();
+		$this->assertSame($expected, $result);
+	}
+
+/**
+ * Verify that page 1 only has a next link
+ *
+ * @return void
+ */
+	public function testMetaPage1() {
+		$this->Paginator->request['paging'] = array(
+			'Article' => array(
+				'page' => 1,
+				'prevPage' => false,
+				'nextPage' => true,
+				'pageCount' => 2,
+			)
+		);
+
+		$expected = '<link rel="next" href="http://localhost/index?page=2">';
+		$result = $this->Paginator->meta();
+		$this->assertSame($expected, $result);
+	}
+
+/**
+ * Verify that the method will append to a block
+ *
+ * @return void
+ */
+	public function testMetaPage1InlineFalse() {
+		$this->Paginator->request['paging'] = array(
+			'Article' => array(
+				'page' => 1,
+				'prevPage' => false,
+				'nextPage' => true,
+				'pageCount' => 2,
+			)
+		);
+
+		$expected = '<link rel="next" href="http://localhost/index?page=2">';
+		$this->Paginator->meta(['block' => true]);
+		$result = $this->View->fetch('meta');
+		$this->assertSame($expected, $result);
+	}
+
+/**
+ * Verify that the last page only has a prev link
+ *
+ * @return void
+ */
+	public function testMetaPage1Last() {
+		$this->Paginator->request['paging'] = array(
+			'Article' => array(
+				'page' => 2,
+				'prevPage' => true,
+				'nextPage' => false,
+				'pageCount' => 2,
+			)
+		);
+
+		$expected = '<link rel="prev" href="http://localhost/index">';
+		$result = $this->Paginator->meta();
+		$this->assertSame($expected, $result);
+	}
+
+/**
+ * Verify that a page in the middle has both links
+ *
+ * @return void
+ */
+	public function testMetaPage10Last() {
+		$this->Paginator->request['paging'] = array(
+			'Article' => array(
+				'page' => 5,
+				'prevPage' => true,
+				'nextPage' => true,
+				'pageCount' => 10,
+			)
+		);
+
+		$expected = '<link rel="prev" href="http://localhost/index?page=4">';
+		$expected .= '<link rel="next" href="http://localhost/index?page=6">';
+		$result = $this->Paginator->meta();
+		$this->assertSame($expected, $result);
+	}
 }