Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Add tab to Page via module


Go to End


2 Posts   1482 Views

Avatar
omnicurse

Community Member, 8 Posts

7 April 2014 at 8:17am

Edited: 07/04/2014 12:33pm

Hello,

I'm trying to create a standalone module that will add a new tab to the standard Page type page in the CMS.

This was easy enough to do using a DataExtension but then whenever I tried to add a dataobject to that dataextension via the gridfield I was getting "canView()[..]" errors.

So I back tracked and in my module created a file named Page and added the code I wanted in there and that worked fine with the exception that when I created another module using the same method, the CMS would only show the first module and not the second.

Whats the best approach here? Should I be using a DataExtesion and figuring out what I can't attach objects to it? Or should I be figuring out why having two modules with both the class Page override the last loaded?

edit:

class QuestionAnswer extends DataExtension
{
    private static $db = array(
        'TestTextField' => 'Varchar(3)',
    );

    private static $has_many = array(
        'FAQ' => 'FAQ'
    );

    public function updateCMSFields(FieldList $fields)
    {
        $fields->addFieldsToTab('Root.QuestionsAndAnswers', array(
            HeaderField::create("Questions"),
            GridField::create('FAQ', 'Questions and Answers', FAQ::get(), GridFieldConfig_RecordEditor::create())
        ));
    }

class FAQ extends DataObject
{
    private static $db = array(
        'Title' => 'Varchar(155)',
        'Answer' => 'HTMLText',
        'SortOrder' => 'Int'
    );

    private static $has_one = array(
        'QuestionAnswer' => 'QuestionAnswer'
    );
}

This technically works, but obviously returns all the FAQ objects, rather than just the ones you'd expect. Can someone explain how to resolve this? Using..

   GridField::create('FAQ', 'Questions and Answers', $this->FAQ(), GridFieldConfig_RecordEditor::create())

Just returns a white screen (even when in dev, with php debugging enabled) so I'm a little lost.

edit again:

I've solved it by making the dataobject FAQ belong to Page and then referencing $this->owner->FAQ() on the gridfield. This feels really dirty and hackery... Is there a better solution?

Avatar
Willr

Forum Moderator, 5523 Posts

7 April 2014 at 4:16pm

'QuestionAnswer' you cannot link a relation to this object. The extension is on Page correct? so you should link the relation to 'Page'.