Skip to content

Commit

Permalink
Fixed page collections containing dummy items for untranslated defaul…
Browse files Browse the repository at this point in the history
…t language [#2985]
  • Loading branch information
mahagr committed Jan 15, 2021
1 parent 3ba68bf commit 3a3b84a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* Fixed ordering issue with moving pages [grav-plugin-admin#2015](https://github.com/getgrav/grav-plugin-admin/issues/2015)
* Fixed Flex Pages cache not invalidating if saving an old `Page` object [#3152](https://github.com/getgrav/grav/issues/3152)
* Fixed multiple issues with `system.language.translations: false`
* Fixed page collections containing dummy items for untranslated default language [#2985](https://github.com/getgrav/grav/issues/2985)

# v1.7.0-rc.20
## 12/15/2020
Expand Down
42 changes: 42 additions & 0 deletions system/src/Grav/Common/Page/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,48 @@ public function modular()
return $this;
}

/**
* Creates new collection with only translated pages
*
* @return Collection The collection with only published pages
* @internal
*/
public function translated()
{
$published = [];

foreach ($this->items as $path => $slug) {
$page = $this->pages->get($path);
if ($page !== null && $page->translated()) {
$published[$path] = $slug;
}
}
$this->items = $published;

return $this;
}

/**
* Creates new collection with only untranslated pages
*
* @return Collection The collection with only non-published pages
* @internal
*/
public function nonTranslated()
{
$published = [];

foreach ($this->items as $path => $slug) {
$page = $this->pages->get($path);
if ($page !== null && !$page->translated()) {
$published[$path] = $slug;
}
}
$this->items = $published;

return $this;
}

/**
* Creates new collection with only published pages
*
Expand Down
14 changes: 14 additions & 0 deletions system/src/Grav/Common/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class Page implements PageInterface

/** @var string|null Filename. Leave as null if page is folder. */
protected $name;
/** @var bool */
protected $initialized = false;
/** @var string */
protected $folder;
/** @var string */
Expand Down Expand Up @@ -183,6 +185,8 @@ public function init(SplFileInfo $file, $extension = null)
{
$config = Grav::instance()['config'];

$this->initialized = true;

// some extension logic
if (empty($extension)) {
$this->extension('.' . $file->getExtension());
Expand Down Expand Up @@ -1048,6 +1052,15 @@ public function rawMarkdown($var = null)
return $this->raw_content;
}

/**
* @return bool
* @internal
*/
public function translated(): bool
{
return $this->initialized;
}

/**
* Get file object to the page.
*
Expand Down Expand Up @@ -2597,6 +2610,7 @@ public function collection($params = 'content', $pagination = true)
throw new InvalidArgumentException('Argument should be either header variable name or array of parameters');
}

$params['filter'] = ($params['filter'] ?? []) + ['translated' => true];
if (!$pagination) {
$params['pagination'] = false;
}
Expand Down
7 changes: 7 additions & 0 deletions system/src/Grav/Common/Page/Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,13 @@ public function getCollection(array $params = [], array $context = [])
}

switch ($type) {
case 'translated':
if ($filter) {
$collection = $collection->translated();
} else {
$collection = $collection->nonTranslated();
}
break;
case 'published':
if ($filter) {
$collection = $collection->published();
Expand Down

0 comments on commit 3a3b84a

Please sign in to comment.