Search code examples
silverstripe-4

Can I have 2 gridfield components in an admin page in Silverstripe 4


Can I have 2 gridfield components in an admin page named differently from the same dataobject - eg

   class MainLandingPage_au extends Page
   {

    private static $has_many = [
       'ImagesWithHtml' => ImageWithHtml::class,
       'ImagesWithHtml2' => ImageWithHtml::class,
    ];

    // ...
    $fields->addFieldToTab('Root.Section1', HtmlEditorField::create('Section1Title','Section 1 Title')->setRows(4));
    $fields->addFieldToTab('Root.Section1', GridField::create(
        'ImagesWithHtml',
        'Images With Html For This Page',
        $this->ImagesWithHtml(),
        GridFieldConfig_RecordEditor::create()
    ));

    $fields->addFieldToTab('Root.Section2', HtmlEditorField::create('Section2Title','Section 2 Title')->setRows(4));
    $fields->addFieldToTab('Root.Section2', GridField::create(
        'ImagesWithHtml2',
        'Images With Html For Section 2',
        $this->ImagesWithHtml2(),
        GridFieldConfig_RecordEditor::create()
    ));

Solution

  • Yes can use 2 gridfields this worked for the proper individual relations

    class MainLandingPage_au extends Page
       {
    
        private static $has_many = [
           'ImagesWithHtml' => ImageWithHtml::class . '.AuMainLandingPage',
           'ImagesWithHtml2' => ImageWithHtml::class . '.AuMainLandingPage2'
        ];
    
        // ...
        // Gridfields as in posted question
        // ... etc
    

    Other side

    class ImageWithHtml extends DataObject
        {
    
        private static $has_one = [
           'AuMainLandingPage' => AuMainLandingPage::class . '.ImagesWithHtml',
           'AuMainLandingPage2' => AuMainLandingPage::class . '.ImagesWithHtml2'
        ];