diff --git a/src/StringTagField.php b/src/StringTagField.php index 27bf13a..180d7a5 100644 --- a/src/StringTagField.php +++ b/src/StringTagField.php @@ -254,12 +254,12 @@ class StringTagField extends DropdownField public function setValue($value, $source = null) { if (is_string($value)) { - $value = explode(',', $value); + $value = explode(',', $value ?? ''); } if ($source instanceof DataObject) { $name = $this->getName(); - $value = explode(',', $source->$name); + $value = explode(',', $source->$name ?? ''); } if ($source instanceof SS_List) { @@ -270,7 +270,7 @@ class StringTagField extends DropdownField $value = []; } - return parent::setValue(array_filter($value)); + return parent::setValue(array_filter($value ?? [])); } public function saveInto(DataObjectInterface $record) @@ -328,7 +328,7 @@ class StringTagField extends DropdownField /** @var ArrayData $tag */ $tagValue = $tag->Value; // Map into a distinct list (prevent duplicates) - if (stripos($tagValue, $term) !== false && !array_key_exists($tagValue, $items)) { + if (stripos($tagValue ?? '', $term ?? '') !== false && !array_key_exists($tagValue, $items ?? [])) { $items[$tagValue] = [ 'id' => $tag->Title, 'text' => $tag->Value, @@ -336,7 +336,7 @@ class StringTagField extends DropdownField } } // @todo do we actually need lazy loading limits for StringTagField? - return array_slice(array_values($items), 0, $this->getLazyLoadItemLimit()); + return array_slice(array_values($items ?? []), 0, $this->getLazyLoadItemLimit()); } /** diff --git a/src/TagField.php b/src/TagField.php index d3595bc..2c0b2ef 100644 --- a/src/TagField.php +++ b/src/TagField.php @@ -234,7 +234,7 @@ class TagField extends MultiSelectField ); if (!$this->getShouldLazyLoad()) { - $schema['options'] = array_values($this->getOptions()->toNestedArray()); + $schema['options'] = array_values($this->getOptions()->toNestedArray() ?? []); } else { $schema['optionUrl'] = $this->getSuggestURL(); } @@ -421,7 +421,7 @@ class TagField extends MultiSelectField } } - $relation->setByIDList(array_filter($ids)); + $relation->setByIDList(array_filter($ids ?? [])); } /** @@ -513,7 +513,7 @@ class TagField extends MultiSelectField ]; } - return array_values($items); + return array_values($items ?? []); } /** diff --git a/tests/TagFieldTest.php b/tests/TagFieldTest.php index 4e24ea6..9a70742 100755 --- a/tests/TagFieldTest.php +++ b/tests/TagFieldTest.php @@ -72,7 +72,7 @@ class TagFieldTest extends SapphireTest */ protected function compareTagLists(array $expected, DataList $actualSource) { - $actual = array_values($actualSource->map('ID', 'Title')->toArray()); + $actual = array_values($actualSource->map('ID', 'Title')->toArray() ?? []); sort($expected); sort($actual);