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