Skip to content

Instantly share code, notes, and snippets.

@hissy
Last active September 24, 2020 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hissy/fe09291fd1c683f282c74818961a0af6 to your computer and use it in GitHub Desktop.
Save hissy/fe09291fd1c683f282c74818961a0af6 to your computer and use it in GitHub Desktop.
[concrete5][V8] An automated job to restore "Page Not Found" page

It should exist only one "Page Not Found" page with "/page_not_found" path on concrete5 system. However, you're able to delete that page or change the URL accidentally. You can install it from dashboard, but it won't work correctly if you enables multilingual sections. If it happened, sometimes concrete5 can't render 404 correctly and may occur an infinite loop. I got a Segmentation fault error due to this case.

This job was written for re-installing "/page_not_found" page correctly. Please upload this restore_page_not_found.php file into your /application/jobs directory, then install it from Dashboard > System & Settings > Optimization > Automated Jobs page. After install it, just push the "Run" button.

<?php
namespace Application\Job;
use Concrete\Core\Job\Job;
use Concrete\Core\Page\Page;
use Concrete\Core\Page\Single;
class RestorePageNotFound extends Job
{
public function run()
{
$item = '/page_not_found';
$c = Page::getByPath($item);
if (is_object($c) && !$c->isError()) {
return t('You have already correct /page_not_found page.');
}
$pageNotFound = null;
$singlePages = Single::getList();
foreach ($singlePages as $singlePage) {
$fileName = $singlePage->getCollectionFilename();
if ($fileName === '/page_not_found.php') {
$pageNotFound = $singlePage;
break 1;
}
}
if (is_object($pageNotFound)) {
$pageNotFound->delete();
}
$newPageNotFound = Single::addGlobal('/page_not_found');
if (is_object($newPageNotFound)) {
$newPageNotFound->setCanonicalPagePath('/page_not_found');
return t('Successfully created a new /page_not_found page.');
}
return t('Something wrong happened when creating a new /page_not_found page.');
}
public function getJobName()
{
return t('Restore Page Not Found');
}
public function getJobDescription()
{
return t('Delete existing page not found page that did not created properly, then reinstall it with correct path.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment