Skip to content

Commit

Permalink
Element::HOMEPAGE_URI + getIsHomepage()
Browse files Browse the repository at this point in the history
Resolves #4993
  • Loading branch information
brandonkelly committed Sep 26, 2019
1 parent a4daaeb commit 4a00146
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Added
- Added `craft\base\ElementInterface::getIsHomepage()`. ([#4993](https://github.com/craftcms/cms/issues/4993))
- Added `craft\base\Element::HOMEPAGE_URI`.

### Fixed
- Fixed a bug where `craft\helper\UrlHelper` wasn’t encoding `+` and `&` characters in query param values.

Expand Down
20 changes: 14 additions & 6 deletions src/base/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ abstract class Element extends Component implements ElementInterface
// Constants
// =========================================================================

const HOMEPAGE_URI = '__home__';

// Statuses
// -------------------------------------------------------------------------

Expand Down Expand Up @@ -1369,6 +1371,14 @@ public function getRoute()
return $this->route();
}

/**
* @inheritdoc
*/
public function getIsHomepage(): bool
{
return $this->uri === self::HOMEPAGE_URI;
}

/**
* @inheritdoc
*/
Expand All @@ -1378,7 +1388,7 @@ public function getUrl()
return null;
}

$path = ($this->uri === '__home__') ? '' : $this->uri;
$path = $this->getIsHomepage() ? '' : $this->uri;
return UrlHelper::siteUrl($path, null, null, $this->siteId);
}

Expand Down Expand Up @@ -1439,7 +1449,7 @@ public function getPreviewTargets(): array
'label' => Craft::t('app', 'Primary {type} page', [
'type' => StringHelper::toLowerCase(static::displayName()),
]),
'url' => $this->uri === '__home__' ? '' : $this->uri
'url' => $this->getIsHomepage() ? '' : $this->uri
];
}

Expand Down Expand Up @@ -2391,9 +2401,7 @@ protected function tableAttributeHtml(string $attribute): string
$url = $this->getUrl();

if ($url !== null) {
$value = $this->uri;

if ($value === '__home__') {
if ($this->getIsHomepage()) {
$value = Html::tag('span', '', [
'data-icon' => 'home',
'title' => Craft::t('app', 'Homepage'),
Expand All @@ -2410,7 +2418,7 @@ protected function tableAttributeHtml(string $attribute): string
$replace[] = $wordSeparator . '<wbr>';
}

$value = str_replace($find, $replace, $value);
$value = str_replace($find, $replace, $this->uri);
}

return Html::a(Html::tag('span', $value, ['dir' => 'ltr']), $url, [
Expand Down
8 changes: 8 additions & 0 deletions src/base/ElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,14 @@ public function getSearchKeywords(string $attribute): string;
*/
public function getRoute();

/**
* Returns whether this element represents the site homepage.
*
* @retern bool
* @since 3.3.6
*/
public function getIsHomepage(): bool;

/**
* Returns the element’s full URL.
*
Expand Down
3 changes: 2 additions & 1 deletion src/controllers/SectionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace craft\controllers;

use Craft;
use craft\base\Element;
use craft\elements\Entry;
use craft\helpers\Json;
use craft\helpers\UrlHelper;
Expand Down Expand Up @@ -171,7 +172,7 @@ public function actionSaveSection()
$siteSettings->siteId = $site->id;

if ($section->type === Section::TYPE_SINGLE) {
$siteSettings->uriFormat = ($postedSettings['singleHomepage'] ?? false) ? '__home__' : ($postedSettings['singleUri'] ?? null);
$siteSettings->uriFormat = ($postedSettings['singleHomepage'] ?? false) ? Element::HOMEPAGE_URI : ($postedSettings['singleUri'] ?? null);
} else {
$siteSettings->uriFormat = $postedSettings['uriFormat'] ?? null;
$siteSettings->enabledByDefault = (bool)$postedSettings['enabledByDefault'];
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/ElementHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static function isTempSlug(string $slug): bool
public static function createSlug(string $str): string
{
// Special case for the homepage
if ($str === '__home__') {
if ($str === Element::HOMEPAGE_URI) {
return $str;
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/Elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public function getElementByUri(string $uri, int $siteId = null, bool $enabledOn
}

if ($uri === '') {
$uri = '__home__';
$uri = Element::HOMEPAGE_URI;
}

if ($siteId === null) {
Expand Down

0 comments on commit 4a00146

Please sign in to comment.