silverstripe-framework/tests/php/Forms/FormFactoryTest/ControllerExtension.php

93 lines
2.5 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\Forms\Tests\FormFactoryTest;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\LiteralField;
use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\Versioned;
/**
* Provides versionable extensions to a controller / scaffolder
*/
class ControllerExtension extends Extension
{
/**
* Handlers for extra actions added by this extension
*
* @var array
*/
private static $allowed_actions = [
'publish',
'preview',
];
/**
* Adds additional form actions
*
* @param FieldList $actions
* @param Controller $controller
* @param string $name
* @param array $context
*/
public function updateFormActions(FieldList &$actions, Controller $controller, $name, $context = [])
{
// Add publish button if record is versioned
if (empty($context['Record'])) {
return;
}
/** @var Versioned|DataObject $record */
$record = $context['Record'];
if ($record->hasExtension(Versioned::class) && $record->hasStages()) {
$actions->push(new FormAction('publish', 'Publish'));
}
}
/**
* Adds extra fields to this form
*
* @param FieldList $fields
* @param Controller $controller
* @param string $name
* @param array $context
*/
public function updateFormFields(FieldList &$fields, Controller $controller, $name, $context = [])
{
// Add preview link
if (empty($context['Record'])) {
return;
}
/** @var Versioned|DataObject $record */
$record = $context['Record'];
if ($record->hasExtension(Versioned::class) && $record->hasStages()) {
$link = $controller->Link('preview');
$fields->unshift(
new LiteralField(
"PreviewLink",
sprintf('<a href="%s" rel="external" target="_blank">Preview</a>', Convert::raw2att($link))
)
);
}
}
public function publish(array $data, Form $form): HTTPResponse
{
// noop
return HTTPResponse::create();
}
public function preview(HTTPRequest $request): HTTPResponse
{
// noop
return HTTPResponse::create();
}
}