Go to file
2024-05-17 15:17:27 +12:00
_config Setup TagField to work within AssetAdmin (Fixes #107) 2019-07-25 12:13:44 +12:00
.github MNT Run module-standardiser 2024-05-17 15:17:27 +12:00
client DEP Update JS dependencies (#284) 2024-04-02 14:16:45 +13:00
docs/en FIX Selected tag is not shown when isMutliple is false (#201) 2022-06-07 13:00:12 +12:00
src ENH Add generic types (#277) 2024-01-23 13:08:06 +13:00
templates/SilverStripe/TagField Additional changes to convert tagfield to use react-select 2018-07-16 13:27:11 +12:00
tests Merge branch '2' into 3.0 2023-04-27 14:56:05 +12:00
_config.php FIX Update module for latest SS4, vendorise 2017-10-18 14:19:15 +13:00
.editorconfig FIX Selected tag is not shown when isMutliple is false (#201) 2022-06-07 13:00:12 +12:00
.eslintrc.js Additional changes to convert tagfield to use react-select 2018-07-16 13:27:11 +12:00
.gitattributes Added standard git attributes 2015-11-19 19:13:48 +13:00
.gitignore FIX Adding a debounce to fetching lazy loaded tags with tag field 2018-12-04 16:15:47 +13:00
.nvmrc DEP Upgrade frontend build stack (#229) 2023-02-02 10:13:18 +13:00
.ss-storybook.js FIX Ensure tagfield is compatible with both React and Entwine contexts 2019-07-25 12:13:50 +12:00
.stylelintignore MNT Replace sass-lint with stylelint (#285) 2024-05-01 16:58:46 +12:00
.stylelintrc.js MNT Replace sass-lint with stylelint (#285) 2024-05-01 16:58:46 +12:00
babel.config.json DEP Upgrade frontend build stack (#229) 2023-02-02 10:13:18 +13:00
code-of-conduct.md Added standard code of conduct 2015-11-21 20:17:21 +13:00
codecov.yml FIX Update module for latest SS4, vendorise 2017-10-18 14:19:15 +13:00
composer.json MNT Run module-standardiser (#279) 2024-02-02 14:08:19 +13:00
LICENSE MNT Run module-standardiser 2023-08-14 15:49:30 +12:00
package.json MNT Replace sass-lint with stylelint (#285) 2024-05-01 16:58:46 +12:00
phpcs.xml.dist MNT Use shared travis config, use sminnee/phpunit 2020-11-10 15:37:53 +13:00
phpstan.neon.dist MNT Run module-standardiser (#279) 2024-02-02 14:08:19 +13:00
phpunit.xml.dist MNT Standardise modules 2022-08-01 16:23:29 +12:00
README.md DOC Update readme example 2023-11-20 13:19:19 +13:00
webpack.config.js DEP Upgrade frontend build stack (#229) 2023-02-02 10:13:18 +13:00
yarn.lock Merge branch '3.2' into 3 2024-05-01 17:31:20 +12:00

Tag Field

CI Silverstripe supported module

Custom tag input field, for SilverStripe.

Installation

composer require silverstripe/tagfield

Overview

Screenshot

Allows storing tags as a relationship, or comma-delimited strings. Supports autocompletion with lazy-loading.

Note: The field is optimised for usage in the Silverstripe CMS UI. The form field class itself can be used outside of the CMS, but you'll need to build your own frontend to interpret the raw field data (data-schema attribute).

Using

Relational Tags

use SilverStripe\ORM\DataObject;

class BlogPost extends DataObject
{
    private static $many_many = [
        'BlogTags' => BlogTag::class
    ];
}
use SilverStripe\ORM\DataObject;

class BlogTag extends DataObject
{
    private static $db = [
        'Title' => 'Varchar(200)',
    ];

    private static $belongs_many_many = [
        'BlogPosts' => BlogPost::class
    ];
}
$field = TagField::create(
    'BlogTags',
    'Blog Tags',
    BlogTag::get(),
    $this->BlogTags()
)
    ->setShouldLazyLoad(true) // tags should be lazy loaded
    ->setCanCreate(true);     // new tag DataObjects can be created

Note: This assumes you have imported the namespaces class, e.g. use SilverStripe\TagField\TagField;

Has-One Relations

You can also use the TagField to select values for has_one relations. Let's assume, that a BlogPost has one BlogCategory.

class BlogCategory extends DataObject
{
    private static $db = [
        'Title' => 'Varchar(200)',
    ];
}
use SilverStripe\ORM\DataObject;

class BlogPost extends DataObject
{
    private static $has_one = [
        'BlogCategory' => BlogCategory::class
    ];
}
$field = TagField::create(
    'BlogCategoryID',
    $this->fieldLabel('BlogCategory'),
    BlogCategory::get()
)
    ->setIsMultiple(false)
    ->setCanCreate(true);

Note: We're using the ID suffix for the field-name (eg. BlogCategoryID instead of BlogCategory) and only allow one value by setting ->setIsMultiple(false)

String Tags

use SilverStripe\ORM\DataObject;

class BlogPost extends DataObject
{
    private static $db = [
        'Tags' => 'Text',
    ];
}
$field = StringTagField::create(
    'Tags',
    'Tags',
    ['one', 'two'],
    explode(',', $this->Tags ?: '')
);

$field->setShouldLazyLoad(true); // tags should be lazy loaded

You can find more in-depth documentation in docs/en.

Using TagField with silverstripe-taxonomy

TagField assumes a Title field on objects. For classes without a Title field use setTitleField to modify accordingly.

$field = TagField::create(
    'Tags',
    'Blog Tags',
    TaxonomyTerm::get(),
)
    ->setTitleField('Name');

Versioning

This library follows Semver. According to Semver, you will be able to upgrade to any minor or patch version of this library without any breaking changes to the public API. Semver also requires that we clearly define the public API for this library.

All methods, with public visibility, are part of the public API. All other methods are not part of the public API. Where possible, we'll try to keep protected methods backwards-compatible in minor/patch versions, but if you're overriding methods then please test your work before upgrading.

Reporting Issues

Please create an issue for any bugs you've found, or features you're missing.