|
| 1 | +--- |
| 2 | +title: TagField |
| 3 | +summary: Provides a custom form field for adding tags to content, supporting either related data objects or simple string-based tags |
| 4 | +icon: tags |
| 5 | +--- |
| 6 | + |
| 7 | +# TagField |
| 8 | + |
| 9 | +The TagField module provides a custom form field for adding tags to content. It supports creating tags as related data objects or as a simple comma-separated list of strings. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +```bash |
| 14 | +composer require silverstripe/tagfield |
| 15 | +``` |
| 16 | + |
| 17 | +Then run `/dev/build`, from either the command line or the browser. |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +The primary use, for this module, is as a custom input field interface. For instance, imagine you had the following data objects: |
| 22 | + |
| 23 | +```php |
| 24 | +namespace App\Models; |
| 25 | + |
| 26 | +class BlogPost extends DataObject |
| 27 | +{ |
| 28 | + private static $many_many = [ |
| 29 | + 'BlogTags' => BlogTag::class, |
| 30 | + ]; |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +```php |
| 35 | +namespace App\Models; |
| 36 | + |
| 37 | +class BlogTag extends DataObject |
| 38 | +{ |
| 39 | + private static $db = [ |
| 40 | + 'Title' => 'Varchar(200)', |
| 41 | + ]; |
| 42 | + |
| 43 | + private static $belongs_many_many = [ |
| 44 | + 'BlogPosts' => BlogPost::class, |
| 45 | + ]; |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +If you wanted to link blog tags to blog posts, you might override [`DataObject::getCMSFields()`](api:SilverStripe\ORM\DataObject::getCMSFields()) with the following field: |
| 50 | + |
| 51 | +```php |
| 52 | +$field = TagField::create( |
| 53 | + 'BlogTags', |
| 54 | + 'Blog Tags', |
| 55 | + BlogTag::get(), |
| 56 | + $this->BlogTags() |
| 57 | +) |
| 58 | + // tags should be lazy loaded |
| 59 | + ->setShouldLazyLoad(true) |
| 60 | + // new tag DataObjects can be created |
| 61 | + ->setCanCreate(true); |
| 62 | +``` |
| 63 | + |
| 64 | +**Note:** This assumes you have imported the namespaces class, e.g. `use SilverStripe\TagField\TagField;`. |
| 65 | + |
| 66 | +This will present a tag field, in which you can select existing blog tags or create new ones. They will be created/linked after the blog posts are saved. |
| 67 | + |
| 68 | +### StringTagField |
| 69 | + |
| 70 | +You can also store string-based tags, with the following field type: |
| 71 | + |
| 72 | +```php |
| 73 | +$field = StringTagField::create( |
| 74 | + 'Tags', |
| 75 | + 'Tags', |
| 76 | + ['one', 'two'], |
| 77 | + explode(',', $this->Tags) |
| 78 | +) |
| 79 | + ->setCanCreate(true) |
| 80 | + ->setShouldLazyLoad(true); |
| 81 | +``` |
| 82 | + |
| 83 | +This assumes you are storing tags in the following data object structure: |
| 84 | + |
| 85 | +```php |
| 86 | +namespace App\Models; |
| 87 | + |
| 88 | +class BlogPost extends DataObject |
| 89 | +{ |
| 90 | + private static $db = [ |
| 91 | + 'Tags' => 'Text', |
| 92 | + ]; |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +In the above code example, the options available (whether lazy loaded or not) would be "one" and "two", and the |
| 97 | +user would be able to create new options. Whichever tags are chosen would be stored in the BlogPost's `Tags` field |
| 98 | +as a comma-delimited string. |
| 99 | + |
| 100 | +## Using tagfield with silverstripe-taxonomy |
| 101 | + |
| 102 | +TagField assumes that objects have a `Title` field. For classes without a `Title` field |
| 103 | +use [`TagField::setTitleField()`](api:SilverStripe\TagField\TagField::setTitleField()) to modify accordingly. |
| 104 | + |
| 105 | +```php |
| 106 | +$field = TagField::create( |
| 107 | + 'Tags', |
| 108 | + 'Blog Tags', |
| 109 | + TaxonomyTerm::get() |
| 110 | +) |
| 111 | + ->setTitleField('Name'); |
| 112 | +``` |
0 commit comments