Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed GridFieldOrderableRows issue when data class is Versioned and relation is has_many #243

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions src/GridFieldOrderableRows.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObjectInterface;
use SilverStripe\ORM\ManyManyList;
use SilverStripe\ORM\Map;
use SilverStripe\ORM\SS_List;
use SilverStripe\ORM\SS_Map;
use SilverStripe\Versioned\Versioned;
use SilverStripe\View\ViewableData;
use Exception;

Expand Down Expand Up @@ -180,24 +181,19 @@ public function setReorderColumnNumber($colno)
public function getSortTable(SS_List $list)
{
$field = $this->getSortField();

if ($list instanceof ManyManyList) {
$extra = $list->getExtraFields();
$table = $list->getJoinTable();

if ($extra && array_key_exists($field, $extra)) {
return $table;
}
}

$classes = ClassInfo::dataClassesFor($list->dataClass());

foreach ($classes as $class) {
if (singleton($class)->hasDataBaseField($field)) {
return DataObject::getSchema()->tableName($class);
}
}

throw new \Exception("Couldn't find the sort field '$field'");
}

Expand Down Expand Up @@ -499,15 +495,15 @@ protected function reorderItems($list, array $values, array $sortedIDs)
/** @var SS_List $map */
$map = $list->map('ID', $sortField);
//fix for versions of SS that return inconsistent types for `map` function
if ($map instanceof SS_Map) {
if ($map instanceof Map) {
$map = $map->toArray();
}

// If not a ManyManyList and using versioning, detect it.
$isVersioned = false;
$class = $list->dataClass();
if ($class == $this->getSortTable($list)) {
$isVersioned = $class::has_extension('SilverStripe\\ORM\\Versioning\\Versioned');
if (DataObject::getSchema()->tableName($class) == $this->getSortTable($list)) {
$isVersioned = $class::has_extension(Versioned::class);
}

// Loop through each item, and update the sort values which do not
Expand Down