Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the injector to instantiate a new Page. #1641

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion code/Controllers/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\i18n\i18n;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataModel;
Expand Down Expand Up @@ -67,7 +68,7 @@ class ContentController extends Controller {
*/
public function __construct($dataRecord = null) {
if(!$dataRecord) {
$dataRecord = new Page();
$dataRecord = Injector::inst()->create('Page');
if($this->hasMethod("Title")) {
$dataRecord->Title = $this->Title();
}
Expand Down
2 changes: 1 addition & 1 deletion code/Model/ErrorPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ protected function requireDefaultRecordFixture($defaultData) {
$page = ErrorPage::get()->filter('ErrorCode', $code)->first();
$pageExists = !empty($page);
if(!$pageExists) {
$page = new ErrorPage($defaultData);
$page = Injector::inst()->create(self::class, $defaultData);
$page->write();
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
}
Expand Down
43 changes: 26 additions & 17 deletions code/Model/SiteTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\CompositeField;
Expand Down Expand Up @@ -1515,37 +1516,45 @@ public function requireDefaultRecords() {
// default pages
if(static::class == self::class && $this->config()->create_default_pages) {
if(!SiteTree::get_by_link(RootURLController::config()->default_homepage_link)) {
$homepage = new Page();
$homepage->Title = _t('SiteTree.DEFAULTHOMETITLE', 'Home');
$homepage->Content = _t('SiteTree.DEFAULTHOMECONTENT', '<p>Welcome to SilverStripe! This is the default homepage. You can edit this page by opening <a href="admin/">the CMS</a>.</p><p>You can now access the <a href="http://docs.silverstripe.org">developer documentation</a>, or begin the <a href="http://www.silverstripe.org/learn/lessons">SilverStripe lessons</a>.</p>');
$homepage->URLSegment = RootURLController::config()->default_homepage_link;
$homepage->Sort = 1;
$homepageData = array(
'Title' => _t('SiteTree.DEFAULTHOMETITLE', 'Home'),
'Content' => _t('SiteTree.DEFAULTHOMECONTENT', '<p>Welcome to SilverStripe! This is the default homepage. You can edit this page by opening <a href="admin/">the CMS</a>.</p><p>You can now access the <a href="http://docs.silverstripe.org">developer documentation</a>, or begin the <a href="http://www.silverstripe.org/learn/lessons">SilverStripe lessons</a>.</p>'),
'URLSegment' => RootURLController::config()->default_homepage_link,
'Sort' => 1
);
$homepage = Injector::inst()->create('Page', $homepageData);
$homepage->write();
$homepage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$homepage->flushCache();
DB::alteration_message('Home page created', 'created');
}

if(DB::query("SELECT COUNT(*) FROM \"SiteTree\"")->value() == 1) {
$aboutus = new Page();
$aboutus->Title = _t('SiteTree.DEFAULTABOUTTITLE', 'About Us');
$aboutus->Content = _t(
'SiteTree.DEFAULTABOUTCONTENT',
'<p>You can fill this page out with your own content, or delete it and create your own pages.</p>'
// Create an About Us page
$aboutusData = array(
'Title' => _t('SiteTree.DEFAULTABOUTTITLE', 'About Us'),
'Content' => _t(
'SiteTree.DEFAULTABOUTCONTENT',
'<p>You can fill this page out with your own content, or delete it and create your own pages.</p>'
),
'Sort' => 2
);
$aboutus->Sort = 2;
$aboutus = Injector::inst()->create('Page', $aboutusData);
$aboutus->write();
$aboutus->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$aboutus->flushCache();
DB::alteration_message('About Us page created', 'created');

$contactus = new Page();
$contactus->Title = _t('SiteTree.DEFAULTCONTACTTITLE', 'Contact Us');
$contactus->Content = _t(
'SiteTree.DEFAULTCONTACTCONTENT',
'<p>You can fill this page out with your own content, or delete it and create your own pages.</p>'
// Create a Contact Us page
$contactusData = array(
'Title' => _t('SiteTree.DEFAULTCONTACTTITLE', 'Contact Us'),
'Content' => _t(
'SiteTree.DEFAULTCONTACTCONTENT',
'<p>You can fill this page out with your own content, or delete it and create your own pages.</p>'
),
'Sort' => 3
);
$contactus->Sort = 3;
$contactus = Injector::inst()->create('Page', $contactusData);
$contactus->write();
$contactus->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$contactus->flushCache();
Expand Down