Skip to content

Commit

Permalink
Fixed root node creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck Allimant committed Aug 26, 2024
1 parent 066fc50 commit 122bcaf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>1.2.5</version>
<version>1.2.6</version>
<authors>
<author>
<name>Damien Foulhoux</name>
Expand Down
21 changes: 21 additions & 0 deletions Model/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Page\Model\Base\Page as BasePage;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Exception\PropelException;
use Thelia\Core\Translation\Translator;
use Thelia\Model\Tools\UrlRewritingTrait;

/**
Expand All @@ -25,6 +27,25 @@ public function getRewrittenUrlViewName()
return 'page';
}

/**
* Be sur to have a locale and a title when creating a root node
*
* @param string $locale
* @return $this|Page
* @throws PropelException*
*/
public function safeMakeRoot(string $locale)
{
if ($locale !== $this->getLocale()) {
$this->setLocale($locale);
}

if (! $this->getTitle()) {
$this->setTitle(Translator::getInstance()->trans("Root node", [], \Page\Page::DOMAIN_NAME));
}

return $this->makeRoot();
}
protected function createSlug()
{
// create the slug based on the `slug_pattern` and the object properties
Expand Down
3 changes: 2 additions & 1 deletion Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Thelia\Core\Template\TemplateDefinition;
use Thelia\Install\Database;
use Thelia\Model\ConfigQuery;
use Thelia\Model\Lang;
use Thelia\Module\BaseModule;

class Page extends BaseModule
Expand Down Expand Up @@ -128,7 +129,7 @@ public function update($currentVersion, $newVersion, ConnectionInterface $con =

$pageRoot = new \Page\Model\Page();
$pageRoot->setCode('root');
$pageRoot->makeRoot();
$pageRoot->safeMakeRoot(Lang::getDefaultLanguage()->getLocale());
$pageRoot->save();

foreach ($stmt->fetchAll(\PDO::FETCH_ASSOC) as $pageData) {
Expand Down
9 changes: 4 additions & 5 deletions Service/PageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function createPage(
string $locale = 'en_US',
int $parentId = null
): void {

if (!$blockGroupId) {
$newBlockGroup = new BlockGroup();
$newBlockGroup
Expand All @@ -45,7 +45,7 @@ public function createPage(

$blockGroupId = $newBlockGroup->getId();
}

$parent = null;
if (null !== $parentId) {
$parent = PageQuery::create()->filterById($parentId)->findOne();
Expand All @@ -57,13 +57,12 @@ public function createPage(

if (null === $parent) {
$root = new Page();
$root->makeRoot();
$root->save();
$root->safeMakeRoot($locale)->save();
$parent = $root;
}

$page = new Page();

$page
->setLocale($locale)
->setTitle($title)
Expand Down

0 comments on commit 122bcaf

Please sign in to comment.