2015-04-14 03:15:28 +02:00
|
|
|
<?php
|
|
|
|
|
2017-01-13 20:11:59 +01:00
|
|
|
namespace SilverStripe\TagField;
|
|
|
|
|
2018-09-17 08:18:40 +02:00
|
|
|
use Exception;
|
2017-01-13 20:11:59 +01:00
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
use SilverStripe\Control\HTTPResponse;
|
2019-07-18 23:41:54 +02:00
|
|
|
use SilverStripe\Core\Convert;
|
2017-01-13 20:11:59 +01:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2018-10-10 04:33:28 +02:00
|
|
|
use SilverStripe\Forms\MultiSelectField;
|
2018-11-15 20:33:38 +01:00
|
|
|
use SilverStripe\Forms\Validator;
|
2017-01-13 20:11:59 +01:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
|
|
|
use SilverStripe\ORM\DataList;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\DataObjectInterface;
|
2018-09-17 08:18:40 +02:00
|
|
|
use SilverStripe\ORM\Relation;
|
2017-01-13 20:11:59 +01:00
|
|
|
use SilverStripe\ORM\SS_List;
|
|
|
|
use SilverStripe\View\ArrayData;
|
|
|
|
|
2015-04-14 03:15:28 +02:00
|
|
|
/**
|
2015-05-15 12:29:17 +02:00
|
|
|
* Provides a tagging interface, storing links between tag DataObjects and a parent DataObject.
|
2015-04-14 03:15:28 +02:00
|
|
|
*
|
2015-05-15 12:29:17 +02:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields
|
2015-04-14 03:15:28 +02:00
|
|
|
*/
|
2018-10-10 04:33:28 +02:00
|
|
|
class TagField extends MultiSelectField
|
2015-11-18 05:05:38 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-11-15 20:33:38 +01:00
|
|
|
private static $allowed_actions = [
|
|
|
|
'suggest',
|
|
|
|
];
|
2015-11-18 05:05:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $shouldLazyLoad = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $lazyLoadItemLimit = 10;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $canCreate = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $titleField = 'Title';
|
|
|
|
|
2017-01-13 20:11:59 +01:00
|
|
|
/**
|
|
|
|
* @var DataList
|
|
|
|
*/
|
|
|
|
protected $sourceList;
|
|
|
|
|
2015-11-18 05:05:38 +01:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $isMultiple = true;
|
|
|
|
|
2018-10-10 04:33:28 +02:00
|
|
|
/** @skipUpgrade */
|
|
|
|
protected $schemaComponent = 'TagField';
|
|
|
|
|
2015-11-18 05:05:38 +01:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param string $title
|
2018-09-17 08:24:03 +02:00
|
|
|
* @param null|DataList|array $source
|
2015-11-18 05:05:38 +01:00
|
|
|
* @param null|DataList $value
|
2018-04-06 04:18:14 +02:00
|
|
|
* @param string $titleField
|
2015-11-18 05:05:38 +01:00
|
|
|
*/
|
2018-04-06 03:47:49 +02:00
|
|
|
public function __construct($name, $title = '', $source = [], $value = null, $titleField = 'Title')
|
2015-11-18 05:05:38 +01:00
|
|
|
{
|
2018-04-06 03:47:49 +02:00
|
|
|
$this->setTitleField($titleField);
|
2015-11-18 05:05:38 +01:00
|
|
|
parent::__construct($name, $title, $source, $value);
|
2019-07-18 23:41:54 +02:00
|
|
|
|
|
|
|
$this->addExtraClass('ss-tag-field');
|
2015-11-18 05:05:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getIsMultiple()
|
|
|
|
{
|
|
|
|
return $this->isMultiple;
|
|
|
|
}
|
2015-04-23 21:19:10 +02:00
|
|
|
|
2015-10-09 00:10:33 +02:00
|
|
|
/**
|
2015-11-18 05:05:38 +01:00
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:11:59 +01:00
|
|
|
/**
|
2018-09-17 08:18:40 +02:00
|
|
|
* Get the DataList source. The 4.x upgrade for SelectField::setSource starts to convert this to an array.
|
|
|
|
* If empty use getSource() for array version
|
|
|
|
*
|
2017-01-13 20:11:59 +01:00
|
|
|
* @return DataList
|
|
|
|
*/
|
|
|
|
public function getSourceList()
|
|
|
|
{
|
|
|
|
return $this->sourceList;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the model class name for tags
|
2018-09-17 08:18:40 +02:00
|
|
|
*
|
|
|
|
* @param DataList $sourceList
|
2017-01-13 20:11:59 +01:00
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setSourceList($sourceList)
|
|
|
|
{
|
|
|
|
$this->sourceList = $sourceList;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-11-18 05:05:38 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-11-15 20:33:38 +01:00
|
|
|
public function Field($properties = [])
|
2015-11-18 05:05:38 +01:00
|
|
|
{
|
2019-07-18 23:41:54 +02:00
|
|
|
$this->addExtraClass('entwine');
|
2018-11-15 20:48:46 +01:00
|
|
|
|
|
|
|
return $this->customise($properties)->renderWith(self::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide TagField data to the JSON schema for the frontend component
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getSchemaDataDefaults()
|
|
|
|
{
|
2019-07-18 23:41:54 +02:00
|
|
|
$options = $this->getOptions(true);
|
2018-11-15 20:48:46 +01:00
|
|
|
$schema = array_merge(
|
|
|
|
parent::getSchemaDataDefaults(),
|
|
|
|
[
|
|
|
|
'name' => $this->getName() . '[]',
|
|
|
|
'lazyLoad' => $this->getShouldLazyLoad(),
|
|
|
|
'creatable' => $this->getCanCreate(),
|
|
|
|
'multi' => $this->getIsMultiple(),
|
2019-07-18 23:41:54 +02:00
|
|
|
'value' => $options->count() ? $options->toNestedArray() : null,
|
2018-11-15 20:48:46 +01:00
|
|
|
'disabled' => $this->isDisabled() || $this->isReadonly(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2018-07-11 07:47:38 +02:00
|
|
|
if (!$this->getShouldLazyLoad()) {
|
|
|
|
$schema['options'] = array_values($this->getOptions()->toNestedArray());
|
2018-07-16 04:34:08 +02:00
|
|
|
} else {
|
2018-07-16 03:27:11 +02:00
|
|
|
$schema['optionUrl'] = $this->getSuggestURL();
|
2015-11-18 05:05:38 +01:00
|
|
|
}
|
2018-11-15 20:33:38 +01:00
|
|
|
|
2018-11-15 20:48:46 +01:00
|
|
|
return $schema;
|
|
|
|
}
|
2016-07-21 22:52:17 +02:00
|
|
|
|
2015-11-18 05:05:38 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getSuggestURL()
|
|
|
|
{
|
|
|
|
return Controller::join_links($this->Link(), 'suggest');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
2019-07-18 23:41:54 +02:00
|
|
|
protected function getOptions($onlySelected = false)
|
2015-11-18 05:05:38 +01:00
|
|
|
{
|
2018-10-10 04:33:28 +02:00
|
|
|
$options = ArrayList::create();
|
2017-01-13 20:11:59 +01:00
|
|
|
$source = $this->getSourceList();
|
2015-11-18 05:05:38 +01:00
|
|
|
|
2019-07-18 23:41:54 +02:00
|
|
|
// No source means we have no options
|
2017-10-18 02:28:49 +02:00
|
|
|
if (!$source) {
|
2019-07-18 23:41:54 +02:00
|
|
|
return ArrayList::create();
|
2015-11-18 05:05:38 +01:00
|
|
|
}
|
2015-08-17 17:53:38 +02:00
|
|
|
|
2015-11-18 05:05:38 +01:00
|
|
|
$dataClass = $source->dataClass();
|
2018-10-10 04:33:28 +02:00
|
|
|
|
2015-11-18 05:05:38 +01:00
|
|
|
$values = $this->Value();
|
|
|
|
|
2019-07-18 23:41:54 +02:00
|
|
|
// If we have no values and we only want selected options we can bail here
|
|
|
|
if (empty($values) && $onlySelected) {
|
|
|
|
return ArrayList::create();
|
2015-11-18 05:05:38 +01:00
|
|
|
}
|
2018-10-10 04:33:28 +02:00
|
|
|
|
2019-07-18 23:41:54 +02:00
|
|
|
// Convert an array of values into a datalist of options
|
|
|
|
if (is_array($values) && !empty($values)) {
|
2018-10-10 04:33:28 +02:00
|
|
|
$values = DataList::create($dataClass)
|
|
|
|
->filter($this->getTitleField(), $values);
|
2019-07-18 23:41:54 +02:00
|
|
|
} else {
|
|
|
|
$values = ArrayList::create();
|
2015-11-18 05:05:38 +01:00
|
|
|
}
|
|
|
|
|
2019-07-18 23:41:54 +02:00
|
|
|
// Prep a function to parse a dataobject into an option
|
|
|
|
$addOption = function (DataObject $item) use ($options, $values) {
|
|
|
|
$titleField = $this->getTitleField();
|
2019-07-24 05:06:54 +02:00
|
|
|
$option = $item->$titleField;
|
2019-07-18 23:41:54 +02:00
|
|
|
$options->push(ArrayData::create([
|
2019-07-24 05:06:54 +02:00
|
|
|
'Title' => $option,
|
2019-07-24 05:55:08 +02:00
|
|
|
'Value' => $option,
|
2019-07-18 23:41:54 +02:00
|
|
|
'Selected' => (bool) $values->find('ID', $item->ID)
|
|
|
|
]));
|
|
|
|
};
|
|
|
|
|
|
|
|
// Only parse the values if we only want the selected items in the values list (this is for lazy-loading)
|
|
|
|
if ($onlySelected) {
|
|
|
|
$values->each($addOption);
|
|
|
|
return $options;
|
2015-11-18 05:05:38 +01:00
|
|
|
}
|
|
|
|
|
2019-07-18 23:41:54 +02:00
|
|
|
$source->each($addOption);
|
2015-11-18 05:05:38 +01:00
|
|
|
return $options;
|
2017-10-18 02:28:49 +02:00
|
|
|
}
|
2015-11-18 05:05:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function setValue($value, $source = null)
|
|
|
|
{
|
|
|
|
if ($source instanceof DataObject) {
|
|
|
|
$name = $this->getName();
|
|
|
|
|
|
|
|
if ($source->hasMethod($name)) {
|
2019-07-18 23:41:54 +02:00
|
|
|
$value = $source->$name()->column($this->getTitleField());
|
2015-11-18 05:05:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_array($value)) {
|
|
|
|
return parent::setValue($value);
|
|
|
|
}
|
|
|
|
|
2019-07-18 23:41:54 +02:00
|
|
|
return parent::setValue(array_filter($value));
|
2015-11-18 05:05:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-09-13 01:18:55 +02:00
|
|
|
* Gets the source array if required
|
|
|
|
*
|
|
|
|
* Note: this is expensive for a SS_List
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getSource()
|
|
|
|
{
|
|
|
|
if (is_null($this->source)) {
|
2018-09-17 08:18:40 +02:00
|
|
|
$this->source = $this->getListMap($this->getSourceList());
|
2018-09-13 01:18:55 +02:00
|
|
|
}
|
|
|
|
return $this->source;
|
|
|
|
}
|
|
|
|
|
2018-09-17 08:18:40 +02:00
|
|
|
/**
|
|
|
|
* Intercept DataList source
|
|
|
|
*
|
|
|
|
* @param mixed $source
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setSource($source)
|
|
|
|
{
|
|
|
|
// When setting a datalist force internal list to null
|
|
|
|
if ($source instanceof DataList) {
|
|
|
|
$this->source = null;
|
|
|
|
$this->setSourceList($source);
|
|
|
|
} else {
|
|
|
|
parent::setSource($source);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-11-18 05:05:38 +01:00
|
|
|
/**
|
2018-09-17 08:18:40 +02:00
|
|
|
* @param DataObject|DataObjectInterface $record DataObject to save data into
|
|
|
|
* @throws Exception
|
2015-11-18 05:05:38 +01:00
|
|
|
*/
|
2018-10-10 04:33:28 +02:00
|
|
|
public function getAttributes()
|
2015-11-18 05:05:38 +01:00
|
|
|
{
|
2018-10-10 04:33:28 +02:00
|
|
|
return array_merge(
|
|
|
|
parent::getAttributes(),
|
|
|
|
[
|
|
|
|
'name' => $this->getName() . '[]',
|
|
|
|
'style' => 'width: 100%',
|
|
|
|
'data-schema' => json_encode($this->getSchemaData()),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2015-11-18 05:05:38 +01:00
|
|
|
|
2018-10-10 04:33:28 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function saveInto(DataObjectInterface $record)
|
|
|
|
{
|
2015-11-18 05:05:38 +01:00
|
|
|
$name = $this->getName();
|
|
|
|
$titleField = $this->getTitleField();
|
|
|
|
$values = $this->Value();
|
2018-09-17 08:18:40 +02:00
|
|
|
|
2018-11-15 20:33:38 +01:00
|
|
|
$ids = [];
|
2016-02-04 00:08:34 +01:00
|
|
|
|
2017-01-13 20:11:59 +01:00
|
|
|
if (!$values) {
|
2018-11-15 20:33:38 +01:00
|
|
|
$values = [];
|
2015-11-18 05:05:38 +01:00
|
|
|
}
|
2018-10-10 04:33:28 +02:00
|
|
|
|
2017-01-26 06:10:14 +01:00
|
|
|
if (empty($record) || empty($titleField)) {
|
2015-11-18 05:05:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:11:59 +01:00
|
|
|
if (!$record->hasMethod($name)) {
|
2015-11-18 05:05:38 +01:00
|
|
|
throw new Exception(
|
|
|
|
sprintf("%s does not have a %s method", get_class($record), $name)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-06 11:53:20 +02:00
|
|
|
/** @var Relation $relation */
|
|
|
|
$relation = $record->$name();
|
|
|
|
|
2016-02-04 00:08:34 +01:00
|
|
|
foreach ($values as $key => $value) {
|
2016-01-27 03:49:36 +01:00
|
|
|
// Get or create record
|
|
|
|
$record = $this->getOrCreateTag($value);
|
2017-01-13 20:11:59 +01:00
|
|
|
if ($record) {
|
2016-02-04 00:08:34 +01:00
|
|
|
$ids[] = $record->ID;
|
|
|
|
$values[$key] = $record->Title;
|
2015-11-18 05:05:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 00:08:34 +01:00
|
|
|
$relation->setByIDList(array_filter($ids));
|
2017-10-18 02:28:49 +02:00
|
|
|
}
|
2015-11-18 05:05:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get or create tag with the given value
|
|
|
|
*
|
2017-01-13 20:11:59 +01:00
|
|
|
* @param string $term
|
2018-11-15 20:33:38 +01:00
|
|
|
* @return DataObject|bool
|
2015-11-18 05:05:38 +01:00
|
|
|
*/
|
|
|
|
protected function getOrCreateTag($term)
|
|
|
|
{
|
|
|
|
// Check if existing record can be found
|
2017-01-13 20:11:59 +01:00
|
|
|
$source = $this->getSourceList();
|
2018-09-17 08:18:40 +02:00
|
|
|
if (!$source) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-18 05:05:38 +01:00
|
|
|
$titleField = $this->getTitleField();
|
|
|
|
$record = $source
|
|
|
|
->filter($titleField, $term)
|
|
|
|
->first();
|
2017-01-13 20:11:59 +01:00
|
|
|
if ($record) {
|
2015-11-18 05:05:38 +01:00
|
|
|
return $record;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new instance if not yet saved
|
2016-02-04 04:07:15 +01:00
|
|
|
if ($this->getCanCreate()) {
|
|
|
|
$dataClass = $source->dataClass();
|
|
|
|
$record = Injector::inst()->create($dataClass);
|
2018-10-10 04:33:28 +02:00
|
|
|
|
|
|
|
if (is_array($term)) {
|
|
|
|
$term = $term['Value'];
|
|
|
|
}
|
|
|
|
|
2016-02-04 04:07:15 +01:00
|
|
|
$record->{$titleField} = $term;
|
|
|
|
$record->write();
|
2019-04-29 03:07:31 +02:00
|
|
|
if ($source instanceof SS_List) {
|
2019-04-17 07:49:42 +02:00
|
|
|
$source->add($record);
|
|
|
|
}
|
2016-02-04 04:07:15 +01:00
|
|
|
return $record;
|
|
|
|
}
|
2018-11-15 20:33:38 +01:00
|
|
|
|
|
|
|
return false;
|
2015-11-18 05:05:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a JSON string of tags, for lazy loading.
|
|
|
|
*
|
2017-01-13 20:11:59 +01:00
|
|
|
* @param HTTPRequest $request
|
|
|
|
* @return HTTPResponse
|
2015-11-18 05:05:38 +01:00
|
|
|
*/
|
2017-01-13 20:11:59 +01:00
|
|
|
public function suggest(HTTPRequest $request)
|
2015-11-18 05:05:38 +01:00
|
|
|
{
|
|
|
|
$tags = $this->getTags($request->getVar('term'));
|
|
|
|
|
2018-11-15 20:33:38 +01:00
|
|
|
$response = HTTPResponse::create();
|
2015-11-18 05:05:38 +01:00
|
|
|
$response->addHeader('Content-Type', 'application/json');
|
2018-11-15 20:33:38 +01:00
|
|
|
$response->setBody(json_encode(['items' => $tags]));
|
2015-11-18 05:05:38 +01:00
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns array of arrays representing tags.
|
|
|
|
*
|
2017-01-13 20:11:59 +01:00
|
|
|
* @param string $term
|
2015-11-18 05:05:38 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getTags($term)
|
|
|
|
{
|
2017-01-13 20:11:59 +01:00
|
|
|
$source = $this->getSourceList();
|
2018-09-17 08:18:40 +02:00
|
|
|
if (!$source) {
|
|
|
|
return [];
|
|
|
|
}
|
2015-11-18 05:05:38 +01:00
|
|
|
|
|
|
|
$titleField = $this->getTitleField();
|
|
|
|
|
|
|
|
$query = $source
|
|
|
|
->filter($titleField . ':PartialMatch:nocase', $term)
|
|
|
|
->sort($titleField)
|
|
|
|
->limit($this->getLazyLoadItemLimit());
|
|
|
|
|
2018-07-17 06:19:44 +02:00
|
|
|
// Map into a distinct list
|
2018-11-15 20:33:38 +01:00
|
|
|
$items = [];
|
2018-07-17 06:19:44 +02:00
|
|
|
$titleField = $this->getTitleField();
|
|
|
|
foreach ($query->map('ID', $titleField) as $id => $title) {
|
2018-11-15 20:33:38 +01:00
|
|
|
$items[$title] = [
|
2018-10-10 04:33:28 +02:00
|
|
|
'Title' => $title,
|
2019-07-24 05:55:08 +02:00
|
|
|
'Value' => $title,
|
2018-11-15 20:33:38 +01:00
|
|
|
];
|
2018-07-17 06:19:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return array_values($items);
|
2015-11-18 05:05:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2016-07-21 22:52:17 +02:00
|
|
|
|
2016-02-25 02:32:29 +01:00
|
|
|
/**
|
|
|
|
* Converts the field to a readonly variant.
|
|
|
|
*
|
2018-11-15 20:33:38 +01:00
|
|
|
* @return ReadonlyTagField
|
2016-02-25 02:32:29 +01:00
|
|
|
*/
|
|
|
|
public function performReadonlyTransformation()
|
|
|
|
{
|
2018-11-15 20:33:38 +01:00
|
|
|
/** @var ReadonlyTagField $copy */
|
2018-01-19 08:37:55 +01:00
|
|
|
$copy = $this->castedCopy(ReadonlyTagField::class);
|
2017-01-13 20:11:59 +01:00
|
|
|
$copy->setSourceList($this->getSourceList());
|
2020-06-17 06:40:57 +02:00
|
|
|
$copy->setTitleField($this->getTitleField());
|
|
|
|
|
2016-02-25 02:32:29 +01:00
|
|
|
return $copy;
|
|
|
|
}
|
2018-11-15 20:23:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Prevent the default, which would return "tag"
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function Type()
|
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
2018-10-10 04:33:28 +02:00
|
|
|
|
|
|
|
public function getSchemaStateDefaults()
|
|
|
|
{
|
|
|
|
$data = parent::getSchemaStateDefaults();
|
|
|
|
|
|
|
|
// Add options to 'data'
|
|
|
|
$data['lazyLoad'] = $this->getShouldLazyLoad();
|
|
|
|
$data['multi'] = $this->getIsMultiple();
|
|
|
|
$data['optionUrl'] = $this->getSuggestURL();
|
|
|
|
$data['creatable'] = $this->getCanCreate();
|
2019-07-18 23:41:54 +02:00
|
|
|
$options = $this->getOptions(true);
|
|
|
|
$data['value'] = $options->count() ? $options->toNestedArray() : null;
|
2018-10-10 04:33:28 +02:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2015-04-14 03:15:28 +02:00
|
|
|
}
|