From b29d7a662fe8a5c4dbc6eb2547bffcca6d3091f2 Mon Sep 17 00:00:00 2001 From: Alexandre Saelens Date: Wed, 17 Jun 2020 15:42:17 +1200 Subject: [PATCH] Allow to specify a default image per theme --- README.md | 14 ++++++++++++++ src/Extensions/OpenGraphObjectExtension.php | 15 +++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/README.md b/README.md index 8b2e5ea..abeb225 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,20 @@ class MyPage extends Page { By implementing these properties explicitly in your page classes, you can override the default properties defined in the OpenGraphPageExtension. +#### Setting open Graph default image + +The Open Graph image is a required property and should be supplied a default image. +You can set the path to the default image in the yml config + +```yml +TractorCow\OpenGraph\Extensions\OpenGraphObjectExtension: + default_image: 'app/images/logo.png' + theme_name_default_image: 'app/images/theme-logo.png' +``` + +Note that you can specify a different image for each theme by prefixing the default_image config name with the theme name (replace everything that is not a letter with an _). +This is useful if you want to set a different default image on sub sites. + ### Adding new types If you wish to add a new og:type you will need to: diff --git a/src/Extensions/OpenGraphObjectExtension.php b/src/Extensions/OpenGraphObjectExtension.php index a0fc9c9..86548b2 100644 --- a/src/Extensions/OpenGraphObjectExtension.php +++ b/src/Extensions/OpenGraphObjectExtension.php @@ -9,6 +9,7 @@ use SilverStripe\ORM\DataExtension; use SilverStripe\ORM\FieldType\DBText; use SilverStripe\SiteConfig\SiteConfig; +use SilverStripe\View\SSViewer; use TractorCow\OpenGraph\Constants\OGDeterminers; use TractorCow\OpenGraph\Constants\OGTypes; use TractorCow\OpenGraph\Interfaces\IOpenGraphObjectBuilder; @@ -139,6 +140,20 @@ public function getOGSiteName() public function getOGImage() { + // If a theme is in use, check if a default image is provided (theme_name_default_image) + // This is useful to have a different default image on sub sites + if (SSViewer::config()->uninherited('theme_enabled') === true) { + $themes = SSViewer::get_themes(); + if (isset($themes[0])) { + $themeName = preg_replace('/[^\w ]+/ ', '_', strtolower($themes[0])); + $config = $themeName . '_default_image'; + + if ($image = self::config()->{$config}) { + return Director::absoluteURL(ModuleResourceLoader::resourceURL($image)); + } + } + } + // Since og:image is a required property, provide a reasonable default if ($image = self::config()->default_image) { return Director::absoluteURL(ModuleResourceLoader::resourceURL($image));