mirror of
https://github.com/silverstripe/silverstripe-tagfield
synced 2024-10-22 11:05:32 +02:00
Merge pull request #60 from helpfulrobot/convert-to-psr-2
Converted to PSR-2
This commit is contained in:
commit
10b84002b3
@ -9,314 +9,332 @@
|
||||
* @package forms
|
||||
* @subpackage fields
|
||||
*/
|
||||
class StringTagField extends DropdownField {
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public static $allowed_actions = array(
|
||||
'suggest',
|
||||
);
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $shouldLazyLoad = false;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $lazyLoadItemLimit = 10;
|
||||
|
||||
/**
|
||||
* @var null|DataObject
|
||||
*/
|
||||
protected $record;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $isMultiple = true;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $title
|
||||
* @param array|SS_List $source
|
||||
* @param array|SS_List $value
|
||||
*/
|
||||
public function __construct($name, $title = '', $source = array(), $value = array()) {
|
||||
parent::__construct($name, $title, $source, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getShouldLazyLoad() {
|
||||
return $this->shouldLazyLoad;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $shouldLazyLoad
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setShouldLazyLoad($shouldLazyLoad) {
|
||||
$this->shouldLazyLoad = $shouldLazyLoad;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getLazyLoadItemLimit() {
|
||||
return $this->lazyLoadItemLimit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $lazyLoadItemLimit
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setLazyLoadItemLimit($lazyLoadItemLimit) {
|
||||
$this->lazyLoadItemLimit = $lazyLoadItemLimit;
|
||||
|
||||
return $this;
|
||||
}
|
||||
class StringTagField extends DropdownField
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public static $allowed_actions = array(
|
||||
'suggest',
|
||||
);
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getIsMultiple() {
|
||||
return $this->isMultiple;
|
||||
}
|
||||
* @var bool
|
||||
*/
|
||||
protected $shouldLazyLoad = false;
|
||||
|
||||
/**
|
||||
* @param bool $isMultiple
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setIsMultiple($isMultiple) {
|
||||
$this->isMultiple = $isMultiple;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $lazyLoadItemLimit = 10;
|
||||
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @var null|DataObject
|
||||
*/
|
||||
protected $record;
|
||||
|
||||
/**
|
||||
* @return null|DataObject
|
||||
*/
|
||||
public function getRecord() {
|
||||
if($this->record) {
|
||||
return $this->record;
|
||||
}
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $isMultiple = true;
|
||||
|
||||
if($form = $this->getForm()) {
|
||||
return $form->getRecord();
|
||||
}
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $title
|
||||
* @param array|SS_List $source
|
||||
* @param array|SS_List $value
|
||||
*/
|
||||
public function __construct($name, $title = '', $source = array(), $value = array())
|
||||
{
|
||||
parent::__construct($name, $title, $source, $value);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getShouldLazyLoad()
|
||||
{
|
||||
return $this->shouldLazyLoad;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DataObject $record
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setRecord(DataObject $record) {
|
||||
$this->record = $record;
|
||||
/**
|
||||
* @param bool $shouldLazyLoad
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setShouldLazyLoad($shouldLazyLoad)
|
||||
{
|
||||
$this->shouldLazyLoad = $shouldLazyLoad;
|
||||
|
||||
return $this;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function Field($properties = array()) {
|
||||
Requirements::css(TAG_FIELD_DIR . '/css/select2.min.css');
|
||||
Requirements::css(TAG_FIELD_DIR . '/css/TagField.css');
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getLazyLoadItemLimit()
|
||||
{
|
||||
return $this->lazyLoadItemLimit;
|
||||
}
|
||||
|
||||
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
|
||||
Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js');
|
||||
Requirements::javascript(TAG_FIELD_DIR . '/js/select2.js');
|
||||
Requirements::javascript(TAG_FIELD_DIR . '/js/TagField.js');
|
||||
/**
|
||||
* @param int $lazyLoadItemLimit
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setLazyLoadItemLimit($lazyLoadItemLimit)
|
||||
{
|
||||
$this->lazyLoadItemLimit = $lazyLoadItemLimit;
|
||||
|
||||
$this->addExtraClass('ss-tag-field');
|
||||
return $this;
|
||||
}
|
||||
|
||||
if ($this->getIsMultiple()) {
|
||||
$this->setAttribute('multiple', 'multiple');
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getIsMultiple()
|
||||
{
|
||||
return $this->isMultiple;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $isMultiple
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setIsMultiple($isMultiple)
|
||||
{
|
||||
$this->isMultiple = $isMultiple;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|DataObject
|
||||
*/
|
||||
public function getRecord()
|
||||
{
|
||||
if ($this->record) {
|
||||
return $this->record;
|
||||
}
|
||||
|
||||
if($this->getShouldLazyLoad()) {
|
||||
$this->setAttribute('data-ss-tag-field-suggest-url', $this->getSuggestURL());
|
||||
} else {
|
||||
$properties = array_merge($properties, array(
|
||||
'Options' => $this->getOptions()
|
||||
));
|
||||
}
|
||||
if ($form = $this->getForm()) {
|
||||
return $form->getRecord();
|
||||
}
|
||||
|
||||
return $this
|
||||
->customise($properties)
|
||||
->renderWith(array("templates/TagField"));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getSuggestURL() {
|
||||
return Controller::join_links($this->Link(), 'suggest');
|
||||
}
|
||||
/**
|
||||
* @param DataObject $record
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setRecord(DataObject $record)
|
||||
{
|
||||
$this->record = $record;
|
||||
|
||||
/**
|
||||
* @return ArrayList
|
||||
*/
|
||||
protected function getOptions() {
|
||||
$options = ArrayList::create();
|
||||
return $this;
|
||||
}
|
||||
|
||||
$source = $this->getSource();
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function Field($properties = array())
|
||||
{
|
||||
Requirements::css(TAG_FIELD_DIR . '/css/select2.min.css');
|
||||
Requirements::css(TAG_FIELD_DIR . '/css/TagField.css');
|
||||
|
||||
if($source instanceof Iterator) {
|
||||
$source = iterator_to_array($source);
|
||||
}
|
||||
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
|
||||
Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js');
|
||||
Requirements::javascript(TAG_FIELD_DIR . '/js/select2.js');
|
||||
Requirements::javascript(TAG_FIELD_DIR . '/js/TagField.js');
|
||||
|
||||
$values = $this->Value();
|
||||
$this->addExtraClass('ss-tag-field');
|
||||
|
||||
foreach($source as $value) {
|
||||
$options->push(
|
||||
ArrayData::create(array(
|
||||
'Title' => $value,
|
||||
'Value' => $value,
|
||||
'Selected' => in_array($value, $values),
|
||||
))
|
||||
);
|
||||
}
|
||||
if ($this->getIsMultiple()) {
|
||||
$this->setAttribute('multiple', 'multiple');
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
if ($this->getShouldLazyLoad()) {
|
||||
$this->setAttribute('data-ss-tag-field-suggest-url', $this->getSuggestURL());
|
||||
} else {
|
||||
$properties = array_merge($properties, array(
|
||||
'Options' => $this->getOptions()
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setValue($value, $source = null) {
|
||||
if(is_string($value)) {
|
||||
$value = explode(',', $value);
|
||||
}
|
||||
return $this
|
||||
->customise($properties)
|
||||
->renderWith(array("templates/TagField"));
|
||||
}
|
||||
|
||||
if($source instanceof DataObject) {
|
||||
$name = $this->getName();
|
||||
$value = explode(',', $source->$name);
|
||||
}
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getSuggestURL()
|
||||
{
|
||||
return Controller::join_links($this->Link(), 'suggest');
|
||||
}
|
||||
|
||||
if($source instanceof SS_List) {
|
||||
$value = $source->column('ID');
|
||||
}
|
||||
/**
|
||||
* @return ArrayList
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
$options = ArrayList::create();
|
||||
|
||||
return parent::setValue(array_filter($value));
|
||||
}
|
||||
$source = $this->getSource();
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAttributes() {
|
||||
return array_merge(
|
||||
parent::getAttributes(),
|
||||
array('name' => $this->getName() . '[]')
|
||||
);
|
||||
}
|
||||
if ($source instanceof Iterator) {
|
||||
$source = iterator_to_array($source);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function saveInto(DataObjectInterface $record) {
|
||||
parent::saveInto($record);
|
||||
$values = $this->Value();
|
||||
|
||||
$name = $this->getName();
|
||||
foreach ($source as $value) {
|
||||
$options->push(
|
||||
ArrayData::create(array(
|
||||
'Title' => $value,
|
||||
'Value' => $value,
|
||||
'Selected' => in_array($value, $values),
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
$record->$name = join(',', $this->Value());
|
||||
$record->write();
|
||||
}
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a JSON string of tags, for lazy loading.
|
||||
*
|
||||
* @param SS_HTTPRequest $request
|
||||
*
|
||||
* @return SS_HTTPResponse
|
||||
*/
|
||||
public function suggest(SS_HTTPRequest $request) {
|
||||
$responseBody = Convert::raw2json(
|
||||
array('items' => array())
|
||||
);
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setValue($value, $source = null)
|
||||
{
|
||||
if (is_string($value)) {
|
||||
$value = explode(',', $value);
|
||||
}
|
||||
|
||||
$response = new SS_HTTPResponse();
|
||||
$response->addHeader('Content-Type', 'application/json');
|
||||
if ($source instanceof DataObject) {
|
||||
$name = $this->getName();
|
||||
$value = explode(',', $source->$name);
|
||||
}
|
||||
|
||||
if($record = $this->getRecord()) {
|
||||
$tags = array();
|
||||
$term = $request->getVar('term');
|
||||
if ($source instanceof SS_List) {
|
||||
$value = $source->column('ID');
|
||||
}
|
||||
|
||||
if($record->hasField($this->getName())) {
|
||||
$tags = $this->getTags($term);
|
||||
}
|
||||
return parent::setValue(array_filter($value));
|
||||
}
|
||||
|
||||
$responseBody = Convert::raw2json(
|
||||
array('items' => $tags)
|
||||
);
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAttributes()
|
||||
{
|
||||
return array_merge(
|
||||
parent::getAttributes(),
|
||||
array('name' => $this->getName() . '[]')
|
||||
);
|
||||
}
|
||||
|
||||
$response->setBody($responseBody);
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function saveInto(DataObjectInterface $record)
|
||||
{
|
||||
parent::saveInto($record);
|
||||
|
||||
return $response;
|
||||
}
|
||||
$name = $this->getName();
|
||||
|
||||
/**
|
||||
* Returns array of arrays representing tags.
|
||||
*
|
||||
* @param string $term
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getTags($term) {
|
||||
$record = $this->getRecord();
|
||||
$record->$name = join(',', $this->Value());
|
||||
$record->write();
|
||||
}
|
||||
|
||||
if(!$record) {
|
||||
return array();
|
||||
}
|
||||
/**
|
||||
* Returns a JSON string of tags, for lazy loading.
|
||||
*
|
||||
* @param SS_HTTPRequest $request
|
||||
*
|
||||
* @return SS_HTTPResponse
|
||||
*/
|
||||
public function suggest(SS_HTTPRequest $request)
|
||||
{
|
||||
$responseBody = Convert::raw2json(
|
||||
array('items' => array())
|
||||
);
|
||||
|
||||
$fieldName = $this->getName();
|
||||
$className = $record->getClassName();
|
||||
$response = new SS_HTTPResponse();
|
||||
$response->addHeader('Content-Type', 'application/json');
|
||||
|
||||
$term = Convert::raw2sql($term);
|
||||
if ($record = $this->getRecord()) {
|
||||
$tags = array();
|
||||
$term = $request->getVar('term');
|
||||
|
||||
$query = $className::get()
|
||||
->filter($fieldName . ':PartialMatch:nocase', $term)
|
||||
->limit($this->getLazyLoadItemLimit());
|
||||
if ($record->hasField($this->getName())) {
|
||||
$tags = $this->getTags($term);
|
||||
}
|
||||
|
||||
$items = array();
|
||||
$responseBody = Convert::raw2json(
|
||||
array('items' => $tags)
|
||||
);
|
||||
}
|
||||
|
||||
foreach($query->column($fieldName) as $tags) {
|
||||
$tags = explode(',', $tags);
|
||||
$response->setBody($responseBody);
|
||||
|
||||
foreach($tags as $i => $tag) {
|
||||
if(stripos($tag, $term) !== false && !in_array($tag, $items)) {
|
||||
$items[] = array(
|
||||
'id' => $tag,
|
||||
'text' => $tag
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
/**
|
||||
* Returns array of arrays representing tags.
|
||||
*
|
||||
* @param string $term
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getTags($term)
|
||||
{
|
||||
$record = $this->getRecord();
|
||||
|
||||
/**
|
||||
* DropdownField assumes value will be a scalar so we must
|
||||
* override validate. This only applies to Silverstripe 3.2+
|
||||
*
|
||||
* @param Validator $validator
|
||||
* @return bool
|
||||
*/
|
||||
public function validate($validator) {
|
||||
return true;
|
||||
}
|
||||
if (!$record) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$fieldName = $this->getName();
|
||||
$className = $record->getClassName();
|
||||
|
||||
$term = Convert::raw2sql($term);
|
||||
|
||||
$query = $className::get()
|
||||
->filter($fieldName . ':PartialMatch:nocase', $term)
|
||||
->limit($this->getLazyLoadItemLimit());
|
||||
|
||||
$items = array();
|
||||
|
||||
foreach ($query->column($fieldName) as $tags) {
|
||||
$tags = explode(',', $tags);
|
||||
|
||||
foreach ($tags as $i => $tag) {
|
||||
if (stripos($tag, $term) !== false && !in_array($tag, $items)) {
|
||||
$items[] = array(
|
||||
'id' => $tag,
|
||||
'text' => $tag
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* DropdownField assumes value will be a scalar so we must
|
||||
* override validate. This only applies to Silverstripe 3.2+
|
||||
*
|
||||
* @param Validator $validator
|
||||
* @return bool
|
||||
*/
|
||||
public function validate($validator)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -6,382 +6,403 @@
|
||||
* @package forms
|
||||
* @subpackage fields
|
||||
*/
|
||||
class TagField extends DropdownField {
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public static $allowed_actions = array(
|
||||
'suggest',
|
||||
);
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $shouldLazyLoad = false;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $lazyLoadItemLimit = 10;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $canCreate = true;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $titleField = 'Title';
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $isMultiple = true;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $title
|
||||
* @param null|DataList $source
|
||||
* @param null|DataList $value
|
||||
*/
|
||||
public function __construct($name, $title = '', $source = null, $value = null) {
|
||||
parent::__construct($name, $title, $source, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getShouldLazyLoad() {
|
||||
return $this->shouldLazyLoad;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $shouldLazyLoad
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setShouldLazyLoad($shouldLazyLoad) {
|
||||
$this->shouldLazyLoad = $shouldLazyLoad;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getLazyLoadItemLimit() {
|
||||
return $this->lazyLoadItemLimit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $lazyLoadItemLimit
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setLazyLoadItemLimit($lazyLoadItemLimit) {
|
||||
$this->lazyLoadItemLimit = $lazyLoadItemLimit;
|
||||
|
||||
return $this;
|
||||
}
|
||||
class TagField extends DropdownField
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public static $allowed_actions = array(
|
||||
'suggest',
|
||||
);
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getIsMultiple() {
|
||||
return $this->isMultiple;
|
||||
}
|
||||
* @var bool
|
||||
*/
|
||||
protected $shouldLazyLoad = false;
|
||||
|
||||
/**
|
||||
* @param bool $isMultiple
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setIsMultiple($isMultiple) {
|
||||
$this->isMultiple = $isMultiple;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $lazyLoadItemLimit = 10;
|
||||
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $canCreate = true;
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getCanCreate() {
|
||||
return $this->canCreate;
|
||||
}
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $titleField = 'Title';
|
||||
|
||||
/**
|
||||
* @param bool $canCreate
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setCanCreate($canCreate) {
|
||||
$this->canCreate = $canCreate;
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $isMultiple = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $title
|
||||
* @param null|DataList $source
|
||||
* @param null|DataList $value
|
||||
*/
|
||||
public function __construct($name, $title = '', $source = null, $value = null)
|
||||
{
|
||||
parent::__construct($name, $title, $source, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTitleField() {
|
||||
return $this->titleField;
|
||||
}
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getShouldLazyLoad()
|
||||
{
|
||||
return $this->shouldLazyLoad;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $titleField
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setTitleField($titleField) {
|
||||
$this->titleField = $titleField;
|
||||
/**
|
||||
* @param bool $shouldLazyLoad
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setShouldLazyLoad($shouldLazyLoad)
|
||||
{
|
||||
$this->shouldLazyLoad = $shouldLazyLoad;
|
||||
|
||||
return $this;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function Field($properties = array()) {
|
||||
Requirements::css(TAG_FIELD_DIR . '/css/select2.min.css');
|
||||
Requirements::css(TAG_FIELD_DIR . '/css/TagField.css');
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getLazyLoadItemLimit()
|
||||
{
|
||||
return $this->lazyLoadItemLimit;
|
||||
}
|
||||
|
||||
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
|
||||
Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js');
|
||||
Requirements::javascript(TAG_FIELD_DIR . '/js/select2.js');
|
||||
Requirements::javascript(TAG_FIELD_DIR . '/js/TagField.js');
|
||||
/**
|
||||
* @param int $lazyLoadItemLimit
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setLazyLoadItemLimit($lazyLoadItemLimit)
|
||||
{
|
||||
$this->lazyLoadItemLimit = $lazyLoadItemLimit;
|
||||
|
||||
$this->addExtraClass('ss-tag-field');
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getIsMultiple()
|
||||
{
|
||||
return $this->isMultiple;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $isMultiple
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setIsMultiple($isMultiple)
|
||||
{
|
||||
$this->isMultiple = $isMultiple;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getCanCreate()
|
||||
{
|
||||
return $this->canCreate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $canCreate
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setCanCreate($canCreate)
|
||||
{
|
||||
$this->canCreate = $canCreate;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTitleField()
|
||||
{
|
||||
return $this->titleField;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $titleField
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setTitleField($titleField)
|
||||
{
|
||||
$this->titleField = $titleField;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function Field($properties = array())
|
||||
{
|
||||
Requirements::css(TAG_FIELD_DIR . '/css/select2.min.css');
|
||||
Requirements::css(TAG_FIELD_DIR . '/css/TagField.css');
|
||||
|
||||
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
|
||||
Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js');
|
||||
Requirements::javascript(TAG_FIELD_DIR . '/js/select2.js');
|
||||
Requirements::javascript(TAG_FIELD_DIR . '/js/TagField.js');
|
||||
|
||||
$this->addExtraClass('ss-tag-field');
|
||||
|
||||
if ($this->getIsMultiple()) {
|
||||
$this->setAttribute('multiple', 'multiple');
|
||||
$this->setAttribute('multiple', 'multiple');
|
||||
}
|
||||
|
||||
if($this->shouldLazyLoad) {
|
||||
$this->setAttribute('data-ss-tag-field-suggest-url', $this->getSuggestURL());
|
||||
} else {
|
||||
$properties = array_merge($properties, array(
|
||||
'Options' => $this->getOptions()
|
||||
));
|
||||
}
|
||||
if ($this->shouldLazyLoad) {
|
||||
$this->setAttribute('data-ss-tag-field-suggest-url', $this->getSuggestURL());
|
||||
} else {
|
||||
$properties = array_merge($properties, array(
|
||||
'Options' => $this->getOptions()
|
||||
));
|
||||
}
|
||||
|
||||
return $this
|
||||
->customise($properties)
|
||||
->renderWith(array("templates/TagField"));
|
||||
}
|
||||
return $this
|
||||
->customise($properties)
|
||||
->renderWith(array("templates/TagField"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getSuggestURL() {
|
||||
return Controller::join_links($this->Link(), 'suggest');
|
||||
}
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getSuggestURL()
|
||||
{
|
||||
return Controller::join_links($this->Link(), 'suggest');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ArrayList
|
||||
*/
|
||||
protected function getOptions() {
|
||||
$options = ArrayList::create();
|
||||
/**
|
||||
* @return ArrayList
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
$options = ArrayList::create();
|
||||
|
||||
$source = $this->getSource();
|
||||
$source = $this->getSource();
|
||||
|
||||
if(!$source) {
|
||||
$source = new ArrayList();
|
||||
}
|
||||
if (!$source) {
|
||||
$source = new ArrayList();
|
||||
}
|
||||
|
||||
$dataClass = $source->dataClass();
|
||||
$dataClass = $source->dataClass();
|
||||
|
||||
$values = $this->Value();
|
||||
$values = $this->Value();
|
||||
|
||||
if(!$values) {
|
||||
return $options;
|
||||
}
|
||||
if (!$values) {
|
||||
return $options;
|
||||
}
|
||||
|
||||
if(is_array($values)) {
|
||||
$values = DataList::create($dataClass)->filter('ID', $values);
|
||||
}
|
||||
if (is_array($values)) {
|
||||
$values = DataList::create($dataClass)->filter('ID', $values);
|
||||
}
|
||||
|
||||
$ids = $values->column('ID');
|
||||
$ids = $values->column('ID');
|
||||
|
||||
$titleField = $this->getTitleField();
|
||||
$titleField = $this->getTitleField();
|
||||
|
||||
foreach($source as $object) {
|
||||
$options->push(
|
||||
ArrayData::create(array(
|
||||
'Title' => $object->$titleField,
|
||||
'Value' => $object->ID,
|
||||
'Selected' => in_array($object->ID, $ids),
|
||||
))
|
||||
);
|
||||
}
|
||||
foreach ($source as $object) {
|
||||
$options->push(
|
||||
ArrayData::create(array(
|
||||
'Title' => $object->$titleField,
|
||||
'Value' => $object->ID,
|
||||
'Selected' => in_array($object->ID, $ids),
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setValue($value, $source = null) {
|
||||
if($source instanceof DataObject) {
|
||||
$name = $this->getName();
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setValue($value, $source = null)
|
||||
{
|
||||
if ($source instanceof DataObject) {
|
||||
$name = $this->getName();
|
||||
|
||||
if($source->hasMethod($name)) {
|
||||
$value = $source->$name()->getIDList();
|
||||
}
|
||||
} elseif($value instanceof SS_List) {
|
||||
$value = $value->column('ID');
|
||||
}
|
||||
if ($source->hasMethod($name)) {
|
||||
$value = $source->$name()->getIDList();
|
||||
}
|
||||
} elseif ($value instanceof SS_List) {
|
||||
$value = $value->column('ID');
|
||||
}
|
||||
|
||||
if(!is_array($value)) {
|
||||
return parent::setValue($value);
|
||||
}
|
||||
if (!is_array($value)) {
|
||||
return parent::setValue($value);
|
||||
}
|
||||
|
||||
return parent::setValue(array_filter($value));
|
||||
}
|
||||
return parent::setValue(array_filter($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAttributes() {
|
||||
return array_merge(
|
||||
parent::getAttributes(),
|
||||
array('name' => $this->getName() . '[]')
|
||||
);
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAttributes()
|
||||
{
|
||||
return array_merge(
|
||||
parent::getAttributes(),
|
||||
array('name' => $this->getName() . '[]')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function saveInto(DataObjectInterface $record) {
|
||||
parent::saveInto($record);
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function saveInto(DataObjectInterface $record)
|
||||
{
|
||||
parent::saveInto($record);
|
||||
|
||||
$name = $this->getName();
|
||||
$titleField = $this->getTitleField();
|
||||
$name = $this->getName();
|
||||
$titleField = $this->getTitleField();
|
||||
|
||||
$source = $this->getSource();
|
||||
$source = $this->getSource();
|
||||
|
||||
$values = $this->Value();
|
||||
$values = $this->Value();
|
||||
|
||||
if(!$values) {
|
||||
$values = array();
|
||||
}
|
||||
if (!$values) {
|
||||
$values = array();
|
||||
}
|
||||
|
||||
if(empty($record) || empty($source) || empty($titleField)) {
|
||||
return;
|
||||
}
|
||||
if (empty($record) || empty($source) || empty($titleField)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(!$record->hasMethod($name)) {
|
||||
throw new Exception(
|
||||
sprintf("%s does not have a %s method", get_class($record), $name)
|
||||
);
|
||||
}
|
||||
if (!$record->hasMethod($name)) {
|
||||
throw new Exception(
|
||||
sprintf("%s does not have a %s method", get_class($record), $name)
|
||||
);
|
||||
}
|
||||
|
||||
$relation = $record->$name();
|
||||
$relation = $record->$name();
|
||||
|
||||
foreach($values as $i => $value) {
|
||||
if(!is_numeric($value)) {
|
||||
if(!$this->getCanCreate()) {
|
||||
unset($values[$i]);
|
||||
continue;
|
||||
}
|
||||
foreach ($values as $i => $value) {
|
||||
if (!is_numeric($value)) {
|
||||
if (!$this->getCanCreate()) {
|
||||
unset($values[$i]);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get or create record
|
||||
$record = $this->getOrCreateTag($value);
|
||||
$values[$i] = $record->ID;
|
||||
}
|
||||
}
|
||||
// Get or create record
|
||||
$record = $this->getOrCreateTag($value);
|
||||
$values[$i] = $record->ID;
|
||||
}
|
||||
}
|
||||
|
||||
if($values instanceof SS_List) {
|
||||
$values = iterator_to_array($values);
|
||||
}
|
||||
if ($values instanceof SS_List) {
|
||||
$values = iterator_to_array($values);
|
||||
}
|
||||
|
||||
$relation->setByIDList(array_filter($values));
|
||||
}
|
||||
$relation->setByIDList(array_filter($values));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or create tag with the given value
|
||||
*
|
||||
* @param string $term
|
||||
* @return DataObject
|
||||
*/
|
||||
protected function getOrCreateTag($term) {
|
||||
// Check if existing record can be found
|
||||
$source = $this->getSource();
|
||||
$titleField = $this->getTitleField();
|
||||
$record = $source
|
||||
->filter($titleField, $term)
|
||||
->first();
|
||||
if($record) {
|
||||
return $record;
|
||||
}
|
||||
/**
|
||||
* Get or create tag with the given value
|
||||
*
|
||||
* @param string $term
|
||||
* @return DataObject
|
||||
*/
|
||||
protected function getOrCreateTag($term)
|
||||
{
|
||||
// Check if existing record can be found
|
||||
$source = $this->getSource();
|
||||
$titleField = $this->getTitleField();
|
||||
$record = $source
|
||||
->filter($titleField, $term)
|
||||
->first();
|
||||
if ($record) {
|
||||
return $record;
|
||||
}
|
||||
|
||||
// Create new instance if not yet saved
|
||||
$dataClass = $source->dataClass();
|
||||
$record = Injector::inst()->create($dataClass);
|
||||
$record->{$titleField} = $term;
|
||||
$record->write();
|
||||
return $record;
|
||||
}
|
||||
// Create new instance if not yet saved
|
||||
$dataClass = $source->dataClass();
|
||||
$record = Injector::inst()->create($dataClass);
|
||||
$record->{$titleField} = $term;
|
||||
$record->write();
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a JSON string of tags, for lazy loading.
|
||||
*
|
||||
* @param SS_HTTPRequest $request
|
||||
*
|
||||
* @return SS_HTTPResponse
|
||||
*/
|
||||
public function suggest(SS_HTTPRequest $request) {
|
||||
$tags = $this->getTags($request->getVar('term'));
|
||||
/**
|
||||
* Returns a JSON string of tags, for lazy loading.
|
||||
*
|
||||
* @param SS_HTTPRequest $request
|
||||
*
|
||||
* @return SS_HTTPResponse
|
||||
*/
|
||||
public function suggest(SS_HTTPRequest $request)
|
||||
{
|
||||
$tags = $this->getTags($request->getVar('term'));
|
||||
|
||||
$response = new SS_HTTPResponse();
|
||||
$response->addHeader('Content-Type', 'application/json');
|
||||
$response->setBody(json_encode(array('items' => $tags)));
|
||||
$response = new SS_HTTPResponse();
|
||||
$response->addHeader('Content-Type', 'application/json');
|
||||
$response->setBody(json_encode(array('items' => $tags)));
|
||||
|
||||
return $response;
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns array of arrays representing tags.
|
||||
*
|
||||
* @param string $term
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getTags($term) {
|
||||
/**
|
||||
* @var DataList $source
|
||||
*/
|
||||
$source = $this->getSource();
|
||||
/**
|
||||
* Returns array of arrays representing tags.
|
||||
*
|
||||
* @param string $term
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getTags($term)
|
||||
{
|
||||
/**
|
||||
* @var DataList $source
|
||||
*/
|
||||
$source = $this->getSource();
|
||||
|
||||
$titleField = $this->getTitleField();
|
||||
$titleField = $this->getTitleField();
|
||||
|
||||
$query = $source
|
||||
->filter($titleField . ':PartialMatch:nocase', $term)
|
||||
->sort($titleField)
|
||||
->limit($this->getLazyLoadItemLimit());
|
||||
$query = $source
|
||||
->filter($titleField . ':PartialMatch:nocase', $term)
|
||||
->sort($titleField)
|
||||
->limit($this->getLazyLoadItemLimit());
|
||||
|
||||
// Map into a distinct list
|
||||
$items = array();
|
||||
$titleField = $this->getTitleField();
|
||||
foreach($query->map('ID', $titleField) as $id => $title) {
|
||||
$items[$title] = array(
|
||||
'id' => $id,
|
||||
'text' => $title
|
||||
);
|
||||
}
|
||||
// Map into a distinct list
|
||||
$items = array();
|
||||
$titleField = $this->getTitleField();
|
||||
foreach ($query->map('ID', $titleField) as $id => $title) {
|
||||
$items[$title] = array(
|
||||
'id' => $id,
|
||||
'text' => $title
|
||||
);
|
||||
}
|
||||
|
||||
return array_values($items);
|
||||
}
|
||||
|
||||
/**
|
||||
* DropdownField assumes value will be a scalar so we must
|
||||
* override validate. This only applies to Silverstripe 3.2+
|
||||
*
|
||||
* @param Validator $validator
|
||||
* @return bool
|
||||
*/
|
||||
public function validate($validator) {
|
||||
return true;
|
||||
}
|
||||
return array_values($items);
|
||||
}
|
||||
|
||||
/**
|
||||
* DropdownField assumes value will be a scalar so we must
|
||||
* override validate. This only applies to Silverstripe 3.2+
|
||||
*
|
||||
* @param Validator $validator
|
||||
* @return bool
|
||||
*/
|
||||
public function validate($validator)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -3,147 +3,157 @@
|
||||
/**
|
||||
* @mixin PHPUnit_Framework_TestCase
|
||||
*/
|
||||
class StringTagFieldTest extends SapphireTest {
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public static $fixture_file = 'tagfield/tests/StringTagFieldTest.yml';
|
||||
class StringTagFieldTest extends SapphireTest
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public static $fixture_file = 'tagfield/tests/StringTagFieldTest.yml';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $extraDataObjects = array(
|
||||
'StringTagFieldTestBlogPost',
|
||||
);
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $extraDataObjects = array(
|
||||
'StringTagFieldTestBlogPost',
|
||||
);
|
||||
|
||||
function testItSavesTagsOnNewRecords() {
|
||||
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
|
||||
public function testItSavesTagsOnNewRecords()
|
||||
{
|
||||
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
|
||||
|
||||
$field = new StringTagField('Tags');
|
||||
$field->setValue(array('Tag1', 'Tag2'));
|
||||
$field->saveInto($record);
|
||||
$field = new StringTagField('Tags');
|
||||
$field->setValue(array('Tag1', 'Tag2'));
|
||||
$field->saveInto($record);
|
||||
|
||||
$record->write();
|
||||
$record->write();
|
||||
|
||||
$this->assertEquals('Tag1,Tag2', $record->Tags);
|
||||
}
|
||||
$this->assertEquals('Tag1,Tag2', $record->Tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return StringTagFieldTestBlogPost
|
||||
*/
|
||||
protected function getNewStringTagFieldTestBlogPost($name) {
|
||||
return $this->objFromFixture(
|
||||
'StringTagFieldTestBlogPost',
|
||||
$name
|
||||
);
|
||||
}
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return StringTagFieldTestBlogPost
|
||||
*/
|
||||
protected function getNewStringTagFieldTestBlogPost($name)
|
||||
{
|
||||
return $this->objFromFixture(
|
||||
'StringTagFieldTestBlogPost',
|
||||
$name
|
||||
);
|
||||
}
|
||||
|
||||
function testItSavesTagsOnExistingRecords() {
|
||||
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
|
||||
$record->write();
|
||||
public function testItSavesTagsOnExistingRecords()
|
||||
{
|
||||
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
|
||||
$record->write();
|
||||
|
||||
$field = new StringTagField('Tags');
|
||||
$field->setValue(array('Tag1', 'Tag2'));
|
||||
$field->saveInto($record);
|
||||
$field = new StringTagField('Tags');
|
||||
$field->setValue(array('Tag1', 'Tag2'));
|
||||
$field->saveInto($record);
|
||||
|
||||
$this->assertEquals('Tag1,Tag2', $record->Tags);
|
||||
}
|
||||
$this->assertEquals('Tag1,Tag2', $record->Tags);
|
||||
}
|
||||
|
||||
function testItSuggestsTags() {
|
||||
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost2');
|
||||
public function testItSuggestsTags()
|
||||
{
|
||||
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost2');
|
||||
|
||||
$field = new StringTagField('Tags');
|
||||
$field->setRecord($record);
|
||||
$field = new StringTagField('Tags');
|
||||
$field->setRecord($record);
|
||||
|
||||
/**
|
||||
* Partial tag title match.
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'Tag'));
|
||||
/**
|
||||
* Partial tag title match.
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'Tag'));
|
||||
|
||||
$this->assertEquals(
|
||||
'{"items":[{"id":"Tag1","text":"Tag1"},{"id":"Tag2","text":"Tag2"}]}',
|
||||
$field->suggest($request)->getBody()
|
||||
);
|
||||
$this->assertEquals(
|
||||
'{"items":[{"id":"Tag1","text":"Tag1"},{"id":"Tag2","text":"Tag2"}]}',
|
||||
$field->suggest($request)->getBody()
|
||||
);
|
||||
|
||||
/**
|
||||
* Exact tag title match.
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'Tag1'));
|
||||
/**
|
||||
* Exact tag title match.
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'Tag1'));
|
||||
|
||||
$this->assertEquals($field->suggest($request)->getBody(), '{"items":[{"id":"Tag1","text":"Tag1"}]}');
|
||||
$this->assertEquals($field->suggest($request)->getBody(), '{"items":[{"id":"Tag1","text":"Tag1"}]}');
|
||||
|
||||
/**
|
||||
* Case-insensitive tag title match.
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'TAG1'));
|
||||
/**
|
||||
* Case-insensitive tag title match.
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'TAG1'));
|
||||
|
||||
$this->assertEquals(
|
||||
'{"items":[{"id":"Tag1","text":"Tag1"}]}',
|
||||
$field->suggest($request)->getBody()
|
||||
);
|
||||
$this->assertEquals(
|
||||
'{"items":[{"id":"Tag1","text":"Tag1"}]}',
|
||||
$field->suggest($request)->getBody()
|
||||
);
|
||||
|
||||
/**
|
||||
* No tag title match.
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'unknown'));
|
||||
/**
|
||||
* No tag title match.
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'unknown'));
|
||||
|
||||
$this->assertEquals(
|
||||
'{"items":[]}',
|
||||
$field->suggest($request)->getBody()
|
||||
);
|
||||
}
|
||||
$this->assertEquals(
|
||||
'{"items":[]}',
|
||||
$field->suggest($request)->getBody()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $parameters
|
||||
*
|
||||
* @return SS_HTTPRequest
|
||||
*/
|
||||
protected function getNewRequest(array $parameters) {
|
||||
return new SS_HTTPRequest(
|
||||
'get',
|
||||
'StringTagFieldTestController/StringTagFieldTestForm/fields/Tags/suggest',
|
||||
$parameters
|
||||
);
|
||||
}
|
||||
/**
|
||||
* @param array $parameters
|
||||
*
|
||||
* @return SS_HTTPRequest
|
||||
*/
|
||||
protected function getNewRequest(array $parameters)
|
||||
{
|
||||
return new SS_HTTPRequest(
|
||||
'get',
|
||||
'StringTagFieldTestController/StringTagFieldTestForm/fields/Tags/suggest',
|
||||
$parameters
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @property string $Tags
|
||||
*/
|
||||
class StringTagFieldTestBlogPost extends DataObject implements TestOnly {
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $db = array(
|
||||
'Title' => 'Text',
|
||||
'Content' => 'Text',
|
||||
'Tags' => 'Text',
|
||||
);
|
||||
class StringTagFieldTestBlogPost extends DataObject implements TestOnly
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $db = array(
|
||||
'Title' => 'Text',
|
||||
'Content' => 'Text',
|
||||
'Tags' => 'Text',
|
||||
);
|
||||
}
|
||||
|
||||
class StringTagFieldTestController extends Controller implements TestOnly {
|
||||
/**
|
||||
* @return Form
|
||||
*/
|
||||
public function StringTagFieldTestForm() {
|
||||
$fields = new FieldList(
|
||||
$tagField = new StringTagField('Tags')
|
||||
);
|
||||
class StringTagFieldTestController extends Controller implements TestOnly
|
||||
{
|
||||
/**
|
||||
* @return Form
|
||||
*/
|
||||
public function StringTagFieldTestForm()
|
||||
{
|
||||
$fields = new FieldList(
|
||||
$tagField = new StringTagField('Tags')
|
||||
);
|
||||
|
||||
$actions = new FieldList(
|
||||
new FormAction('StringTagFieldTestFormSubmit')
|
||||
);
|
||||
$actions = new FieldList(
|
||||
new FormAction('StringTagFieldTestFormSubmit')
|
||||
);
|
||||
|
||||
return new Form($this, 'StringTagFieldTestForm', $fields, $actions);
|
||||
}
|
||||
return new Form($this, 'StringTagFieldTestForm', $fields, $actions);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DataObject $dataObject
|
||||
* @param Form $form
|
||||
*/
|
||||
public function StringTagFieldTestFormSubmit(DataObject $dataObject, Form $form) {
|
||||
$form->saveInto($dataObject);
|
||||
}
|
||||
/**
|
||||
* @param DataObject $dataObject
|
||||
* @param Form $form
|
||||
*/
|
||||
public function StringTagFieldTestFormSubmit(DataObject $dataObject, Form $form)
|
||||
{
|
||||
$form->saveInto($dataObject);
|
||||
}
|
||||
}
|
||||
|
@ -3,353 +3,372 @@
|
||||
/**
|
||||
* @mixin PHPUnit_Framework_TestCase
|
||||
*/
|
||||
class TagFieldTest extends SapphireTest {
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public static $fixture_file = 'tagfield/tests/TagFieldTest.yml';
|
||||
class TagFieldTest extends SapphireTest
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public static $fixture_file = 'tagfield/tests/TagFieldTest.yml';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $extraDataObjects = array(
|
||||
'TagFieldTestBlogTag',
|
||||
'TagFieldTestBlogPost',
|
||||
);
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $extraDataObjects = array(
|
||||
'TagFieldTestBlogTag',
|
||||
'TagFieldTestBlogPost',
|
||||
);
|
||||
|
||||
function testItSavesLinksToNewTagsOnNewRecords() {
|
||||
$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
|
||||
public function testItSavesLinksToNewTagsOnNewRecords()
|
||||
{
|
||||
$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
|
||||
|
||||
$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
|
||||
$field->setValue(array('Tag3', 'Tag4'));
|
||||
$field->saveInto($record);
|
||||
$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
|
||||
$field->setValue(array('Tag3', 'Tag4'));
|
||||
$field->saveInto($record);
|
||||
|
||||
$record->write();
|
||||
$record->write();
|
||||
|
||||
$this->compareExpectedAndActualTags(
|
||||
array('Tag3', 'Tag4'),
|
||||
$record
|
||||
);
|
||||
}
|
||||
$this->compareExpectedAndActualTags(
|
||||
array('Tag3', 'Tag4'),
|
||||
$record
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return TagFieldTestBlogPost
|
||||
*/
|
||||
protected function getNewTagFieldTestBlogPost($name) {
|
||||
return $this->objFromFixture(
|
||||
'TagFieldTestBlogPost',
|
||||
$name
|
||||
);
|
||||
}
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return TagFieldTestBlogPost
|
||||
*/
|
||||
protected function getNewTagFieldTestBlogPost($name)
|
||||
{
|
||||
return $this->objFromFixture(
|
||||
'TagFieldTestBlogPost',
|
||||
$name
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $expected
|
||||
* @param TagFieldTestBlogPost $record
|
||||
*/
|
||||
protected function compareExpectedAndActualTags(array $expected, TagFieldTestBlogPost $record) {
|
||||
$this->compareTagLists($expected, $record->Tags());
|
||||
}
|
||||
/**
|
||||
* @param array $expected
|
||||
* @param TagFieldTestBlogPost $record
|
||||
*/
|
||||
protected function compareExpectedAndActualTags(array $expected, TagFieldTestBlogPost $record)
|
||||
{
|
||||
$this->compareTagLists($expected, $record->Tags());
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure a source of tags matches the given string tag names
|
||||
*
|
||||
* @param array $expected
|
||||
* @param DataList $actualSource
|
||||
*/
|
||||
protected function compareTagLists(array $expected, DataList $actualSource) {
|
||||
$actual = array_values($actualSource->map('ID', 'Title')->toArray());
|
||||
/**
|
||||
* Ensure a source of tags matches the given string tag names
|
||||
*
|
||||
* @param array $expected
|
||||
* @param DataList $actualSource
|
||||
*/
|
||||
protected function compareTagLists(array $expected, DataList $actualSource)
|
||||
{
|
||||
$actual = array_values($actualSource->map('ID', 'Title')->toArray());
|
||||
|
||||
sort($expected);
|
||||
sort($actual);
|
||||
sort($expected);
|
||||
sort($actual);
|
||||
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$actual
|
||||
);
|
||||
}
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$actual
|
||||
);
|
||||
}
|
||||
|
||||
public function testItSavesLinksToNewTagsOnExistingRecords() {
|
||||
$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
|
||||
$record->write();
|
||||
public function testItSavesLinksToNewTagsOnExistingRecords()
|
||||
{
|
||||
$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
|
||||
$record->write();
|
||||
|
||||
$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
|
||||
$field->setValue(array('Tag3', 'Tag4'));
|
||||
$field->saveInto($record);
|
||||
$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
|
||||
$field->setValue(array('Tag3', 'Tag4'));
|
||||
$field->saveInto($record);
|
||||
|
||||
$this->compareExpectedAndActualTags(
|
||||
array('Tag3', 'Tag4'),
|
||||
$record
|
||||
);
|
||||
}
|
||||
$this->compareExpectedAndActualTags(
|
||||
array('Tag3', 'Tag4'),
|
||||
$record
|
||||
);
|
||||
}
|
||||
|
||||
public function testItSavesLinksToExistingTagsOnNewRecords() {
|
||||
$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
|
||||
public function testItSavesLinksToExistingTagsOnNewRecords()
|
||||
{
|
||||
$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
|
||||
|
||||
$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
|
||||
$field->setValue(array('Tag1', 'Tag2'));
|
||||
$field->saveInto($record);
|
||||
$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
|
||||
$field->setValue(array('Tag1', 'Tag2'));
|
||||
$field->saveInto($record);
|
||||
|
||||
$record->write();
|
||||
$record->write();
|
||||
|
||||
$this->compareExpectedAndActualTags(
|
||||
array('Tag1', 'Tag2'),
|
||||
$record
|
||||
);
|
||||
}
|
||||
$this->compareExpectedAndActualTags(
|
||||
array('Tag1', 'Tag2'),
|
||||
$record
|
||||
);
|
||||
}
|
||||
|
||||
public function testItSavesLinksToExistingTagsOnExistingRecords() {
|
||||
$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
|
||||
$record->write();
|
||||
public function testItSavesLinksToExistingTagsOnExistingRecords()
|
||||
{
|
||||
$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
|
||||
$record->write();
|
||||
|
||||
$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
|
||||
$field->setValue(array('Tag1', 'Tag2'));
|
||||
$field->saveInto($record);
|
||||
$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
|
||||
$field->setValue(array('Tag1', 'Tag2'));
|
||||
$field->saveInto($record);
|
||||
|
||||
$this->compareExpectedAndActualTags(
|
||||
array('Tag1', 'Tag2'),
|
||||
$record
|
||||
);
|
||||
}
|
||||
$this->compareExpectedAndActualTags(
|
||||
array('Tag1', 'Tag2'),
|
||||
$record
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that {@see TagField::saveInto} respects existing tags
|
||||
*/
|
||||
public function testSaveDuplicateTags() {
|
||||
$record = $this->getNewTagFieldTestBlogPost('BlogPost2');
|
||||
$record->write();
|
||||
$tag2ID = $this->idFromFixture('TagFieldTestBlogTag', 'Tag2');
|
||||
/**
|
||||
* Ensure that {@see TagField::saveInto} respects existing tags
|
||||
*/
|
||||
public function testSaveDuplicateTags()
|
||||
{
|
||||
$record = $this->getNewTagFieldTestBlogPost('BlogPost2');
|
||||
$record->write();
|
||||
$tag2ID = $this->idFromFixture('TagFieldTestBlogTag', 'Tag2');
|
||||
|
||||
// Check tags before write
|
||||
$this->compareExpectedAndActualTags(
|
||||
array('Tag1', 'Tag2'),
|
||||
$record
|
||||
);
|
||||
$this->compareTagLists(
|
||||
array('Tag1', 'Tag2'),
|
||||
TagFieldTestBlogTag::get()
|
||||
);
|
||||
$this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID'));
|
||||
// Check tags before write
|
||||
$this->compareExpectedAndActualTags(
|
||||
array('Tag1', 'Tag2'),
|
||||
$record
|
||||
);
|
||||
$this->compareTagLists(
|
||||
array('Tag1', 'Tag2'),
|
||||
TagFieldTestBlogTag::get()
|
||||
);
|
||||
$this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID'));
|
||||
|
||||
// Write new tags
|
||||
$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
|
||||
$field->setValue(array('Tag2', 'Tag3'));
|
||||
$field->saveInto($record);
|
||||
// Write new tags
|
||||
$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
|
||||
$field->setValue(array('Tag2', 'Tag3'));
|
||||
$field->saveInto($record);
|
||||
|
||||
// Check only one new tag was added
|
||||
$this->compareExpectedAndActualTags(
|
||||
array('Tag2', 'Tag3'),
|
||||
$record
|
||||
);
|
||||
// Check only one new tag was added
|
||||
$this->compareExpectedAndActualTags(
|
||||
array('Tag2', 'Tag3'),
|
||||
$record
|
||||
);
|
||||
|
||||
// Ensure that only one new dataobject was added and that tag2s id has not changed
|
||||
$this->compareTagLists(
|
||||
array('Tag1', 'Tag2', 'Tag3'),
|
||||
TagFieldTestBlogTag::get()
|
||||
);
|
||||
$this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID'));
|
||||
}
|
||||
// Ensure that only one new dataobject was added and that tag2s id has not changed
|
||||
$this->compareTagLists(
|
||||
array('Tag1', 'Tag2', 'Tag3'),
|
||||
TagFieldTestBlogTag::get()
|
||||
);
|
||||
$this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID'));
|
||||
}
|
||||
|
||||
function testItSuggestsTags() {
|
||||
$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
|
||||
public function testItSuggestsTags()
|
||||
{
|
||||
$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
|
||||
|
||||
/**
|
||||
* Partial tag title match.
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'Tag'));
|
||||
/**
|
||||
* Partial tag title match.
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'Tag'));
|
||||
|
||||
$this->assertEquals(
|
||||
'{"items":[{"id":1,"text":"Tag1"},{"id":2,"text":"Tag2"}]}',
|
||||
$field->suggest($request)->getBody()
|
||||
);
|
||||
$this->assertEquals(
|
||||
'{"items":[{"id":1,"text":"Tag1"},{"id":2,"text":"Tag2"}]}',
|
||||
$field->suggest($request)->getBody()
|
||||
);
|
||||
|
||||
/**
|
||||
* Exact tag title match.
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'Tag1'));
|
||||
/**
|
||||
* Exact tag title match.
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'Tag1'));
|
||||
|
||||
$this->assertEquals(
|
||||
'{"items":[{"id":1,"text":"Tag1"}]}',
|
||||
$field->suggest($request)->getBody()
|
||||
);
|
||||
$this->assertEquals(
|
||||
'{"items":[{"id":1,"text":"Tag1"}]}',
|
||||
$field->suggest($request)->getBody()
|
||||
);
|
||||
|
||||
/**
|
||||
* Case-insensitive tag title match.
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'TAG1'));
|
||||
/**
|
||||
* Case-insensitive tag title match.
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'TAG1'));
|
||||
|
||||
$this->assertEquals(
|
||||
'{"items":[{"id":1,"text":"Tag1"}]}',
|
||||
$field->suggest($request)->getBody()
|
||||
);
|
||||
$this->assertEquals(
|
||||
'{"items":[{"id":1,"text":"Tag1"}]}',
|
||||
$field->suggest($request)->getBody()
|
||||
);
|
||||
|
||||
/**
|
||||
* No tag title match.
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'unknown'));
|
||||
/**
|
||||
* No tag title match.
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'unknown'));
|
||||
|
||||
$this->assertEquals(
|
||||
'{"items":[]}',
|
||||
$field->suggest($request)->getBody()
|
||||
);
|
||||
}
|
||||
$this->assertEquals(
|
||||
'{"items":[]}',
|
||||
$field->suggest($request)->getBody()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that TagField supports pre-filtered data sources
|
||||
*/
|
||||
public function testRestrictedSuggestions() {
|
||||
$source = TagFieldTestBlogTag::get()->exclude('Title', 'Tag2');
|
||||
$field = new TagField('Tags', '', $source);
|
||||
/**
|
||||
* Tests that TagField supports pre-filtered data sources
|
||||
*/
|
||||
public function testRestrictedSuggestions()
|
||||
{
|
||||
$source = TagFieldTestBlogTag::get()->exclude('Title', 'Tag2');
|
||||
$field = new TagField('Tags', '', $source);
|
||||
|
||||
/**
|
||||
* Partial tag title match.
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'Tag'));
|
||||
/**
|
||||
* Partial tag title match.
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'Tag'));
|
||||
|
||||
$this->assertEquals(
|
||||
'{"items":[{"id":1,"text":"Tag1"}]}',
|
||||
$field->suggest($request)->getBody()
|
||||
);
|
||||
$this->assertEquals(
|
||||
'{"items":[{"id":1,"text":"Tag1"}]}',
|
||||
$field->suggest($request)->getBody()
|
||||
);
|
||||
|
||||
/**
|
||||
* Exact tag title match.
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'Tag1'));
|
||||
/**
|
||||
* Exact tag title match.
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'Tag1'));
|
||||
|
||||
$this->assertEquals(
|
||||
'{"items":[{"id":1,"text":"Tag1"}]}',
|
||||
$field->suggest($request)->getBody()
|
||||
);
|
||||
$this->assertEquals(
|
||||
'{"items":[{"id":1,"text":"Tag1"}]}',
|
||||
$field->suggest($request)->getBody()
|
||||
);
|
||||
|
||||
/**
|
||||
* Excluded item doesn't appear in matches
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'Tag2'));
|
||||
/**
|
||||
* Excluded item doesn't appear in matches
|
||||
*/
|
||||
$request = $this->getNewRequest(array('term' => 'Tag2'));
|
||||
|
||||
$this->assertEquals(
|
||||
'{"items":[]}',
|
||||
$field->suggest($request)->getBody()
|
||||
);
|
||||
}
|
||||
$this->assertEquals(
|
||||
'{"items":[]}',
|
||||
$field->suggest($request)->getBody()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $parameters
|
||||
*
|
||||
* @return SS_HTTPRequest
|
||||
*/
|
||||
protected function getNewRequest(array $parameters) {
|
||||
return new SS_HTTPRequest(
|
||||
'get',
|
||||
'TagFieldTestController/TagFieldTestForm/fields/Tags/suggest',
|
||||
$parameters
|
||||
);
|
||||
}
|
||||
/**
|
||||
* @param array $parameters
|
||||
*
|
||||
* @return SS_HTTPRequest
|
||||
*/
|
||||
protected function getNewRequest(array $parameters)
|
||||
{
|
||||
return new SS_HTTPRequest(
|
||||
'get',
|
||||
'TagFieldTestController/TagFieldTestForm/fields/Tags/suggest',
|
||||
$parameters
|
||||
);
|
||||
}
|
||||
|
||||
function testItDisplaysValuesFromRelations() {
|
||||
$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
|
||||
$record->write();
|
||||
public function testItDisplaysValuesFromRelations()
|
||||
{
|
||||
$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
|
||||
$record->write();
|
||||
|
||||
$form = new Form(
|
||||
new TagFieldTestController($record),
|
||||
'Form',
|
||||
new FieldList(
|
||||
$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'))
|
||||
),
|
||||
new FieldList()
|
||||
);
|
||||
$form = new Form(
|
||||
new TagFieldTestController($record),
|
||||
'Form',
|
||||
new FieldList(
|
||||
$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'))
|
||||
),
|
||||
new FieldList()
|
||||
);
|
||||
|
||||
$form->loadDataFrom(
|
||||
$this->objFromFixture('TagFieldTestBlogPost', 'BlogPost2')
|
||||
);
|
||||
$form->loadDataFrom(
|
||||
$this->objFromFixture('TagFieldTestBlogPost', 'BlogPost2')
|
||||
);
|
||||
|
||||
$this->assertEquals($field->Value(), array(1 => 1, 2 => 2));
|
||||
}
|
||||
$this->assertEquals($field->Value(), array(1 => 1, 2 => 2));
|
||||
}
|
||||
|
||||
function testItIgnoresNewTagsIfCannotCreate() {
|
||||
$record = new TagFieldTestBlogPost();
|
||||
$record->write();
|
||||
public function testItIgnoresNewTagsIfCannotCreate()
|
||||
{
|
||||
$record = new TagFieldTestBlogPost();
|
||||
$record->write();
|
||||
|
||||
$tag = TagFieldTestBlogTag::get()->filter('Title', 'Tag1')->first();
|
||||
$tag = TagFieldTestBlogTag::get()->filter('Title', 'Tag1')->first();
|
||||
|
||||
$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'), array($tag->ID, 'Tag3'));
|
||||
$field->setCanCreate(false);
|
||||
$field->saveInto($record);
|
||||
$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'), array($tag->ID, 'Tag3'));
|
||||
$field->setCanCreate(false);
|
||||
$field->saveInto($record);
|
||||
|
||||
/**
|
||||
* @var TagFieldTestBlogPost $record
|
||||
*/
|
||||
$record = DataObject::get_by_id('TagFieldTestBlogPost', $record->ID);
|
||||
/**
|
||||
* @var TagFieldTestBlogPost $record
|
||||
*/
|
||||
$record = DataObject::get_by_id('TagFieldTestBlogPost', $record->ID);
|
||||
|
||||
$this->compareExpectedAndActualTags(
|
||||
array('Tag1'),
|
||||
$record
|
||||
);
|
||||
}
|
||||
$this->compareExpectedAndActualTags(
|
||||
array('Tag1'),
|
||||
$record
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class TagFieldTestBlogTag extends DataObject implements TestOnly {
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private static $default_sort = '"TagFieldTestBlogTag"."ID" ASC';
|
||||
class TagFieldTestBlogTag extends DataObject implements TestOnly
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private static $default_sort = '"TagFieldTestBlogTag"."ID" ASC';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $db = array(
|
||||
'Title' => 'Varchar(200)',
|
||||
);
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $db = array(
|
||||
'Title' => 'Varchar(200)',
|
||||
);
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $belongs_many_many = array(
|
||||
'BlogPosts' => 'TagFieldTestBlogPost',
|
||||
);
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $belongs_many_many = array(
|
||||
'BlogPosts' => 'TagFieldTestBlogPost',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @method ManyManyList Tags()
|
||||
*/
|
||||
class TagFieldTestBlogPost extends DataObject implements TestOnly {
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $db = array(
|
||||
'Title' => 'Text',
|
||||
'Content' => 'Text',
|
||||
);
|
||||
class TagFieldTestBlogPost extends DataObject implements TestOnly
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $db = array(
|
||||
'Title' => 'Text',
|
||||
'Content' => 'Text',
|
||||
);
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $many_many = array(
|
||||
'Tags' => 'TagFieldTestBlogTag',
|
||||
);
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $many_many = array(
|
||||
'Tags' => 'TagFieldTestBlogTag',
|
||||
);
|
||||
}
|
||||
|
||||
class TagFieldTestController extends Controller implements TestOnly {
|
||||
/**
|
||||
* @return Form
|
||||
*/
|
||||
public function TagFieldTestForm() {
|
||||
$fields = new FieldList(
|
||||
$tagField = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'))
|
||||
);
|
||||
class TagFieldTestController extends Controller implements TestOnly
|
||||
{
|
||||
/**
|
||||
* @return Form
|
||||
*/
|
||||
public function TagFieldTestForm()
|
||||
{
|
||||
$fields = new FieldList(
|
||||
$tagField = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'))
|
||||
);
|
||||
|
||||
$actions = new FieldList(
|
||||
new FormAction('TagFieldTestFormSubmit')
|
||||
);
|
||||
$actions = new FieldList(
|
||||
new FormAction('TagFieldTestFormSubmit')
|
||||
);
|
||||
|
||||
return new Form($this, 'TagFieldTestForm', $fields, $actions);
|
||||
}
|
||||
return new Form($this, 'TagFieldTestForm', $fields, $actions);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DataObject $dataObject
|
||||
* @param Form $form
|
||||
*/
|
||||
public function TagFieldTestFormSubmit(DataObject $dataObject, Form $form) {
|
||||
$form->saveInto($dataObject);
|
||||
}
|
||||
/**
|
||||
* @param DataObject $dataObject
|
||||
* @param Form $form
|
||||
*/
|
||||
public function TagFieldTestFormSubmit(DataObject $dataObject, Form $form)
|
||||
{
|
||||
$form->saveInto($dataObject);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user