Skip to content

Commit

Permalink
Merge branch '2.3' into 2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Garion Herman committed Feb 13, 2020
2 parents ec0d659 + 2620218 commit 01e7f45
Showing 1 changed file with 53 additions and 5 deletions.
58 changes: 53 additions & 5 deletions src/TagField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SilverStripe\TagField;

use Exception;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
Expand All @@ -13,6 +14,8 @@
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DataObjectInterface;
use SilverStripe\ORM\Relation;
use SilverStripe\ORM\SS_List;
use SilverStripe\View\ArrayData;

/**
Expand Down Expand Up @@ -66,13 +69,12 @@ class TagField extends MultiSelectField
/**
* @param string $name
* @param string $title
* @param null|DataList $source
* @param null|DataList|array $source
* @param null|DataList $value
* @param string $titleField
*/
public function __construct($name, $title = '', $source = [], $value = null, $titleField = 'Title')
{
$this->setSourceList($source);
$this->setTitleField($titleField);
parent::__construct($name, $title, $source, $value);

Expand Down Expand Up @@ -180,7 +182,9 @@ public function setTitleField($titleField)
}

/**
* Get the DataList source. The 4.x upgrade for SelectField::setSource starts to convert this to an array
* 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
*
* @return DataList
*/
public function getSourceList()
Expand All @@ -190,7 +194,8 @@ public function getSourceList()

/**
* Set the model class name for tags
* @param DataList $className
*
* @param DataList $sourceList
* @return self
*/
public function setSourceList($sourceList)
Expand Down Expand Up @@ -318,7 +323,41 @@ public function setValue($value, $source = null)
}

/**
* {@inheritdoc}
* Gets the source array if required
*
* Note: this is expensive for a SS_List
*
* @return array
*/
public function getSource()
{
if (is_null($this->source)) {
$this->source = $this->getListMap($this->getSourceList());
}
return $this->source;
}

/**
* 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;
}

/**
* @param DataObject|DataObjectInterface $record DataObject to save data into
* @throws Exception
*/
public function getAttributes()
{
Expand All @@ -340,6 +379,8 @@ public function saveInto(DataObjectInterface $record)
$name = $this->getName();
$titleField = $this->getTitleField();
$values = $this->Value();

/** @var Relation $relation */
$relation = $record->$name();
$ids = [];

Expand Down Expand Up @@ -379,6 +420,10 @@ protected function getOrCreateTag($term)
{
// Check if existing record can be found
$source = $this->getSourceList();
if (!$source) {
return false;
}

$titleField = $this->getTitleField();
$record = $source
->filter($titleField, $term)
Expand Down Expand Up @@ -433,6 +478,9 @@ public function suggest(HTTPRequest $request)
protected function getTags($term)
{
$source = $this->getSourceList();
if (!$source) {
return [];
}

$titleField = $this->getTitleField();

Expand Down

0 comments on commit 01e7f45

Please sign in to comment.