Skip to content

Commit

Permalink
Now runs initialization on the EVENT_AFTER_LOAD_PLUGINS event.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbossarte committed Jun 25, 2018
1 parent d0c2ab5 commit 92fe44f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## [Unreleased]

## [1.2.0] 2018-02-26

### Fixed
- Fallback Site now performs initialization after all plugins have loaded, its old behavior could cause compatibility issues with other plugins.
- Set correct `license` option in the `composer.json`.

## [1.1.0] 2018-02-26
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": "charliedev/fallback-site",
"description": "Allows sites of a multi-site install to use entries of other sites if a matching slug is found.",
"type": "craft-plugin",
"version": "1.1.0",
"version": "1.2.0",
"license": "proprietary",
"authors": [
{
Expand Down
56 changes: 34 additions & 22 deletions src/FallbackSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@

use Craft;
use craft\base\Plugin;
use craft\events\ExceptionEvent;
use craft\helpers\ArrayHelper;
use craft\services\Fields;
use craft\web\ErrorHandler;
use craft\services\Plugins;
use craft\web\UrlManager;

use yii\base\Event;
use yii\web\HttpException;

/**
* The main Craft plugin class.
Expand All @@ -28,21 +24,18 @@ class FallbackSite extends Plugin
*/
public function init()
{
// Determine if there is an element at the given URL, and attempt to find one using fallback sites
// if one is not available. This may interfere with custom routes or direct templates, if one
// happens to conflict with the potential path.

// Only run for regular web frontend requests.
if (!Craft::$app->getRequest()->getIsConsoleRequest()
&& Craft::$app->getRequest()->getIsSiteRequest()
&& !Craft::$app->getRequest()->getIsLivePreview()) {
$element = Craft::$app->getUrlManager()->getMatchedElement(); // Find the element that the current URL is going to.
if ($element == null) { // No element found, this request may 404 normally.
$this->renderFallbackEntry();
}
}

parent::init();

// NOTE: This will wind up using Craft's `UrlManager`, initializing it earlier than Craft normally would.
// This means any plugins/modules that have not yet attached their event handlers to UrlManager will never
// receive some of its events (EVENT_REGISTER_CP_URL_RULES, EVENT_REGISTER_SITE_URL_RULES).
Event::on(
Plugins::class,
Plugins::EVENT_AFTER_LOAD_PLUGINS,
function () {
$this->checkFallbackEntry();
}
);
}

/**
Expand All @@ -53,7 +46,7 @@ protected function createSettingsModel()
{
return new \charliedev\fallbacksite\models\Settings();
}

/**
* @inheritdoc
* @see craft\base\Plugin
Expand All @@ -78,7 +71,26 @@ protected function settingsHtml()
'siteoptions' => $siteoptions
]);
}


/**
* Attempts to find and render an entry from configured fallback sites if an entry hasn't been found for the visited site.
*/
private function checkFallbackEntry() {

// Only run for regular web frontend requests.
if (!Craft::$app->getRequest()->getIsConsoleRequest()
&& Craft::$app->getRequest()->getIsSiteRequest()
&& !Craft::$app->getRequest()->getIsLivePreview()) {
// Determine if there is an element at the given URL, and attempt to find one using fallback sites
// if one is not available. This may interfere with custom routes or direct templates, if one
// happens to conflict with the potential path.
$element = Craft::$app->getUrlManager()->getMatchedElement(); // Find the element that the current URL is going to.
if ($element == null) { // No element found, this request may 404 normally.
$this->renderFallbackEntry();
}
}
}

/**
* Attempts to find and render an entry from configured fallback sites if an entry hasn't been found for the visited site.
*/
Expand All @@ -100,7 +112,7 @@ private function renderFallbackEntry()
return;
}
$checked[$siteid] = true; // Mark this new site as checked.

$element = Craft::$app->getElements()->getElementByUri($path, $siteid, true); // Check for an entry with the same path in the fallback site.
if ($element) { // An element was found with the given path and site id.
// Make sure the element has a route, too.
Expand Down

0 comments on commit 92fe44f

Please sign in to comment.