Skip to content

Commit

Permalink
setMatchedElement() / reset matched element on preview requests
Browse files Browse the repository at this point in the history
Fixes #4542
  • Loading branch information
brandonkelly committed Jul 15, 2019
1 parent ca5594f commit e2457ce
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

## Unreleased

### Added
- Added `craft\web\UrlManager::setMatchedElement()`.

### Fixed
- Fixed a bug where it wasn’t possible to delete Matrix blocks if Min Blocks and Max Blocks were set to the same value, and an element already had more than that many blocks. ([#4562](https://github.com/craftcms/cms/issues/4562))
- Fixed a bug where `craft\web\UrlManager::getMatchedElement()` could return the incorrect result on preview requests. ([#4542](https://github.com/craftcms/cms/issues/4542))

## 3.2.2 - 2019-07-14

Expand Down
1 change: 1 addition & 0 deletions src/controllers/PreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public function actionPreview(string $elementType, int $sourceId, int $siteId, i
$urlManager = Craft::$app->getUrlManager();
$urlManager->checkToken = false;
$urlManager->setRouteParams([], false);
$urlManager->setMatchedElement(null);
return Craft::$app->handleRequest(Craft::$app->getRequest(), true);
}
}
48 changes: 31 additions & 17 deletions src/web/UrlManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,32 @@ public function getMatchedElement()
return $this->_matchedElement;
}

/**
* Sets the matched element for the request.
*
* @param ElementInterface|false|null $element
* @since 3.2.3
*/
public function setMatchedElement($element)
{
if ($element instanceof ElementInterface) {
if ($route = $element->getRoute()) {
if (is_string($route)) {
$route = [$route, []];
}
$this->_matchedElement = $element;
$this->_matchedElementRoute = $route;
return;
}

// Element doesn't have a route so ignore it
$element = false;
}

$this->_matchedElement = $element;
$this->_matchedElementRoute = $element;
}

// Protected Methods
// =========================================================================

Expand Down Expand Up @@ -370,32 +396,20 @@ private function _getMatchedElementRoute(string $path)
return $this->_matchedElementRoute;
}

$this->_matchedElement = false;
$this->_matchedElementRoute = false;

if (Craft::$app->getIsInstalled() && Craft::$app->getRequest()->getIsSiteRequest()) {
/** @var Element $element */
/** @noinspection PhpUnhandledExceptionInspection */
$element = Craft::$app->getElements()->getElementByUri($path, Craft::$app->getSites()->getCurrentSite()->id, true);

if ($element) {
$route = $element->getRoute();

if ($route) {
if (is_string($route)) {
$route = [$route, []];
}

$this->_matchedElement = $element;
$this->_matchedElementRoute = $route;
}
}
} else {
$element = null;
}

$this->setMatchedElement($element ?: false);

if (YII_DEBUG) {
Craft::debug([
'rule' => 'Element URI: ' . $path,
'match' => isset($element, $route),
'match' => $this->_matchedElement instanceof ElementInterface,
'parent' => null
], __METHOD__);
}
Expand Down

0 comments on commit e2457ce

Please sign in to comment.