Skip to content

Commit

Permalink
ArrayHelper::ensureNonAssociative()
Browse files Browse the repository at this point in the history
Implements a better fix for #3947
  • Loading branch information
brandonkelly committed Mar 8, 2019
1 parent 1a1caf6 commit 65f0c14
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes for Craft CMS 3.x

## Unreleased

### Added
- Added `craft\helpers\ArrayHelper::ensureNonAssociative()`.

## 3.1.17 - 2019-03-08

### Changed
Expand Down
18 changes: 3 additions & 15 deletions src/fields/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use craft\base\ElementInterface;
use craft\base\Field;
use craft\fields\data\ColorData;
use craft\helpers\ArrayHelper;
use craft\helpers\DateTimeHelper;
use craft\helpers\Json;
use craft\validators\ColorValidator;
Expand Down Expand Up @@ -99,6 +100,8 @@ public function init()

if (!is_array($this->defaults)) {
$this->defaults = [];
} else {
ArrayHelper::ensureNonAssociative($this->defaults);
}

// Convert default date cell values to ISO8601 strings
Expand All @@ -125,21 +128,6 @@ public function rules()
return $rules;
}

/**
* @inheritdoc
*/
public function getSettings(): array
{
$settings = parent::getSettings();

// Turn defaults into a non-associative array so it doesn't get reordered when saved to the project config
if (!empty($settings['defaults'])) {
$settings['defaults'] = array_values($settings['defaults']);
}

return $settings;
}

/**
* @return bool whether minRows was set
*/
Expand Down
12 changes: 12 additions & 0 deletions src/helpers/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,16 @@ public static function withoutValue(array $array, $value): array
static::removeValue($array, $value);
return $array;
}

/**
* Ensures an array is non-associative.
*
* @param array $array
*/
public static function ensureNonAssociative(array &$array)
{
if (static::isAssociative($array, false)) {
$array = array_values($array);
}
}
}

0 comments on commit 65f0c14

Please sign in to comment.