Skip to content

Commit

Permalink
Merge pull request #18 from nthmedia/v2
Browse files Browse the repository at this point in the history
Add fallback option to link to homepage if target entry does not exist
  • Loading branch information
lindseydiloreto authored Dec 1, 2020
2 parents 2ef7e2a + 0d7c187 commit 39bb760
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 2.2.0 - 2020-05-19

### Added
- Added fallback link to homepage when entry is not found on target site

## 2.1.0 - 2019-04-20

### Added
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ If you want to dynamically loop through each of your sites, try this instead:

### Checking whether a translated element exists

Lastly, you can check to make sure that a translated version of that element exists before showing the link:
You can check to make sure that a translated version of that element exists before showing the link:

```twig
{% set element = (category ?? entry ?? null) %}
Expand All @@ -110,6 +110,21 @@ Lastly, you can check to make sure that a translated version of that element exi
</ul>
```

### Checking whether a translated element exists

Lastly, you can add a fallback to show a link to the homepage of the target site if the entry does not exist on the target site by setting the third parameter to true:

```twig
{% set element = (category ?? entry ?? null) %}
<ul>
<li><a href="{{ siteSwitcher('english', element, true) }}">English</a></li>
<li><a href="{{ siteSwitcher('spanish', element, true) }}">Español</a></li>
<li><a href="{{ siteSwitcher('french', element, true) }}">Français</a></li>
<li><a href="{{ siteSwitcher('german', element, true) }}">Deutsch</a></li>
</ul>
```

***

## Anything else?
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "doublesecretagency/craft-siteswitcher",
"description": "Easily switch between sites on any page of your website.",
"type": "craft-plugin",
"version": "2.1.0",
"version": "2.2.0",
"keywords": [
"craft",
"cms",
Expand Down
8 changes: 7 additions & 1 deletion src/services/SiteSwitcherService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ class SiteSwitcherService extends Component
*
* @param string|null $siteHandle
* @param Element|null $element
* @param bool $fallbackToHomepage
* @return bool|string
* @throws Exception
* @throws InvalidConfigException
* @throws SiteNotFoundException
*/
public function url($siteHandle = null, Element $element = null)
public function url($siteHandle = null, Element $element = null, bool $fallbackToHomepage = false)
{
// If no site handle specified, use the default site
if (!$siteHandle) {
Expand All @@ -60,6 +61,11 @@ public function url($siteHandle = null, Element $element = null)
$siteLink .= "?{$queryString}";
}

// If not element is found on the target site, fallback to homepage
if ($siteLink === false && $fallbackToHomepage === true) {
$siteLink = Craft::$app->getSites()->getSiteByHandle($siteHandle)->getBaseUrl();
}

// Return site link
return $siteLink;
}
Expand Down
5 changes: 3 additions & 2 deletions src/twigextensions/SiteSwitcherTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ public function getFunctions(): array
*
* @param $siteHandle
* @param null $element
* @param bool $fallbackToHomepage
* @return mixed
*/
public function siteSwitcher($siteHandle, $element = null)
public function siteSwitcher($siteHandle, $element = null, $fallbackToHomepage = false)
{
return SiteSwitcher::$plugin->siteSwitcher->url($siteHandle, $element);
return SiteSwitcher::$plugin->siteSwitcher->url($siteHandle, $element, $fallbackToHomepage);
}

/**
Expand Down

0 comments on commit 39bb760

Please sign in to comment.