BUG Fix for $source left null

BUG Fix missing imports
Semver compatible setSource()
This commit is contained in:
Damian Mooyman 2018-09-17 18:18:40 +12:00
parent 024e648e5a
commit 690f0cc793
No known key found for this signature in database
GPG Key ID: 4327857F75021D92
1 changed files with 43 additions and 14 deletions

View File

@ -2,15 +2,18 @@
namespace SilverStripe\TagField; namespace SilverStripe\TagField;
use Exception;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse; use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\Validator;
use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataList; use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DataObjectInterface; use SilverStripe\ORM\DataObjectInterface;
use SilverStripe\ORM\Relation;
use SilverStripe\ORM\SS_List; use SilverStripe\ORM\SS_List;
use SilverStripe\View\ArrayData; use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements; use SilverStripe\View\Requirements;
@ -67,11 +70,10 @@ class TagField extends DropdownField
* @param null|DataList $value * @param null|DataList $value
* @param string $titleField * @param string $titleField
*/ */
public function __construct($name, $title = '', $source = [], $value = null, $titleField = 'Title') public function __construct($name, $title = '', $source = null, $value = null, $titleField = 'Title')
{ {
$this->setSourceList($source);
$this->setTitleField($titleField); $this->setTitleField($titleField);
parent::__construct($name, $title, [], $value); parent::__construct($name, $title, $source, $value);
} }
/** /**
@ -175,7 +177,9 @@ class TagField extends DropdownField
} }
/** /**
* 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 * @return DataList
*/ */
public function getSourceList() public function getSourceList()
@ -185,7 +189,8 @@ class TagField extends DropdownField
/** /**
* Set the model class name for tags * Set the model class name for tags
* @param DataList $className *
* @param DataList $sourceList
* @return self * @return self
*/ */
public function setSourceList($sourceList) public function setSourceList($sourceList)
@ -322,11 +327,29 @@ class TagField extends DropdownField
public function getSource() public function getSource()
{ {
if (is_null($this->source)) { if (is_null($this->source)) {
$this->setSource($this->getSourceList()); $this->source = $this->getListMap($this->getSourceList());
} }
return $this->source; 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;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -342,7 +365,8 @@ class TagField extends DropdownField
} }
/** /**
* {@inheritdoc} * @param DataObject|DataObjectInterface $record DataObject to save data into
* @throws Exception
*/ */
public function saveInto(DataObjectInterface $record) public function saveInto(DataObjectInterface $record)
{ {
@ -350,8 +374,9 @@ class TagField extends DropdownField
$name = $this->getName(); $name = $this->getName();
$titleField = $this->getTitleField(); $titleField = $this->getTitleField();
$source = $this->getSource();
$values = $this->Value(); $values = $this->Value();
/** @var Relation $relation */
$relation = $record->$name(); $relation = $record->$name();
$ids = array(); $ids = array();
@ -384,13 +409,16 @@ class TagField extends DropdownField
* Get or create tag with the given value * Get or create tag with the given value
* *
* @param string $term * @param string $term
* @return DataObject * @return DataObject|false
*/ */
protected function getOrCreateTag($term) protected function getOrCreateTag($term)
{ {
// Check if existing record can be found // Check if existing record can be found
/** @var DataList $source */
$source = $this->getSourceList(); $source = $this->getSourceList();
if (!$source) {
return false;
}
$titleField = $this->getTitleField(); $titleField = $this->getTitleField();
$record = $source $record = $source
->filter($titleField, $term) ->filter($titleField, $term)
@ -436,10 +464,10 @@ class TagField extends DropdownField
*/ */
protected function getTags($term) protected function getTags($term)
{ {
/**
* @var array $source
*/
$source = $this->getSourceList(); $source = $this->getSourceList();
if (!$source) {
return [];
}
$titleField = $this->getTitleField(); $titleField = $this->getTitleField();
@ -476,10 +504,11 @@ class TagField extends DropdownField
/** /**
* Converts the field to a readonly variant. * Converts the field to a readonly variant.
* *
* @return TagField_Readonly * @return ReadonlyTagField
*/ */
public function performReadonlyTransformation() public function performReadonlyTransformation()
{ {
/** @var ReadonlyTagField $copy */
$copy = $this->castedCopy(ReadonlyTagField::class); $copy = $this->castedCopy(ReadonlyTagField::class);
$copy->setSourceList($this->getSourceList()); $copy->setSourceList($this->getSourceList());
return $copy; return $copy;