-
Notifications
You must be signed in to change notification settings - Fork 333
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
Batch actions form - Custom parameter fields enhancement #2744
Comments
Currently writing the PRs for this now |
silverstripe-cms/code/Controllers/CMSMain.php Line 2122 in d304ba9
Don't know if that changes anything for the PRs but seemed worth pointing out. |
@GuySartorelli I apologize if my wording was confusing. To clarify, when I said
In context I was referring to the BatchActionParameters() method in the CMSMain class that calls the But by all means if you can find a reference or call to |
Ahhhh that makes sense. Yeah I can't find any references to |
No Problem Guy @GuySartorelli. I'm glad to hear that you also were unable to find any references as that will lessen the impact of my PR |
Hey Guy @GuySartorelli, I wrote up a test batch action subclass to hopefully help reduce the time investment for testing my PRs It's pretty simple and doesn't actually make any changes to the selected pages, but it will add three parameter fields to the interface (a dropdown, textfield, and checkbox field) and then when you run the action the values are retrieved in the CMSBatchAction_ParameterAction.php <?php
namespace Sitelease\OpenCore\BatchActions;
use SilverStripe\ORM\SS_List;
use SilverStripe\ORM\DataObject;
use SilverStripe\Control\Controller;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Admin\CMSBatchAction;
/**
* An example action to test parameter fields
*/
class CMSBatchAction_ParameterAction extends CMSBatchAction
{
public function getActionTitle()
{
return _t(__CLASS__ . '.ParameterAction', 'Parameter Action');
}
public function run(SS_List $pages)
{
$request = Controller::curr()->getRequest();
$dropdownValue = $request->postVar('ExampleDropdown');
$textValue = $request->postVar('ExampleTextField');
$checkboxValue = $request->postVar('ExampleCheckbox') ?? false;
return $this->mockBatchAction($pages, array(
$dropdownValue,
$textValue,
$checkboxValue ? 'checked' : 'unchecked'
));
}
public function mockBatchAction(SS_List $objs, $parameters)
{
$status = array('modified' => array(), 'error' => array(), 'deleted' => array(), 'success' => array(), 'parameters' => $parameters);
foreach ($objs as $obj) {
// Perform the action
$id = $obj->ID;
$status['success'][$id] = $id;
// Now make sure the tree title is appropriately updated
$publishedRecord = DataObject::get_by_id($this->managedClass, $id);
if ($publishedRecord) {
$status['modified'][$id] = array(
'TreeTitle' => $publishedRecord->TreeTitle,
);
} else {
$status['deleted'][$id] = $id;
}
$obj->destroy();
unset($obj);
}
return $this->response("Success Message", $status);
}
/**
* {@see SiteTree::canEdit()}
*
* @param array $ids
* @return array
*/
public function applicablePages($ids)
{
return $this->applicablePagesHelper($ids, 'canView');
}
/**
* Returns a FieldList containing extra fields for this action
*
* @return FieldList
*/
function getParameterFields() {
return FieldList::create(
DropdownField::create("ExampleDropdown", false, array(
"option_1" => "Option 1",
"option_2" => "Option 2",
"option_3" => "Option 3",
))
->setEmptyString('-- Select an Option --')
->setDescription("A filler description for the dropdown field"),
TextField::create(
'ExampleTextField',
false
)
->setAttribute("placeholder", "Example field"),
CheckboxField::create(
'ExampleCheckbox',
'Enable Something - Will enable something if checked'
)
);
}
} _config.php use SilverStripe\Admin\CMSBatchActionHandler;
use Some\Namespace\CMSBatchAction_ParameterAction;
CMSBatchActionHandler::register('parameteraction', CMSBatchAction_ParameterAction::class); I attached some screenshots showing the test values passed in and the expected output in the developer tools (in firefox) |
Thank you! That will help a lot. |
Glad to hear it 💯 |
PRs merged. Thank you for contributing this. |
Fantastic, Thank you Guy @GuySartorelli and Michal @michalkleiner for all the time you guys spent on looking this over and testing it. I appreciate it 🎊 |
Background Information
When creating my own
CMSBatchAction
subclass I discovered that it had support for adding custom parameter fields via agetParameterFields()
method, which is later processed by aBatchActionParameters()
method in the CMSMain class. The strange thing is that this method is never called anywhere in the framework modules.Enhancement
The goal of this enhancement (which I will create two PRs for shortly) is to implement the
BatchActionParameters()
method by adding an additional section to theBatchActionsForm
in LeftAndMain which will display and submit parameter fields for each batch action that defines it's own fields via thegetParameterFields()
method.With this change, developers will be able to easily create batch actions that take any number of inputs, allowing for more powerful actions like Adrexia's "Move to" action (Shout out to her for her past work on this 🎊)
Working Example
Custom Action
CMSBatchAction_PublishInLocal.php
NOTE: This code is incomplete and just for example purposes, you would of course need to write
run()
andapplicablePages()
methods, register this action usingCMSBatchActionHandler::register()
, etc.Resulting interface
PRs
The text was updated successfully, but these errors were encountered: