From 5a6af32f21ce133c09f47d5a0b02e9814a11215b Mon Sep 17 00:00:00 2001 From: Mark Story Date: Fri, 17 Oct 2014 08:26:47 -0400 Subject: [PATCH] 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 --- src/View/Helper/HtmlHelper.php | 56 ++++++++++--------- tests/TestCase/View/Helper/HtmlHelperTest.php | 6 ++ 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/src/View/Helper/HtmlHelper.php b/src/View/Helper/HtmlHelper.php index 86bfed58eeb..99c9ce0e59a 100644 --- a/src/View/Helper/HtmlHelper.php +++ b/src/View/Helper/HtmlHelper.php @@ -196,12 +196,16 @@ public function docType($type = 'html5') { * * `$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 @@ public function docType($type = 'html5') { 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; diff --git a/tests/TestCase/View/Helper/HtmlHelperTest.php b/tests/TestCase/View/Helper/HtmlHelperTest.php index 7fce6226d30..30d3441d5ed 100644 --- a/tests/TestCase/View/Helper/HtmlHelperTest.php +++ b/tests/TestCase/View/Helper/HtmlHelperTest.php @@ -1566,6 +1566,12 @@ public function testMeta() { '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); } /**