silverstripe-tagfield/readme.md

110 lines
3.3 KiB
Markdown
Raw Permalink Normal View History

2015-09-09 01:07:51 +02:00
# Tag Field
Custom tag input field, for SilverStripe.
2021-01-21 04:42:50 +01:00
[![Build Status](https://api.travis-ci.com/silverstripe/silverstripe-tagfield.svg?branch=2)](https://travis-ci.com/silverstripe/silverstripe-tagfield)
2018-11-15 19:37:14 +01:00
[![Code Quality](http://img.shields.io/scrutinizer/g/silverstripe/silverstripe-tagfield.svg?style=flat)](https://scrutinizer-ci.com/g/silverstripe/silverstripe-tagfield)
[![Code coverage](https://codecov.io/gh/silverstripe/silverstripe-tagfield/branch/master/graph/badge.svg)](https://codecov.io/gh/silverstripe/silverstripe-tagfield)
[![Version](http://img.shields.io/packagist/v/silverstripe/tagfield.svg?style=flat)](https://packagist.org/packages/silverstripe/tagfield)
[![License](http://img.shields.io/packagist/l/silverstripe/tagfield.svg?style=flat)](license.md)
2018-11-15 19:37:14 +01:00
[![SilverStripe supported module](https://img.shields.io/badge/silverstripe-supported-0071C4.svg)](https://www.silverstripe.org/software/addons/silverstripe-commercially-supported-module-list/)
2015-09-09 01:07:51 +02:00
## Overview
2019-07-03 23:11:50 +02:00
![Screenshot](docs/en/screenshot.png)
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).
2015-09-09 01:07:51 +02:00
## Requirements
* SilverStripe 4.0
2015-09-09 01:07:51 +02:00
## Installing
```sh
$ composer require silverstripe/tagfield
```
## Using
### Relational Tags
```php
use SilverStripe\ORM\DataObject;
class BlogPost extends DataObject
{
private static $many_many = [
'BlogTags' => BlogTag::class
];
2015-09-09 01:07:51 +02:00
}
```
```php
use SilverStripe\ORM\DataObject;
class BlogTag extends DataObject
{
private static $db = [
2015-09-09 01:07:51 +02:00
'Title' => 'Varchar(200)',
];
2015-09-09 01:07:51 +02:00
private static $belongs_many_many = [
'BlogPosts' => BlogPost::class
];
2015-09-09 01:07:51 +02:00
}
```
```php
$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
```
2019-02-13 13:22:45 +01:00
**Note:** This assumes you have imported the namespaces class, e.g. use SilverStripe\TagField\TagField;
2015-09-09 01:07:51 +02:00
### String Tags
```php
use SilverStripe\ORM\DataObject;
class BlogPost extends DataObject
{
private static $db = [
'Tags' => 'Text',
];
2015-09-09 01:07:51 +02:00
}
```
```php
$field = StringTagField::create(
'Tags',
'Tags',
['one', 'two'],
2015-09-09 01:07:51 +02:00
explode(',', $this->Tags)
);
$field->setShouldLazyLoad(true); // tags should be lazy loaded
```
You can find more in-depth documentation in [docs/en](docs/en/introduction.md).
## Versioning
This library follows [Semver](http://semver.org). 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](http://github.com/silverstripe/silverstripe-tagfield/issues) for any bugs you've found, or features you're missing.