Skip to content

Commit

Permalink
Craft 4 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
lindseydiloreto committed Mar 29, 2022
1 parent 7410568 commit df14c90
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 74 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ Easily switch between sites on any page of your website.

***

**The plugin formerly known as "Language Link".**

## About

This plugin provides an easy way to switch between sites on your website. Regardless of which page you are currently on, you will be linked to the same page in its parallel site.

_* Note: You must be using Craft's [multi-site](https://craftcms.com/features/all#multi-site) feature to access multiple languages on your website._
Expand Down
53 changes: 43 additions & 10 deletions src/SiteSwitcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@

namespace doublesecretagency\siteswitcher;

use yii\base\Event;

use Craft;
use craft\base\Plugin;
use craft\web\twig\variables\CraftVariable;
use doublesecretagency\siteswitcher\services\SiteSwitcherService;
use doublesecretagency\siteswitcher\twigextensions\SiteSwitcherTwigExtension;
use doublesecretagency\siteswitcher\variables\SiteSwitcherVariable;
use yii\base\Event;

/**
* Class SiteSwitcher
Expand All @@ -27,21 +26,43 @@
class SiteSwitcher extends Plugin
{

/** @var Plugin $plugin Self-referential plugin property. */
public static $plugin;
/**
* @var Plugin Self-referential plugin property.
*/
public static Plugin $plugin;

/** @inheritDoc */
public function init()
/**
* @inheritdoc
*/
public function init(): void
{
parent::init();
self::$plugin = $this;

// Register components
$this->_registerService();
$this->_registerVariable();
$this->_registerTwigExtension();
}

// ========================================================================= //

/**
* Register services.
*/
private function _registerService(): void
{
// Load plugin components
$this->setComponents([
'siteSwitcher' => SiteSwitcherService::class,
]);
}

// Register variables
/**
* Register variables.
*/
private function _registerVariable(): void
{
Event::on(
CraftVariable::class,
CraftVariable::EVENT_INIT,
Expand All @@ -50,16 +71,28 @@ static function (Event $event) {
$variable->set('siteSwitcher', SiteSwitcherVariable::class);
}
);
}

/**
* Register Twig extension.
*/
private function _registerTwigExtension(): void
{
// Get request services
$request = Craft::$app->getRequest();

// If control panel request, bail
if (Craft::$app->getRequest()->getIsCpRequest()) {
if ($request->getIsCpRequest()) {
return;
}

if (Craft::$app->getRequest()->getIsSiteRequest()) {
Craft::$app->getView()->registerTwigExtension(new SiteSwitcherTwigExtension());
// If not a site request, bail
if (!$request->getIsSiteRequest()) {
return;
}

// Register Twig extension
Craft::$app->getView()->registerTwigExtension(new SiteSwitcherTwigExtension());
}

}
27 changes: 6 additions & 21 deletions src/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 12 additions & 12 deletions src/services/SiteSwitcherService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ class SiteSwitcherService extends Component
/**
* Render localized URL for current page.
*
* @param string|null $siteHandle
* @param Element|null $element
* @param null|string $siteHandle
* @param null|Element $element
* @param bool $fallbackToHomepage
* @return bool|string
* @return null|string
* @throws Exception
* @throws InvalidConfigException
* @throws SiteNotFoundException
*/
public function url($siteHandle = null, Element $element = null, bool $fallbackToHomepage = false)
public function url(?string $siteHandle = null, ?Element $element = null, bool $fallbackToHomepage = false): ?string
{
// Get sites service
$sites = Craft::$app->getSites();
Expand Down Expand Up @@ -66,7 +66,7 @@ public function url($siteHandle = null, Element $element = null, bool $fallbackT

// If site link isn't valid, optionally fall back to the homepage
if (!$siteLink && $fallbackToHomepage) {
$siteLink = ($sites->getSiteByHandle($siteHandle)->getBaseUrl() ?? false);
$siteLink = ($sites->getSiteByHandle($siteHandle)->getBaseUrl() ?? null);
}

// Return site link
Expand All @@ -78,13 +78,13 @@ public function url($siteHandle = null, Element $element = null, bool $fallbackT
*
* @param string $siteHandle
* @param Element $element
* @return string|bool
* @return null|string
*/
private function _getElementUrl($siteHandle, Element $element)
private function _getElementUrl(string $siteHandle, Element $element): ?string
{
// If element is not localized, bail
if (!$element->isLocalized()) {
return false;
return null;
}

// Get localized element
Expand All @@ -95,7 +95,7 @@ private function _getElementUrl($siteHandle, Element $element)

// If no localized element exists, bail
if (!$localeElement) {
return false;
return null;
}

// Return localized element URL
Expand All @@ -106,18 +106,18 @@ private function _getElementUrl($siteHandle, Element $element)
* Get localized non-element URL.
*
* @param $siteHandle
* @return bool|string
* @return null|string
* @throws Exception
* @throws InvalidConfigException
*/
private function _getNonElementUrl($siteHandle)
private function _getNonElementUrl(string $siteHandle): ?string
{
// Get specified site
$site = Craft::$app->getSites()->getSiteByHandle($siteHandle);

// If no site, bail
if (!$site) {
return false;
return null;
}

// Get page URI
Expand Down
27 changes: 6 additions & 21 deletions src/twigextensions/SiteSwitcherTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace doublesecretagency\siteswitcher\twigextensions;

use Craft;
use craft\base\Element;
use doublesecretagency\siteswitcher\SiteSwitcher;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
Expand Down Expand Up @@ -39,36 +39,21 @@ public function getName(): string
public function getFunctions(): array
{
return [
new TwigFunction('siteSwitcher', [$this, 'siteSwitcher']),
new TwigFunction('ll', [$this, 'll']), // DEPRECATED
new TwigFunction('siteSwitcher', [$this, 'siteSwitcher'])
];
}

/**
* Shortcut to service method.
*
* @param $siteHandle
* @param null $element
* @param null|string $siteHandle
* @param null|Element $element
* @param bool $fallbackToHomepage
* @return mixed
* @return null|string
*/
public function siteSwitcher($siteHandle, $element = null, $fallbackToHomepage = false)
public function siteSwitcher(?string $siteHandle = null, ?Element $element = null, bool $fallbackToHomepage = false): ?string
{
return SiteSwitcher::$plugin->siteSwitcher->url($siteHandle, $element, $fallbackToHomepage);
}

/**
* Deprecated shortcut to service method.
*
* @param $siteHandle
* @param null $element
* @return mixed
* @deprecated in Site Switcher 2.0. Use siteSwitcher() instead.
*/
public function ll($siteHandle, $element = null)
{
Craft::$app->getDeprecator()->log('ll', 'll() has been deprecated. Use siteSwitcher() instead.');
return $this->siteSwitcher($siteHandle, $element);
}

}
17 changes: 11 additions & 6 deletions src/variables/SiteSwitcherVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace doublesecretagency\siteswitcher\variables;

use Craft;
use craft\base\Element;
use doublesecretagency\siteswitcher\SiteSwitcher;

/**
Expand All @@ -21,15 +23,18 @@ class SiteSwitcherVariable
{

/**
* Render localized URL for current page
* Render localized URL for current page.
*
* @param string $siteHandle
* @param null $element
* @return mixed
* @param null|string $siteHandle
* @param null|Element $element
* @param bool $fallbackToHomepage
* @return null|string
* @deprecated in 2.3.0. Use `siteSwitcher()` instead.
*/
public function url($siteHandle = null, $element = null)
public function url(?string $siteHandle = null, ?Element $element = null, bool $fallbackToHomepage = false): ?string
{
return SiteSwitcher::$plugin->siteSwitcher->url($siteHandle, $element);
Craft::$app->getDeprecator()->log('craft.siteSwitcher.url()', 'craft.siteSwitcher.url() has been deprecated. Use siteSwitcher() instead.');
return SiteSwitcher::$plugin->siteSwitcher->url($siteHandle, $element, $fallbackToHomepage);
}

}

0 comments on commit df14c90

Please sign in to comment.