Skip to content

Commit

Permalink
Merge pull request #30 from dnadesign/feature/themed-default-image
Browse files Browse the repository at this point in the history
Allow to specify a default image per theme
  • Loading branch information
tractorcow authored Jun 18, 2020
2 parents f701656 + b29d7a6 commit b400b3e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 15 additions & 0 deletions src/Extensions/OpenGraphObjectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit b400b3e

Please sign in to comment.