mirror of
https://github.com/silverstripe/silverstripe-cms
synced 2024-10-22 06:05:56 +00:00
NEW Meta tag components
Meta tags can now be individually accessed and modified by key, prior to being rendered and flattened in to a single string.
This commit is contained in:
parent
2533e729fc
commit
e20be9293f
@ -1356,51 +1356,103 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the title, description, keywords and language metatags.
|
* Return attributes for various meta tags, plus a title tag, in a keyed array.
|
||||||
|
* Array structure corresponds to arguments for HTML::create_tag(). Example:
|
||||||
*
|
*
|
||||||
* @todo Move <title> tag in separate getter for easier customization and more obvious usage
|
* $tags['description'] = [
|
||||||
|
* // html tag type, if omitted defaults to 'meta'
|
||||||
|
* 'tag' => 'meta',
|
||||||
|
* // attributes of html tag
|
||||||
|
* 'attributes' => [
|
||||||
|
* 'name' => 'description',
|
||||||
|
* 'content' => $this->customMetaDescription(),
|
||||||
|
* ],
|
||||||
|
* // content of html tag. (True meta tags don't contain content)
|
||||||
|
* 'content' => null
|
||||||
|
* ];
|
||||||
|
*
|
||||||
|
* @see HTML::createTag()
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function MetaComponents()
|
||||||
|
{
|
||||||
|
$tags = [];
|
||||||
|
|
||||||
|
$tags['title'] = [
|
||||||
|
'tag' => 'title',
|
||||||
|
'attributes' => [],
|
||||||
|
'content' => $this->obj('Title')->forTemplate()
|
||||||
|
];
|
||||||
|
|
||||||
|
$generator = trim(Config::inst()->get(self::class, 'meta_generator'));
|
||||||
|
if (!empty($generator)) {
|
||||||
|
$tags['generator'] = [
|
||||||
|
'attributes' => [
|
||||||
|
'name' => 'generator',
|
||||||
|
'content' => $generator,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$charset = ContentNegotiator::config()->uninherited('encoding');
|
||||||
|
$tags['contentType'] = [
|
||||||
|
'attributes' => [
|
||||||
|
'http-equiv' => 'Content-Type',
|
||||||
|
'content' => 'text/html; charset=' . $charset,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
if ($this->MetaDescription) {
|
||||||
|
$tags['description'] = [
|
||||||
|
'attributes' => [
|
||||||
|
'name' => 'description',
|
||||||
|
'content' => $this->MetaDescription,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Permission::check('CMS_ACCESS_CMSMain')
|
||||||
|
&& $this->ID > 0
|
||||||
|
) {
|
||||||
|
$tags['pageId'] = [
|
||||||
|
'attributes' => [
|
||||||
|
'name' => 'x-page-id',
|
||||||
|
'content' => $this->ID,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
$tags['cmsEditLink'] = [
|
||||||
|
'attributes' => [
|
||||||
|
'name' => 'x-cms-edit-link',
|
||||||
|
'content' => $this->CMSEditLink(),
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->extend('MetaComponents', $tags);
|
||||||
|
|
||||||
|
return $tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the title, description, keywords and language metatags.
|
||||||
*
|
*
|
||||||
* @param bool $includeTitle Show default <title>-tag, set to false for custom templating
|
* @param bool $includeTitle Show default <title>-tag, set to false for custom templating
|
||||||
* @return string The XHTML metatags
|
* @return string The XHTML metatags
|
||||||
*/
|
*/
|
||||||
public function MetaTags($includeTitle = true)
|
public function MetaTags($includeTitle = true)
|
||||||
{
|
{
|
||||||
$tags = array();
|
$tags = [];
|
||||||
if ($includeTitle && strtolower($includeTitle) != 'false') {
|
$tagsArray = $this->MetaComponents();
|
||||||
$tags[] = HTML::createTag('title', array(), $this->obj('Title')->forTemplate());
|
if (!$includeTitle) {
|
||||||
|
unset($tagsArray['title']);
|
||||||
}
|
}
|
||||||
|
|
||||||
$generator = trim(Config::inst()->get(self::class, 'meta_generator'));
|
foreach ($tagsArray as $tagProps) {
|
||||||
if (!empty($generator)) {
|
$tag = array_merge([
|
||||||
$tags[] = HTML::createTag('meta', array(
|
'tag' => 'meta',
|
||||||
'name' => 'generator',
|
'attributes' => [],
|
||||||
'content' => $generator,
|
'content' => null,
|
||||||
));
|
], $tagProps);
|
||||||
}
|
$tags[] = HTML::createTag($tag['tag'], $tag['attributes'], $tag['content']);
|
||||||
|
|
||||||
$charset = ContentNegotiator::config()->uninherited('encoding');
|
|
||||||
$tags[] = HTML::createTag('meta', array(
|
|
||||||
'http-equiv' => 'Content-Type',
|
|
||||||
'content' => 'text/html; charset=' . $charset,
|
|
||||||
));
|
|
||||||
if ($this->MetaDescription) {
|
|
||||||
$tags[] = HTML::createTag('meta', array(
|
|
||||||
'name' => 'description',
|
|
||||||
'content' => $this->MetaDescription,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Permission::check('CMS_ACCESS_CMSMain')
|
|
||||||
&& $this->ID > 0
|
|
||||||
) {
|
|
||||||
$tags[] = HTML::createTag('meta', array(
|
|
||||||
'name' => 'x-page-id',
|
|
||||||
'content' => $this->obj('ID')->forTemplate(),
|
|
||||||
));
|
|
||||||
$tags[] = HTML::createTag('meta', array(
|
|
||||||
'name' => 'x-cms-edit-link',
|
|
||||||
'content' => $this->obj('CMSEditLink')->forTemplate(),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$tagString = implode("\n", $tags);
|
$tagString = implode("\n", $tags);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user