Search code examples
silverstripesilverstripe-4

How to publish pages via Model Admin in SilverStripe 4?


I have created the Page, Data Object and Model Admin given below.

Page

class MyPage extends Page {

  private static $db = [];
  private static $singular_name = "My Page";
  private static $plural_name = "My Pages";
  private static $description = 'A page created for testing';
}

Data Object

use SilverStripe\ORM\DataObject;

class MyDataObject extends DataObject {

  private static $db = [
    'Test' => 'Int'
  ];
}

Model Admin

<?php

use SilverStripe\Admin\ModelAdmin;

class MyAdmin extends ModelAdmin  {

    private static $managed_models = [
        'MyPage',
        'MyDataObject'
    ];

    private static $url_segment = 'my-pages';
    private static $menu_title = 'My Page Admin';
}

When I create a new "MyDataObject" it creates and publishes the object when I click the save button. But when I create a "MyPage" it is still a draft after clicking the save button. I have to open the page from Pages Tree and hit the Publish button to publish the page.


Solution

  • You have to change your page's code as shown below. (Just add this line - private static $versioned_gridfield_extensions = true; )

    <?php
    
    class MyPage extends Page {
    
      private static $versioned_gridfield_extensions = true;
    
      private static $db = [];
      private static $singular_name = "My Page";
      private static $plural_name = "My Pages";
      private static $description = 'A page created for testing';
    
    }