Skip to content

Commit

Permalink
Adjusted verbiage
Browse files Browse the repository at this point in the history
  • Loading branch information
lindseydiloreto committed Dec 2, 2020
1 parent 39bb760 commit 8efbe97
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Changelog

## 2.2.0 - 2020-05-19
## 2.2.0 - UNRELEASED

### Added
- Added fallback link to homepage when entry is not found on target site
- Optionally fall back to the translated homepage when a link cannot otherwise be determined.

## 2.1.0 - 2019-04-20

Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ You can check to make sure that a translated version of that element exists befo
</ul>
```

### Checking whether a translated element exists
### Fall back to the translated homepage

Assuming your homepage is properly translated across each site, you can set the homepage as a final backstop. If the link cannot otherwise be determined, it will link to the translated homepage instead.

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:
Simply set the optional third parameter to `true`:

```twig
{% set element = (category ?? entry ?? null) %}
Expand Down
11 changes: 7 additions & 4 deletions src/services/SiteSwitcherService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ class SiteSwitcherService extends Component
*/
public function url($siteHandle = null, Element $element = null, bool $fallbackToHomepage = false)
{
// Get sites service
$sites = Craft::$app->getSites();

// If no site handle specified, use the default site
if (!$siteHandle) {
$siteHandle = Craft::$app->getSites()->getPrimarySite()->handle;
$siteHandle = $sites->getPrimarySite()->handle;
}

// If element is specified
Expand All @@ -61,9 +64,9 @@ public function url($siteHandle = null, Element $element = null, bool $fallbackT
$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();
// If site link isn't valid, optionally fall back to the homepage
if (!$siteLink && $fallbackToHomepage) {
$siteLink = ($sites->getSiteByHandle($siteHandle)->getBaseUrl() ?? false);
}

// Return site link
Expand Down

0 comments on commit 8efbe97

Please sign in to comment.