Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
altwohill committed Feb 26, 2019
1 parent cba678e commit 3cc582d
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/
composer.lock
vendor/
resources/
86 changes: 86 additions & 0 deletions README.md
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();
}
}
```
18 changes: 18 additions & 0 deletions composer.json
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/"
}
}
}
37 changes: 37 additions & 0 deletions src/Forms/NewWindowAction.php
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;
}
}

0 comments on commit 3cc582d

Please sign in to comment.