silverstripe / tagfield
Tag field for SilverStripe
Installs: 964 037
Dependents: 51
Suggesters: 0
Security: 0
Stars: 57
Watchers: 16
Forks: 74
Type:silverstripe-vendormodule
Requires
- php: ^8.3
- silverstripe/framework: ^6
- silverstripe/versioned: ^3
Requires (Dev)
- 4.x-dev
- 4.0.x-dev
- 4.0.0-alpha1
- 3.x-dev
- 3.3.x-dev
- 3.3.0
- 3.3.0-rc1
- 3.3.0-beta1
- 3.2.x-dev
- 3.2.2
- 3.2.1
- 3.2.0
- 3.2.0-rc1
- 3.2.0-beta1
- 3.1.x-dev
- 3.1.2
- 3.1.1
- 3.1.0
- 3.1.0-rc1
- 3.1.0-beta1
- 3.0.x-dev
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 3.0.0-rc1
- 3.0.0-beta1
- 2.x-dev
- 2.11.x-dev
- 2.11.3
- 2.11.2
- 2.11.1
- 2.11.0
- 2.11.0-rc1
- 2.11.0-beta1
- 2.10.x-dev
- 2.10.2
- 2.10.1
- 2.10.0
- 2.9.x-dev
- 2.9.0
- 2.9.0-rc1
- 2.9.0-beta1
- 2.8.x-dev
- 2.8.0
- 2.8.0-rc1
- 2.8.0-beta1
- 2.7.x-dev
- 2.7.1
- 2.7.0
- 2.7.0-rc1
- 2.7.0-beta1
- 2.7.0-alpha1
- 2.6.x-dev
- 2.6.1
- 2.6.0
- 2.6.0-rc1
- 2.6.0-beta1
- 2.5.x-dev
- 2.5.0
- 2.4.x-dev
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.x-dev
- 2.3.0
- 2.2.x-dev
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.2.0-beta1
- 2.2.0-alpha1
- 2.1.x-dev
- 2.1.1
- 2.1.0
- 2.0.x-dev
- 2.0.1
- 2.0.0
- 2.0.0-alpha1
- 1.4.x-dev
- 1.4.0-rc1
- 1.3.x-dev
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.x-dev
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.x-dev
- 1.1.1
- 1.1.0
- 1.0.x-dev
- 1.0.1
- 1.0.0
- 0.5.0
- dev-dependabot/npm_and_yarn/nanoid-3.3.8
- dev-master
This package is auto-updated.
Last update: 2024-12-14 09:55:57 UTC
README
Custom tag input field, for SilverStripe.
Installation
composer require silverstripe/tagfield
Overview
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.