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

Change GridfieldDetailForm to be opt-in #35

Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ before_script:
- phpenv config-rm xdebug.ini
- composer validate
- composer install --prefer-dist
- composer require symfony/config:^3.2 silverstripe/framework:4.0.x-dev embed/embed:^3.0@stable silverstripe/assets:1.0.x-dev silverstripe/config:1.0.x-dev --prefer-dist --no-update
- composer require symfony/config:^3.2 silverstripe/framework:4.0.x-dev embed/embed:^3.0@stable silverstripe/assets:1.0.x-dev silverstripe/admin:1.0.x-dev silverstripe/config:1.0.x-dev --prefer-dist --no-update
- composer update
- "if [ \"$DB\" = \"PGSQL\" ]; then composer require silverstripe/postgresql:2.0.x-dev --prefer-dist; fi"

Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"silverstripe/framework": "^4@dev"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
"phpunit/phpunit": "^5.7",
"silverstripe/admin": "^1"
},
"extra": {
"branch-alias": {
Expand All @@ -37,7 +38,8 @@
}
},
"scripts": {
"lint": "phpcs src/ tests/php/"
"lint": "phpcs src/ tests/php/",
"lint-clean": "phpcbf src/ tests/php/"
},
"minimum-stability": "dev",
"prefer-stable": true
Expand Down
6 changes: 4 additions & 2 deletions src/VersionedGridFieldDetailForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
class VersionedGridFieldDetailForm extends Extension
{

/**
* @param string $class
* @param GridField $gridField
Expand All @@ -22,7 +21,10 @@ class VersionedGridFieldDetailForm extends Extension
public function updateItemRequestClass(&$class, $gridField, $record, $requestHandler)
{
// Conditionally use a versioned item handler
if ($record && $record->has_extension(Versioned::class)) {
if ($record
&& $record->has_extension(Versioned::class)
&& $record->config()->get('versioned_gridfield_extensions')
) {
$class = VersionedGridFieldItemRequest::class;
}
}
Expand Down
107 changes: 55 additions & 52 deletions src/VersionedGridFieldItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Convert;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
Expand All @@ -20,66 +21,68 @@ class VersionedGridFieldItemRequest extends GridFieldDetailForm_ItemRequest

protected function getFormActions()
{
$actions = parent::getFormActions();

// Check if record is versionable
/** @var Versioned|DataObject $record */
$record = $this->getRecord();
if (!$record || !$record->has_extension(Versioned::class)) {
return $actions;
}

// Save & Publish action
if ($record->canPublish()) {
// "publish", as with "save", it supports an alternate state to show when action is needed.
$publish = FormAction::create(
'doPublish',
_t('SilverStripe\\Versioned\\VersionedGridFieldItemRequest.BUTTONPUBLISH', 'Publish')
)
->setUseButtonTag(true)
->addExtraClass('btn btn-primary font-icon-rocket');

// Insert after save
if ($actions->fieldByName('action_doSave')) {
$actions->insertAfter('action_doSave', $publish);
} else {
$actions->push($publish);
}
return parent::getFormActions();
}

// Unpublish action
$isPublished = $record->isPublished();
if ($isPublished && $record->canUnpublish()) {
$actions->push(
FormAction::create(
'doUnpublish',
_t('SilverStripe\\Versioned\\VersionedGridFieldItemRequest.BUTTONUNPUBLISH', 'Unpublish')
// Add extra actions prior to extensions so that these can be modified too
$this->beforeExtending('updateFormActions', function (FieldList $actions) use ($record) {
// Save & Publish action
if ($record->canPublish()) {
// "publish", as with "save", it supports an alternate state to show when action is needed.
$publish = FormAction::create(
'doPublish',
_t(__CLASS__.'.BUTTONPUBLISH', 'Publish')
)
->setUseButtonTag(true)
->setDescription(_t(
'SilverStripe\\Versioned\\VersionedGridFieldItemRequest.BUTTONUNPUBLISHDESC',
'Remove this record from the published site'
))
->addExtraClass('btn-secondary')
);
}
->addExtraClass('btn btn-primary font-icon-rocket');

// Insert after save
if ($actions->fieldByName('action_doSave')) {
$actions->insertAfter('action_doSave', $publish);
} else {
$actions->push($publish);
}
}

// Archive action
if ($record->canArchive()) {
// Replace "delete" action
$actions->removeByName('action_doDelete');

// "archive"
$actions->push(
FormAction::create('doArchive', _t('SilverStripe\\Versioned\\VersionedGridFieldItemRequest.ARCHIVE', 'Archive'))
->setDescription(_t(
'SilverStripe\\Versioned\\VersionedGridFieldItemRequest.BUTTONARCHIVEDESC',
'Unpublish and send to archive'
))
->addExtraClass('delete btn-secondary')
);
}
return $actions;
// Unpublish action
$isPublished = $record->isPublished();
if ($isPublished && $record->canUnpublish()) {
$actions->push(
FormAction::create(
'doUnpublish',
_t(__CLASS__.'.BUTTONUNPUBLISH', 'Unpublish')
)
->setUseButtonTag(true)
->setDescription(_t(
__CLASS__.'.BUTTONUNPUBLISHDESC',
'Remove this record from the published site'
))
->addExtraClass('btn-secondary')
);
}

// Archive action
if ($record->canArchive()) {
// Replace "delete" action
$actions->removeByName('action_doDelete');

// "archive"
$actions->push(
FormAction::create('doArchive', _t(__CLASS__.'.ARCHIVE', 'Archive'))
->setDescription(_t(
__CLASS__.'.BUTTONARCHIVEDESC',
'Unpublish and send to archive'
))
->addExtraClass('delete btn-secondary')
);
}
});

return parent::getFormActions();
}

/**
Expand Down Expand Up @@ -143,7 +146,7 @@ public function doPublish($data, $form)
$xmlTitle = Convert::raw2xml($record->Title);
$link = "<a href=\"{$editURL}\">{$xmlTitle}</a>";
$message = _t(
'SilverStripe\\Versioned\\VersionedGridFieldItemRequest.Published',
__CLASS__.'.Published',
'Published {name} {link}',
[
'name' => $record->i18n_singular_name(),
Expand Down
2 changes: 1 addition & 1 deletion tests/php/ChangeSetItemTest/VersionedObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class VersionedObject extends DataObject implements TestOnly
Versioned::class
];

function canEdit($member = null)
public function canEdit($member = null)
{
return true;
}
Expand Down
65 changes: 65 additions & 0 deletions tests/php/VersionedGridFieldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace SilverStripe\Versioned\Tests;

use SilverStripe\Dev\CSSContentParser;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Versioned\Versioned;

class VersionedGridFieldTest extends FunctionalTest
{
protected static $fixture_file = 'VersionedTest.yml';

protected function setUp()
{
parent::setUp();
$this->useDraftSite(true);
}

protected static $extra_controllers = [
VersionedGridFieldTest\TestController::class,
];

public static function getExtraDataObjects()
{
return VersionedTest::getExtraDataObjects();
}

public function testEditForm()
{
$this->logInWithPermission('ADMIN');
$response = $this->get('VersionedGridFieldTest_Controller');
$this->assertFalse($response->isError());

$parser = new CSSContentParser($response->getBody());
$editlinkitem = $parser->getBySelector('.ss-gridfield-items .first .edit-link');
$editlink = (string)$editlinkitem[0]['href'];

$response = $this->get($editlink);
$this->assertFalse($response->isError());

$parser = new CSSContentParser($response->getBody());
$editform = $parser->getBySelector('#Form_ItemEditForm');
$editformurl = (string)$editform[0]['action'];

// Modify this
$response = $this->post(
$editformurl,
[
'Title' => 'Page 1 renamed',
'action_doPublish' => 1
]
);
$this->assertFalse($response->isError());

// Ensure page is on live and draft
$recordID = $this->idFromFixture(VersionedTest\TestObject::class, 'page1');
$draftPage = Versioned::get_by_stage(VersionedTest\TestObject::class, Versioned::DRAFT)->byID($recordID);
$livePage = Versioned::get_by_stage(VersionedTest\TestObject::class, Versioned::LIVE)->byID($recordID);

$this->assertNotNull($draftPage);
$this->assertNotNull($livePage);
$this->assertEquals('Page 1 renamed', $draftPage->Title);
$this->assertEquals('Page 1 renamed', $livePage->Title);
}
}
43 changes: 43 additions & 0 deletions tests/php/VersionedGridFieldTest/TestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace SilverStripe\Versioned\Tests\VersionedGridFieldTest;

use SilverStripe\Control\Controller;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
use SilverStripe\Versioned\Tests\VersionedTest\TestObject;
use SilverStripe\Versioned\Versioned;

/**
* @skipUpgrade
*/
class TestController extends Controller implements TestOnly
{
public function __construct()
{
parent::__construct();
if (Controller::has_curr()) {
$this->setRequest(Controller::curr()->getRequest());
}
}

public function Link($action = null)
{
return Controller::join_links('VersionedGridFieldTest_Controller', $action, '/');
}

private static $allowed_actions = ['Form'];

protected $template = 'BlankPage';

public function Form()
{
$objects = TestObject::get()
->sort('"VersionedTest_DataObject"."ID" ASC');
$field = new GridField('testfield', 'testfield', $objects, GridFieldConfig_RelationEditor::create());
return new Form($this, 'Form', new FieldList($field), new FieldList());
}
}
1 change: 0 additions & 1 deletion tests/php/VersionedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use DateTime;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Control\Session;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
Expand Down
9 changes: 8 additions & 1 deletion tests/php/VersionedTest/TestObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ class TestObject extends DataObject implements TestOnly
{
private static $table_name = 'VersionedTest_DataObject';

/**
* Enable extensions in gridfield
*
* @config
* @var bool
*/
private static $versioned_gridfield_extensions = true;

private static $db = [
"Name" => "Varchar",
'Title' => 'Varchar',
Expand All @@ -43,7 +51,6 @@ class TestObject extends DataObject implements TestOnly
'Related' => RelatedWithoutversion::class,
];


public function canView($member = null)
{
$extended = $this->extendedCan(__FUNCTION__, $member);
Expand Down