Browse Source

Allow arbitrary meta tags to be created with arrays.

This was removed in the 2.x -> 3.0 updates, but there isn't really
a reason to remove it imo.

Refs #4903
Mark Story 11 years ago
parent
commit
5a6af32f21
2 changed files with 37 additions and 25 deletions
  1. 31 25
      src/View/Helper/HtmlHelper.php
  2. 6 0
      tests/TestCase/View/Helper/HtmlHelperTest.php

+ 31 - 25
src/View/Helper/HtmlHelper.php

@@ -196,12 +196,16 @@ class HtmlHelper extends Helper {
  *
  * `$this->Html->meta('description', 'A great page', array('block' => 'metaTags'));`
  *
+ * Create a custom meta tag:
+ *
+ * `$this->Html->meta(['property' => 'og:site_name', 'content' => 'CakePHP']);`
+ *
  * ### Options
  *
  * - `block` - Set to true to append output to view block "meta" or provide
  *   custom block name.
  *
- * @param string $type The title of the external resource
+ * @param string|array $type The title of the external resource
  * @param string|array $content The address of the external resource or string for content attribute
  * @param array $options Other attributes for the generated tag. If the type attribute is html,
  *    rss, atom, or icon, the mime-type is returned.
@@ -211,33 +215,35 @@ class HtmlHelper extends Helper {
 	public function meta($type, $content = null, array $options = array()) {
 		$options += array('block' => null);
 
-		$types = array(
-			'rss' => array('type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => $type, 'link' => $content),
-			'atom' => array('type' => 'application/atom+xml', 'title' => $type, 'link' => $content),
-			'icon' => array('type' => 'image/x-icon', 'rel' => 'icon', 'link' => $content),
-			'keywords' => array('name' => 'keywords', 'content' => $content),
-			'description' => array('name' => 'description', 'content' => $content),
-			'robots' => array('name' => 'robots', 'content' => $content),
-			'viewport' => array('name' => 'viewport', 'content' => $content),
-		);
-
-		if ($type === 'icon' && $content === null) {
-			$types['icon']['link'] = 'favicon.ico';
-		}
+		if (!is_array($type)) {
+			$types = array(
+				'rss' => array('type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => $type, 'link' => $content),
+				'atom' => array('type' => 'application/atom+xml', 'title' => $type, 'link' => $content),
+				'icon' => array('type' => 'image/x-icon', 'rel' => 'icon', 'link' => $content),
+				'keywords' => array('name' => 'keywords', 'content' => $content),
+				'description' => array('name' => 'description', 'content' => $content),
+				'robots' => array('name' => 'robots', 'content' => $content),
+				'viewport' => array('name' => 'viewport', 'content' => $content),
+			);
+
+			if ($type === 'icon' && $content === null) {
+				$types['icon']['link'] = 'favicon.ico';
+			}
 
-		if (isset($types[$type])) {
-			$type = $types[$type];
-		} elseif (!isset($options['type']) && $content !== null) {
-			if (is_array($content) && isset($content['_ext'])) {
-				$type = $types[$content['_ext']];
+			if (isset($types[$type])) {
+				$type = $types[$type];
+			} elseif (!isset($options['type']) && $content !== null) {
+				if (is_array($content) && isset($content['_ext'])) {
+					$type = $types[$content['_ext']];
+				} else {
+					$type = ['name' => $type, 'content' => $content];
+				}
+			} elseif (isset($options['type']) && isset($types[$options['type']])) {
+				$type = $types[$options['type']];
+				unset($options['type']);
 			} else {
-				$type = ['name' => $type, 'content' => $content];
+				$type = [];
 			}
-		} elseif (isset($options['type']) && isset($types[$options['type']])) {
-			$type = $types[$options['type']];
-			unset($options['type']);
-		} else {
-			$type = [];
 		}
 
 		$options += $type;

+ 6 - 0
tests/TestCase/View/Helper/HtmlHelperTest.php

@@ -1566,6 +1566,12 @@ class HtmlHelperTest extends TestCase {
 			'meta' => ['name' => 'viewport', 'content' => 'width=device-width']
 		];
 		$this->assertHtml($expected, $result);
+
+		$result = $this->Html->meta(['property' => 'og:site_name', 'content' => 'CakePHP']);
+		$expected = [
+			'meta' => ['property' => 'og:site_name', 'content' => 'CakePHP']
+		];
+		$this->assertHtml($expected, $result);
 	}
 
 /**