How to use GridList of one page into home page in shortcode in SilverStripe

63 views Asked by At

I am using SilverStripe and I am stuck on one problem.

I have created a custom NewsPage page type and a News DataObject:

NewsPage

class NewsPage extends Page
{
    private static $has_many = array(
        'News' => 'News'
    );

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        $gridFieldConfig = GridFieldConfig::create()->addComponents(
            new GridFieldToolbarHeader(),
            new GridFieldAddNewButton('toolbar-header-right'),
            new GridFieldSortableHeader(),
            new GridFieldDataColumns(),
            new GridFieldPaginator(10),
            new GridFieldEditButton(),
            new GridFieldDeleteAction(),
            new GridFieldDetailForm(),
            new GridFieldBulkManager()
        );

        $gridField = new GridField(
            'News',
            'All News Entries',
            $this->News(),
            $gridFieldConfig
        );

        $fields->addFieldToTab('Root.News', $gridField);

        return $fields;
    }
}

class NewsPage_Controller extends Page_Controller
{
}

News

class News extends DataObject
{
    private static $db = array(
        'Title' => 'Text',
        'Description' => 'HTMLText'
    );

    private static $has_one = array(
        'NewsPage' => 'NewsPage',
        'NewsImage' => 'Image'
    );

    private static $summary_fields = array(
        'Title' => 'Title',
        'NewsImage'=> 'NewsImage',
        'Description'=> 'Description'
    );

    public function getCMSFields()
    {
        $fields = new FieldList(
            new TextField('Title', 'Title'),
            new UploadField('NewsImage', 'NewsImage'),
            new HTMLEditorField('Description', 'Description')
        );

        return $fields;
    }
}

Now I want to display latest news on my home page but I don't know how to display it using shortcodes.

Here is a screenshot of the CMS. Here you can see the news section I want to display on the home page with a shortcode.

enter image description here

0

There are 0 answers