mirror of
https://github.com/silverstripe/silverstripe-cms
synced 2024-10-22 08:05:56 +02:00
commit
d2310852da
@ -541,7 +541,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
|
|||||||
if (!($page = DataObject::get_by_id(self::class, $arguments['id'])) // Get the current page by ID.
|
if (!($page = DataObject::get_by_id(self::class, $arguments['id'])) // Get the current page by ID.
|
||||||
&& !($page = Versioned::get_latest_version(self::class, $arguments['id'])) // Attempt link to old version.
|
&& !($page = Versioned::get_latest_version(self::class, $arguments['id'])) // Attempt link to old version.
|
||||||
) {
|
) {
|
||||||
return null; // There were no suitable matches at all.
|
return null; // There were no suitable matches at all.
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var SiteTree $page */
|
/** @var SiteTree $page */
|
||||||
@ -1356,51 +1356,102 @@ 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',
|
||||||
|
'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 || strtolower($includeTitle) == 'false') {
|
||||||
|
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);
|
||||||
|
@ -1353,6 +1353,57 @@ class SiteTreeTest extends SapphireTest
|
|||||||
// Test without title
|
// Test without title
|
||||||
$meta = $page->MetaTags(false);
|
$meta = $page->MetaTags(false);
|
||||||
$this->assertNotContains('<title>', $meta);
|
$this->assertNotContains('<title>', $meta);
|
||||||
|
|
||||||
|
$meta = $page->MetaTags('false');
|
||||||
|
$this->assertNotContains('<title>', $meta);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMetaComponents()
|
||||||
|
{
|
||||||
|
$this->logInWithPermission('ADMIN');
|
||||||
|
/** @var SiteTree $page */
|
||||||
|
$page = $this->objFromFixture('Page', 'metapage');
|
||||||
|
|
||||||
|
$charset = Config::inst()->get(ContentNegotiator::class, 'encoding');
|
||||||
|
|
||||||
|
$expected = [
|
||||||
|
'title' => [
|
||||||
|
'tag' => 'title',
|
||||||
|
'content' => "HTML & XML",
|
||||||
|
],
|
||||||
|
'generator' => [
|
||||||
|
'attributes' => [
|
||||||
|
'name' => 'generator',
|
||||||
|
'content' => Config::inst()->get(SiteTree::class, 'meta_generator')
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'contentType' => [
|
||||||
|
'attributes' => [
|
||||||
|
'http-equiv' => 'Content-Type',
|
||||||
|
'content' => "text/html; charset=$charset",
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'description' => [
|
||||||
|
'attributes' => [
|
||||||
|
'name' => 'description',
|
||||||
|
'content' => 'The <br /> and <br> tags'
|
||||||
|
]
|
||||||
|
],
|
||||||
|
'pageId' => [
|
||||||
|
'attributes' => [
|
||||||
|
'name' => 'x-page-id',
|
||||||
|
'content' => $page->ID
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'cmsEditLink' => [
|
||||||
|
'attributes' => [
|
||||||
|
'name' => 'x-cms-edit-link',
|
||||||
|
'content' => $page->CMSEditLink()
|
||||||
|
]
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $page->MetaComponents());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user