SilverStripe: How to set/ specify the icon for GridField custom action button - silverstripe

I am working on a SilverStripe project. In my project, I am trying to create a GridField custom button. I followed the official documentation. Following is the class form the SilverStripe official page.
class GridFieldCustomAction implements GridField_ColumnProvider, GridField_ActionProvider, GridField_ActionMenuItem
{
public function getTitle($gridField, $record, $columnName)
{
return 'Custom action';
}
public function getCustomAction($gridField, $record)
{
if (!$record->canEdit()) {
return;
}
return GridField_FormAction::create(
$gridField,
'CustomAction'.$record->ID,
'Custom action',
"docustomaction",
['RecordID' => $record->ID]
)->addExtraClass(
'action-menu--handled'
);
}
public function getExtraData($gridField, $record, $columnName)
{
$field = $this->getCustomAction($gridField, $record);
if (!$field) {
return;
}
return $field->getAttributes();
}
public function getGroup($gridField, $record, $columnName)
{
return GridField_ActionMenuItem::DEFAULT_GROUP;
}
public function augmentColumns($gridField, &$columns)
{
if (!in_array('Actions', $columns)) {
$columns[] = 'Actions';
}
}
public function getColumnAttributes($gridField, $record, $columnName)
{
return ['class' => 'grid-field__col-compact'];
}
public function getColumnMetadata($gridField, $columnName)
{
if ($columnName === 'Actions') {
return ['title' => ''];
}
}
public function getColumnsHandled($gridField)
{
return ['Actions'];
}
public function getColumnContent($gridField, $record, $columnName)
{
$field = $this->getCustomAction($gridField, $record);
if (!$field) {
return;
}
return $field->Field();
}
public function getActions($gridField)
{
return ['docustomaction'];
}
public function handleAction(GridField $gridField, $actionName, $arguments, $data)
{
if ($actionName !== 'docustomaction') {
return;
}
// perform your action here
// output a success message to the user
Controller::curr()->getResponse()->setStatusCode(
200,
'Do Custom Action Done.'
);
}
}
I am struggling to add the custom icon for the button or specify the style class of the button. I can change the column class name. But I cannot find a way for the button. How can I do that?

you can achieve this using ->setAttribute('classNames', 'font-icon-<your-icon>');
i.e. for "edit" icon the code would looks like this:
return GridField_FormAction::create(
$gridField,
'CustomAction'.$record->ID,
'Custom action',
"docustomaction",
[
'RecordID' => $record->ID
]
)
->addExtraClass('action-menu--handled')
->setAttribute('classNames', 'font-icon-edit');
You can find all available icons on this page:
https://gbaumeister.github.io/ss4-icons/

Related

Widget Elementor that adds a Div to body using hook

Is there a way to add a div to body when the Widget is rendered? Like using a hook 'wp_body_open' or something.
Where can I put a hook in Widget_Base extended class for it?
I'm using a common Class like:
use Elementor\\Widget_Base;
use Elementor\\Controls_Manager;
class MenuHamburguer extends Widget_Base
{
public function __construct($data = [], $args = null)
{
parent::__construct($data, $args);
// I've tried here, but no success.
}
public function get_name()
{
return 'menu-hamburguer';
}
public function get_title()
{
return __('Menu Hamburguer', 'later');
}
public function get_icon()
{
return 'fa fa-bars';
}
public function get_categories()
{
return ['later'];
}
protected function _register_controls()
{
// do normal stuff of the widget...
}
protected function render()
{
// do normal stuff of the widget...
}
}

Wordpress seems to not execute function in add_action

I just start with Wordpress plugin and I try some codes to understand Wordpress plugin.
I try to display a custom message when I activate and deactivate my plugin.
The message for activation works very well but I have no message during deactivation.
Transient value changes but no message when I deactivate the plugin.
I thought add_action sets a function to be executed on every admin action on the plugin.... Am I wrong?
Can you help me? Thanks
class DuplicatePostPlugin
{
const DUPLICATE_POST_ACTIVATED = 'duplicate_post_activated';
public function __construct(string $file) {
register_activation_hook($file, [$this, 'plugin_activation_callback']);
register_deactivation_hook($file, [$this, 'plugin_deactivation_callback']);
add_action('admin_notices', [$this, 'notice_activation_callback']);
}
public function plugin_activation_callback() : void {
set_transient(self::DUPLICATE_POST_ACTIVATED, 1);
}
public function plugin_deactivation_callback() : void {
set_transient(self::DUPLICATE_POST_ACTIVATED, 0);
//do_action('admin_notices');
}
public function notice_activation_callback() : void {
if (get_transient(self::DUPLICATE_POST_ACTIVATED) == 1) {
self::render('notices', [ 'message' => "Activation OK"]);
} else if (get_transient(self::DUPLICATE_POST_ACTIVATED) == 0){
self::render('notices', [ 'message' => "Deactivation OK"]);
}
//delete_transient(self::DUPLICATE_POST_ACTIVATED);
}
public static function render(string $name, array $args = []) : void {
extract($args);
$file = DUPLICATE_POST_PLUGIN_DIR . "views/$name.php";
ob_start();
include_once($file);
echo ob_get_clean();
}

Persisiting Many-to-Many with extra field in sonata admin

Multimedia, Gallery and GalleryMultimedia.
Both Multimedia and Gallery have a One-to-Many relationship with GalleryMultimedia which holds multimedia_id, gallery_id and position.
In my MultimediaAdmin, I added list of Galleries as below:
->with('Thematic Galleries')
->add('gallery', 'entity', array(
'class' => 'ACME\MyBundle\Entity\Gallery',
'property' => 'name',
'multiple' => true,
'expanded' => true,
'mapped' => false,))
->end()
Now I am stuck at persisiting the selected Gallery as a GalleryMultimedia object. In my Multimedia Model, I have the function below which I would love to pass the GalleryMultimedia object for persisiting but just can't figure out how.
public function setGalleries($galleries)
{
if (count($galleries) > 0) {
foreach ($galleries as $gallery)
{
$this->addGallery($gallery);
}
}
return $this;
}
Out of desparation, I added the following code in my MultimediaAdmin.php
public function prePersist($multimedia)
{
$this->preUpdate($multimedia);
}
public function preUpdate($multimedia)
{
$multimedia->setFiles($multimedia->getFiles());
$this->saveGalleries($multimedia);
}
public function saveGalleries($multimedia)
{
$galleryies = $this->getForm()->get('gallery')->getData();
$container = $this->getConfigurationPool()->getContainer();
$em = $container->get('doctrine')->getManager();
$existing_arr = array();
$existing = $em->getRepository('ACMEMyBundle:GalleryMultimedia')->findBy(array('multimedia' => $multimedia));
$gals = array();
foreach($existing as $exist)
{
$existing_arr[] = $exist->getGallery()->getId();
}
foreach($galleryies as $gallery)
{
if(in_array($gallery->getId(),$existing_arr))
{
continue;
}
else
{
$gm = new \ACME\MyBundle\Entity\GalleryMultimedia();
$gm->setGallery($gallery);
$gals[] = $gm;
$multimedia->setGalleries($gals);
}
}
}
Can someone please assist my poor soul?
I solved this by making the persisting in postPersist like this
public function postPersist($multimedia)
{
$this->postUpdate($multimedia);
}
public function postUpdate($multimedia)
{
$this->saveGalleries($multimedia);
}
Hope this will help someone out there
My way of doing it with objects:
public function preUpdate($object)
{
$this->updateCountries($object);
}
public function updateCountries(\AppBundle\Entity\Product $object){
$container = $this->getConfigurationPool()->getContainer();
$em = $container->get('doctrine')->getManager();
$form_countries = $this->getForm()->get('Country')->getData();
//form_countries is a Doctrine\Common\Collections\ArrayCollection
$object_countries = $object->getCountries();
//object_countries is a collection of \AppBundle\Entity\CountryProduct
$list = array();
foreach($object_countries as $country){
if(!$form_countries->contains($country->getCountry())){
//remove objects not in the form
$em->remove($country);
} else {
//save the others to not re add them
$list[] = $country->getCountry();
}
}
foreach($form_countries as $country){
if(!in_array($country, $list)){
//add everyone in the form but not in the list
$countryProduct = new \AppBundle\Entity\CountryProduct();
$countryProduct->setProduct($object);
$countryProduct->setCountry($country);
$countryProduct->setValuation(1);
$em->persist($countryProduct);
}
}
$em->flush();
}

Adding a button to the CMS in SilverStripe

How do I add a button to the backend of the CMS that fires an action? I can display the button where I want using:
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldsToTab("Root.ButtonTest", array(
FormAction::create('doAction', 'Action button')
)
);
return $fields;
}
public function doAction()
{
//Do something
}
However the button added does nothing when clicked.
I've seen one example of how to put a button on the main action bar (next to save/publish) but that's not what I'm trying to do.
Looking at the only page of documentation I can find, do I need to do something within:
public function getCMSActions()
{
$actions = parent::getCMSActions();
//Something here?
}
It isn't very clear how to create the action that the button calls.
You'll have to extend/decorate LeftAndMain with your own extension and the action you want to call. Here's an example:
<?php
class MyExtension extends LeftAndMainExtension
{
private static $allowed_actions = array(
'doAction'
);
public function doAction($data, $form){
$className = $this->owner->stat('tree_class');
$SQL_id = Convert::raw2sql($data['ID']);
$record = DataObject::get_by_id($className, $SQL_id);
if(!$record || !$record->ID){
throw new SS_HTTPResponse_Exception(
"Bad record ID #" . (int)$data['ID'], 404);
}
// at this point you have a $record,
// which is your page you can work with!
// this generates a message that will show up in the CMS
$this->owner->response->addHeader(
'X-Status',
rawurlencode('Success message!')
);
return $this->owner->getResponseNegotiator()
->respond($this->owner->request);
}
}
Once you have written an extension like this, you'll have to apply it to LeftAndMain by adding the following to your mysite/_config/config.yml:
LeftAndMain:
extensions:
- MyExtension
That's it. Your doAction button should now actually do something!
Not sure if this is helpful, but here's how you can add action-buttons to a ModelAdmin.
(does reload the page)
...in the admin class:
public function getEditForm($id = null, $fields = null)
{
$form = parent::getEditForm($id, $fields);
$form
->Fields()
->fieldByName($this->sanitiseClassName($this->modelClass))
->getConfig()
->getComponentByType('GridFieldDetailForm')
->setItemRequestClass('MyGridFieldDetailForm_ItemRequest');
return $form;
}
MyGridFieldDetailForm_ItemRequest.php
class MyGridFieldDetailForm_ItemRequest extends GridFieldDetailForm_ItemRequest
{
function ItemEditForm()
{
$form = parent::ItemEditForm();
$formActions = $form->Actions();
$button = FormAction::create('myAction');
$button->setTitle('button label');
$button->addExtraClass('ss-ui-action-constructive');
$formActions->push($button);
$form->setActions($formActions);
return $form;
}
public function myAction(){ //do things }
}

Symfony2 + Twig: Translate label into a new twig extension

I have implemented a new twig extension and I have some text which had to be translated.
Unfortunately when I use a code label it appears as a sample text.
I mean when twig render this following extension, it displays: 5 entity.years instead of 5 years for example:
class MyExtension extends \Twig_Extension {
public function getFilters()
{
return array(
'myextension' => new \Twig_Filter_Method($this, 'myextension'),
);
}
public function myextension ($myId)
{
// ....
// Some operations concerning $myId...
// ....
if($myId!=0) {
$res = $myId. ' '.'entity.year';
} else {
$res = ($months == 0 ? $days.'entity.days' : $months.'entity.months');
}
return $res;
}
}
Where entity.years, entity.months, entity.days is defined into my translations folder.
Inject the translator service into your extension and use it. For example:
class MyExtension extends \Twig_Extension
{
private $translator;
public function __construct(Translator $translator)
{
$this->translator = $translator;
}
// ...
public function myMethod()
{
return $this->translator->trans('my_string');
}
}

Resources