Skip to content

Commit

Permalink
FIX GridFieldOrderableRows to support polymorphic ManyManyThroughList
Browse files Browse the repository at this point in the history
  • Loading branch information
michalkleiner authored and dnsl48 committed May 18, 2020
1 parent a710c81 commit 9944b67
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 3 deletions.
22 changes: 20 additions & 2 deletions src/GridFieldOrderableRows.php
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ protected function getSortTableClauseForIds(DataList $list, $ids)
$introspector->getExtraFields() :
DataObjectSchema::create()->fieldSpecs($introspector->getJoinClass(), DataObjectSchema::DB_ONLY);
$key = $introspector->getLocalKey();
$foreignKey = $introspector->getForeignKey();
$foreignKey = $this->getManyManyInspectorForeignKey($introspector);
$foreignID = (int) $list->getForeignID();

if ($extra && array_key_exists($this->getSortField(), $extra)) {
Expand Down Expand Up @@ -755,6 +755,24 @@ protected function getManyManyInspector($list)
return $inspector;
}


/**
* Depending on the list inspector and the list itself (ManyMany vs ManyManyThrough), the method to obtain
* the foreign key may be different.
*
* @param $inspector
* @return string
*/
private function getManyManyInspectorForeignKey($inspector)
{
if (($inspector instanceof ManyManyThroughQueryManipulator) && (method_exists($inspector, 'getForeignIDKey'))) {
// This method has been introduced in framework 4.1
return $inspector->getForeignIDKey();
}

return $inspector->getForeignKey();
}

/**
* Used to get sort orders from a many many through list relationship record, rather than the current
* record itself.
Expand All @@ -768,7 +786,7 @@ protected function getSortValuesFromManyManyThroughList($list, $sortField)

// Find the foreign key name, ID and class to look up
$joinClass = $manipulator->getJoinClass();
$fromRelationName = $manipulator->getForeignKey();
$fromRelationName = $this->getManyManyInspectorForeignKey($manipulator);
$toRelationName = $manipulator->getLocalKey();

// Create a list of the MMTL relations
Expand Down
44 changes: 43 additions & 1 deletion tests/GridFieldOrderableRowsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
use Symbiote\GridFieldExtensions\Tests\Stub\PolymorphM2MChild;
use Symbiote\GridFieldExtensions\Tests\Stub\PolymorphM2MMapper;
use Symbiote\GridFieldExtensions\Tests\Stub\PolymorphM2MParent;
use Symbiote\GridFieldExtensions\Tests\Stub\StubOrderableChild;
use Symbiote\GridFieldExtensions\Tests\Stub\StubOrdered;
use Symbiote\GridFieldExtensions\Tests\Stub\StubOrderedVersioned;
Expand All @@ -25,10 +28,14 @@ class GridFieldOrderableRowsTest extends SapphireTest
{
protected static $fixture_file = [
'GridFieldOrderableRowsTest.yml',
'OrderableRowsThroughTest.yml'
'OrderableRowsThroughTest.yml',
// 'OrderablePolymorphicManyToMany.yml' // TODO: introduce this tests in the next minor release
];

protected static $extra_dataobjects = [
// PolymorphM2MChild::class,
// PolymorphM2MMapper::class,
// PolymorphM2MParent::class,
StubParent::class,
StubOrdered::class,
StubSubclass::class,
Expand All @@ -46,6 +53,7 @@ public function reorderItemsProvider()
return [
[StubParent::class . '.parent', 'MyManyMany', 'ManyManySort'],
[ThroughDefiner::class . '.DefinerOne', 'Belongings', 'Sort'],
// [PolymorphM2MParent::class . '.ParentOne', 'Children', 'Sort']
];
}

Expand Down Expand Up @@ -122,6 +130,40 @@ public function testManyManyThroughListSortOrdersAreUsedForInitialRender()
);
}

public function testPolymorphicManyManyListSortOrdersAreUsedForInitialRender()
{
$this->markTestSkipped('TODO: Introduce this test in the next minor release (3.3)');

$record = $this->objFromFixture(PolymorphM2MParent::class, 'ParentOne');

$orderable = new GridFieldOrderableRows('Sort');
$config = new GridFieldConfig_RelationEditor();
$config->addComponent($orderable);

$grid = new GridField(
'Children',
'Testing Polymorphic Many Many',
$record->Children()->sort('Sort'),
$config
);

// Get the first record, which would be the first one to have column contents generated
$intermediary = $this->objFromFixture(PolymorphM2MMapper::class, 'MapP1ToC1');

$result = $orderable->getColumnContent($grid, $record, 'irrelevant');

$this->assertContains(
'Children[GridFieldEditableColumns][' . $record->ID . '][Sort]',
$result,
'The field name is indexed under the record\'s ID'
);
$this->assertContains(
'value="' . $intermediary->Sort . '"',
$result,
'The value comes from the MMTL intermediary Sort value'
);
}

public function testSortableChildClass()
{
$orderable = new GridFieldOrderableRows('Sort');
Expand Down
28 changes: 28 additions & 0 deletions tests/OrderablePolymorphicManyToMany.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Symbiote\GridFieldExtensions\Tests\Stub\PolymorphM2MParent:
ParentOne:
ParentTwo:

Symbiote\GridFieldExtensions\Tests\Stub\PolymorphM2MChild:
ChildOne:
ChildTwo:

Symbiote\GridFieldExtensions\Tests\Stub\PolymorphM2MMapper:
MapP1ToC1:
Parent: '=>Symbiote\GridFieldExtensions\Tests\Stub\PolymorphM2MParent.ParentOne'
Child: '=>Symbiote\GridFieldExtensions\Tests\Stub\PolymorphM2MChild.ChildOne'
Sort: 1

MapP1ToC2:
Parent: '=>Symbiote\GridFieldExtensions\Tests\Stub\PolymorphM2MParent.ParentOne'
Child: '=>Symbiote\GridFieldExtensions\Tests\Stub\PolymorphM2MChild.ChildTwo'
Sort: 2

MapP2ToC1:
Parent: '=>Symbiote\GridFieldExtensions\Tests\Stub\PolymorphM2MParent.ParentTwo'
Child: '=>Symbiote\GridFieldExtensions\Tests\Stub\PolymorphM2MChild.ChildOne'
Sort: 2

MapP2ToC2:
Parent: '=>Symbiote\GridFieldExtensions\Tests\Stub\PolymorphM2MParent.ParentTwo'
Child: '=>Symbiote\GridFieldExtensions\Tests\Stub\PolymorphM2MChild.ChildTwo'
Sort: 1
15 changes: 15 additions & 0 deletions tests/Stub/PolymorphM2MChild.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Symbiote\GridFieldExtensions\Tests\Stub;

use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;

class PolymorphM2MChild extends DataObject implements TestOnly
{
private static $table_name = 'TestOnly_PolymorphM2MChild';

private static $has_many = [
'Parents' => PolymorphM2MMapper::class
];
}
22 changes: 22 additions & 0 deletions tests/Stub/PolymorphM2MMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Symbiote\GridFieldExtensions\Tests\Stub;

use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;

class PolymorphM2MMapper extends DataObject implements TestOnly
{
private static $table_name = 'TestOnly_PolymorphM2MMapper';

private static $db = [
'Sort' => 'Int'
];

private static $has_one = [
'Parent' => DataObject::class, // PolymorphM2MParent
'Child' => PolymorphM2MChild::class,
];

private static $default_sort = '"Sort" ASC';
}
19 changes: 19 additions & 0 deletions tests/Stub/PolymorphM2MParent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Symbiote\GridFieldExtensions\Tests\Stub;

use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;

class PolymorphM2MParent extends DataObject implements TestOnly
{
private static $table_name = 'TableOnly_PolymorphM2MParent';

private static $many_many = [
'Children' => [
'through' => PolymorphM2MMapper::class,
'from' => 'Parent',
'to' => 'Child',
]
];
}

0 comments on commit 9944b67

Please sign in to comment.