-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.idea/ | ||
composer.lock | ||
vendor/ | ||
resources/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,88 @@ | ||
# silverstripe-gridfield-newwindowaction | ||
Adds a button to the SilverStripe GridField that causes an action to open in a new window. | ||
|
||
## How to install | ||
`composer require twohill/silverstripe-gridfield-newwindowaction` | ||
|
||
## How to use | ||
|
||
Simply replace your normal `SilverStripe\Forms\FormAction` with `Twohill\Forms\NewWindowAction` | ||
|
||
## Typical usecase | ||
|
||
### MyModelAdmin.php | ||
|
||
```php | ||
<?php | ||
|
||
namespace My\Admin; | ||
|
||
use My\Model; | ||
use My\ModelGridFieldFormItemRequest; | ||
use SilverStripe\Admin\ModelAdmin; | ||
use SilverStripe\Forms\GridField\GridField; | ||
use SilverStripe\Forms\GridField\GridFieldDetailForm; | ||
|
||
class MyModelAdmin extends ModelAdmin | ||
{ | ||
// Usual ModelAdmin stuff here | ||
private static $managed_models = [Model::class]; | ||
public function getEditForm($id = null, $fields = null) | ||
{ | ||
$form = parent::getEditForm($id, $fields); | ||
|
||
/** @var GridField $gridField */ | ||
$gridField = $form->Fields()->first(); | ||
$config = $gridField->getConfig(); | ||
/** @var GridFieldDetailForm $myForm */ | ||
$myForm = $config->getComponentByType(GridFieldDetailForm::class); | ||
$myForm->setItemRequestClass(ModelGridFieldFormItemRequest::class); | ||
|
||
return $form; | ||
} | ||
|
||
} | ||
``` | ||
|
||
### ModelGridFieldFormItemRequest.php | ||
|
||
```php | ||
<?php | ||
|
||
namespace My\Admin; | ||
|
||
use My\Model; | ||
use Twohill\Forms\NewWindowAction; | ||
use SilverStripe\Versioned\VersionedGridFieldItemRequest; | ||
|
||
class ModelGridFieldFormItemRequest extends VersionedGridFieldItemRequest | ||
{ | ||
|
||
private static $allowed_actions = [ | ||
'edit', | ||
'view', | ||
'ItemEditForm', | ||
'generatePDFInvoice', | ||
]; | ||
|
||
|
||
protected function getFormActions() | ||
{ | ||
$actions = parent::getFormActions(); | ||
|
||
$actions->push(NewWindowAction::create('generatePDFInvoice', 'Generate PDF Invoice') | ||
->setUseButtonTag(true) | ||
->addExtraClass('btn-primary font-icon-block-content')); | ||
|
||
|
||
return $actions; | ||
} | ||
|
||
|
||
public function generatePDFInvoice() { | ||
/** @var Model $invoice */ | ||
$invoice = $this->getRecord(); | ||
//$invoice->generatePDFInvoice(); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "twohill/silverstripe-gridfield-newwindowaction", | ||
"description": "Adds a button to the SilverStripe GridField that causes an action to open in a new window.\n", | ||
"homepage": "https://github.com/twohill/silverstripe-gridfield-newwindowaction", | ||
"keywords": ["silverstripe", "gridfield", "newwindow"], | ||
"license": "BSD-3-Clause", | ||
"authors": [ | ||
{"name": "Al Twohill", "email": "[email protected]"} | ||
], | ||
"require": { | ||
"silverstripe/cms": "^4" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Twohill\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
namespace Twohill\Forms; | ||
|
||
use SilverStripe\Control\Controller; | ||
use SilverStripe\Forms\FormAction; | ||
|
||
class NewWindowAction extends FormAction | ||
{ | ||
/** | ||
* Gets the HTML representing the button | ||
* @param array $properties | ||
* @return string | ||
*/ | ||
public function Field($properties = array()) | ||
{ | ||
return sprintf( | ||
'<a class="btn %s" href="%s" target="_blank">%s</a>', | ||
$this->extraClass(), | ||
$this->Link(), | ||
$this->Title() | ||
); | ||
} | ||
|
||
/** | ||
* Return a link to this field. | ||
* | ||
* @param string $action | ||
* | ||
* @return string | ||
*/ | ||
public function Link($action = null) | ||
{ | ||
$link = Controller::join_links($this->form->getController()->Link(), $this->actionName(), $action); | ||
$this->extend('updateLink', $link, $action); | ||
return $link; | ||
} | ||
} |