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

FIX Make child responsible for its own nested gridfield #393

Merged

Conversation

GuySartorelli
Copy link
Collaborator

@GuySartorelli GuySartorelli commented May 21, 2024

Currently the parent is responsible for dictating how the child's nested gridfield should be configured. This change allows the child to determine that for themselves.

Code before this PR to get nested gridfields

<?php

namespace SilverStripe\FrameworkTest\Fields\NestedGridField;

use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use SilverStripe\ORM\DataObject;
use Symbiote\GridFieldExtensions\GridFieldNestedForm;

class NestedGridFieldParentModel extends DataObject
{
    private static $table_name = 'NestedGridFieldParentModel';

    private static $db = [
        'Title' => 'Varchar(50)',
    ];

    private static $has_many = [
        'Children' => NestedGridFieldChildModel::class,
    ];

    public function getNestedConfig(): GridFieldConfig
    {
        $config = new GridFieldConfig_RecordEditor();
        $config->addComponent(GridFieldNestedForm::create()->setRelationName('GrandChildren'));
        return $config;
    }
}
<?php

namespace SilverStripe\FrameworkTest\Fields\NestedGridField;

use SilverStripe\ORM\DataObject;

class NestedGridFieldChildModel extends DataObject
{
    private static $table_name = 'NestedGridFieldChildModel';

    private static $db = [
        'Title' => 'Varchar(50)',
    ];

    private static $has_one = [
        'Parent' => NestedGridFieldParentModel::class,
    ];

    private static $has_many = [
        'GrandChildren' => NestedGridFieldGrandChildModel::class,
    ];
}

Code after this PR to get nested gridfields

<?php

namespace SilverStripe\FrameworkTest\Fields\NestedGridField;

use SilverStripe\ORM\DataObject;

class NestedGridFieldParentModel extends DataObject
{
    private static $table_name = 'NestedGridFieldParentModel';

    private static $db = [
        'Title' => 'Varchar(50)',
    ];

    private static $has_many = [
        'Children' => NestedGridFieldChildModel::class,
    ];
}
<?php

namespace SilverStripe\FrameworkTest\Fields\NestedGridField;

use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use SilverStripe\ORM\DataObject;
use Symbiote\GridFieldExtensions\GridFieldNestedForm;

class NestedGridFieldChildModel extends DataObject
{
    private static $table_name = 'NestedGridFieldChildModel';

    private static $db = [
        'Title' => 'Varchar(50)',
    ];

    private static $has_one = [
        'Parent' => NestedGridFieldParentModel::class,
    ];

    private static $has_many = [
        'GrandChildren' => NestedGridFieldGrandChildModel::class,
    ];

    public function getNestedConfig(): GridFieldConfig
    {
        $config = new GridFieldConfig_RecordEditor();
        $config->addComponent(GridFieldNestedForm::create()->setRelationName('GrandChildren'));
        return $config;
    }
}

Other change made

This also passes in some information to the getNestedConfig() method which can be used to decide how to display itself. For example, a class which has the Hierarchy extension might want to always list itself as a nested child, but doesn't want to override the default code for that. It could be implemented like so:

<?php

namespace SilverStripe\FrameworkTest\Fields\NestedGridField;

use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\Hierarchy\Hierarchy;
use Symbiote\GridFieldExtensions\GridFieldNestedForm;

class NestedGridFieldGrandChildModel extends DataObject
{
    private static $table_name = 'NestedGridFieldGrandChildModel';

    private static $db = [
        'Title' => 'Varchar(50)',
        'Category'=>'Varchar(255)',
        'Tag'=>'Varchar(255)',
    ];

    private static $has_one = [
        'ChildParent' => NestedGridFieldChildModel::class,
    ];

    private static $extensions = [
        Hierarchy::class,
    ];

    public function getNestedConfig(string $parentClass, string $asRelation): ?GridFieldConfig
    {
        // Don't override default hierarchy nested gridfield
        if (is_a($parentClass, NestedGridFieldGrandChildModel::class) && $asRelation === 'Children') {
            return null;
        }
        // Provide nested gridfield to start showing hierarchy
        $config = new GridFieldConfig_RecordEditor();
        $config->addComponent(GridFieldNestedForm::create()->setRelationName('Children'));
        return $config;
    }
}

Issue

Copy link
Collaborator

@sabina-talipova sabina-talipova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Test locally. Works perfect.

@sabina-talipova sabina-talipova merged commit 4501565 into symbiote:4 May 21, 2024
9 checks passed
@sabina-talipova sabina-talipova deleted the pulls/4/nested-gridfield branch May 21, 2024 23:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants