Skip to content

ENH Refactor TagField from class to functional component#337

Merged
GuySartorelli merged 1 commit intosilverstripe:4from
creative-commoners:pulls/4/refactor-func-comp
Nov 10, 2025
Merged

ENH Refactor TagField from class to functional component#337
GuySartorelli merged 1 commit intosilverstripe:4from
creative-commoners:pulls/4/refactor-func-comp

Conversation

@emteknetnz
Copy link
Copy Markdown
Member

@emteknetnz emteknetnz commented Nov 6, 2025

Issue #336

Can test with the following:

// app/src/BlogPostAdmin.php

use SilverStripe\Admin\ModelAdmin;

class BlogPostAdmin extends ModelAdmin
{
    private static string $url_segment = 'blog-post-admin';
    private static string $menu_title = 'Blog Post Admin';
    private static array $managed_models = [
        BlogPost::class,
        BlogTag::class,
    ];
}

// app/src/BlogPost.php

use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\DataObject;
use SilverStripe\TagField\TagField;

class BlogPost extends DataObject
{
    private static string $table_name = 'BlogPost';
    private static string $singular_name = 'Blog Post';
    private static string $plural_name = 'Blog Posts';

    private static array $db = [
        'Title' => 'Varchar(255)',
        'Content' => 'Text',
    ];

    private static array $many_many = [
        'BlogTags' => BlogTag::class
    ];

    public function getCMSFields(): FieldList
    {
        $fields = parent::getCMSFields();
        $field = TagField::create(
            'BlogTags',
            'Blog Tags',
            BlogTag::get(),
            $this->BlogTags()
        )
            ->setShouldLazyLoad(true)
            ->setCanCreate(true);
        $fields->addFieldToTab('Root.Main', $field);
        return $fields;
    }
}

// app/src/BlogTag.php

use SilverStripe\ORM\DataObject;

class BlogTag extends DataObject
{
    private static string $table_name = 'BlogTag';

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

    private static array $belongs_many_many = [
        'BlogPosts' => BlogPost::class
    ];

    private static string $singular_name = 'Blog Tag';
    private static string $plural_name = 'Blog Tags';

    public function requireDefaultRecords(): void
    {
        parent::requireDefaultRecords();
        $tags = [
            'Lorem Ipsum',
            'Dolor Sit Amet',
            'Consectetur Adipiscing',
            'Elit Sed Do',
            'Eiusmod Tempor',
            'Incididunt Ut Labore',
            'Et Dolore Magna Aliqua',
            'Ut Enim Ad Minim',
        ];
        foreach ($tags as $title) {
            if (!self::get()->filter('Title', $title)->exists()) {
                $tag = self::create();
                $tag->Title = $title;
                $tag->write();
            }
        }
    }
}

@emteknetnz emteknetnz force-pushed the pulls/4/refactor-func-comp branch 4 times, most recently from a2f8dad to fdbd77b Compare November 6, 2025 03:33
Comment thread client/src/components/TagField.js Outdated
Comment thread client/src/components/TagField.js Outdated
Comment thread client/src/components/TagField.js
Comment thread client/src/components/TagField.js
Comment thread client/src/components/TagField.js
@emteknetnz emteknetnz force-pushed the pulls/4/refactor-func-comp branch from fdbd77b to 4e3ad67 Compare November 6, 2025 03:39
Comment thread client/src/components/tests/TagField-test.js
Comment thread client/src/components/TagField.js
@emteknetnz emteknetnz marked this pull request as ready for review November 6, 2025 03:41
@emteknetnz emteknetnz changed the title Refactor TagField from class to functional component ENH Refactor TagField from class to functional component Nov 6, 2025
@emteknetnz emteknetnz force-pushed the pulls/4/refactor-func-comp branch from 4e3ad67 to cd8ab7c Compare November 6, 2025 04:28
Comment thread client/src/components/TagField.js Outdated
Comment thread client/src/components/TagField.js
Comment thread client/src/components/TagField.js Outdated
Comment thread client/src/components/TagField.js Outdated
Comment thread client/src/components/TagField.js Outdated
Comment thread client/src/components/TagField.js Outdated
Comment thread client/src/components/TagField.js Outdated
Comment thread client/src/components/TagField.js Outdated
Comment thread client/src/components/TagField.js
Comment thread client/src/components/TagField.js Outdated
@emteknetnz emteknetnz force-pushed the pulls/4/refactor-func-comp branch from cd8ab7c to 80c271f Compare November 6, 2025 22:59
Copy link
Copy Markdown
Member

@GuySartorelli GuySartorelli left a comment

Choose a reason for hiding this comment

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

Seems to work as expected locally - just some variable naming to square away and possible changes to usage of callback hook as below.

Comment thread client/src/components/TagField.js Outdated
Comment thread client/src/components/TagField.js Outdated
Comment thread client/src/components/TagField.js Outdated
Comment thread client/src/components/TagField.js Outdated
@emteknetnz emteknetnz force-pushed the pulls/4/refactor-func-comp branch from 80c271f to 7649985 Compare November 10, 2025 00:29
Copy link
Copy Markdown
Member

@GuySartorelli GuySartorelli left a comment

Choose a reason for hiding this comment

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

Just the one thing now

Comment thread client/src/components/TagField.js Outdated
@emteknetnz emteknetnz force-pushed the pulls/4/refactor-func-comp branch from 7649985 to 3ab28d8 Compare November 10, 2025 02:54
Copy link
Copy Markdown
Member

@GuySartorelli GuySartorelli left a comment

Choose a reason for hiding this comment

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

LGTM

@GuySartorelli GuySartorelli merged commit 72683dd into silverstripe:4 Nov 10, 2025
10 checks passed
@GuySartorelli GuySartorelli deleted the pulls/4/refactor-func-comp branch November 10, 2025 03:05
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