Skip to content

Commit

Permalink
Allow arbitrary meta tags to be created with arrays.
Browse files Browse the repository at this point in the history
This was removed in the 2.x -> 3.0 updates, but there isn't really
a reason to remove it imo.

Refs #4903
  • Loading branch information
markstory committed Oct 17, 2014
1 parent b073213 commit 5a6af32
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
56 changes: 31 additions & 25 deletions src/View/Helper/HtmlHelper.php
Expand Up @@ -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.
Expand All @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions tests/TestCase/View/Helper/HtmlHelperTest.php
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 5a6af32

Please sign in to comment.