for those missing betterbuttons

<%- if @topic_view.topic.tags.present? %>
<%= t 'js.tagging.tags' %>: <%- @topic_view.topic.tags.each do |t| %> <%= t %> <%- end %>
<% end %>

Hi all,

I recently released this module

It’s basically what’s missing from betterbuttons for SS4 + a few nice addition. I hope it can help others out there :slight_smile:

5 Likes

Hi there! I’m just getting started with Silverstripe, and I’m trying to accomplish something that I would consider rather “simple”, but for the life of me I can’t figure out how to do it. I believe your module can be of assistance, but I’m not sure what steps I need to take. Here is my “simple” task:

When editing a page in the CMS, there are two buttons at the bottom (Save and Publish). I’d like to have a third button down there that says “CLICK ME” and when you click the button it just displays a JavaScript alert of the name of the page you are editing. Obviously this functionality wouldn’t be very useful in the real world, but I’m trying to do it as a proof of concept and the learn how similar actions could be performed in the future.

I installed your module and I think I have it set up correctly, but I’m not sure where to go from there. I see in your code examples that I’m likely going to need something similar to this code:

public function getCMSActions()
{
    $actions = parent::getCMSActions();

    $actions->push(new CustomAction("doCustomAction", "CLICK ME"));

    return $actions;
}

public function doCustomAction() {
    return $Title;
}

The issue I’m facing, is I’m not sure where this code needs to go. I’ve read through the documentation at https://docs.silverstripe.org/en/4/developer_guides/customising_the_admin_interface/how_tos/extend_cms_interface/#extending-the-cms-actions and I was able to get the “Bookmark” example working, but I’m sort of confused beyond that. Would the custom action button code need to go into a new extension file? They list a bunch of code snippets, but don’t say where to put them. The github readme for the silverstripe-cms-actions module says “…use getCMSActions on your DataObjects…” but I don’t really understand what that means. Even if you reply back saying: “create a file called xyz in folder abc and paste in the following code” that would be extremely useful.

hi @derekairdrie,

yes maybe what isn’t clear in the readme is that this works from a ModelAdmin controller. I haven’t experimented a lot on DataObjects attached to pages for instance, but that would be very useful addition to my module. Maybe it works or need a little bit of finetuning, I really don’t know !

so, simple use case is this:

create a model admin with the models you want to manage

all actions declared in getCMSActions will appear as they should :slight_smile:

hi again @derekairdrie,

just gave it a quick run on a regular page. indeed, currently i wasn’t extending leftAndMain and custom actions would not execute properly

i just released an update of the module that should allow actions to run correctly on LeftAndMain controllers (and therefore, pages in the cms for instances).

Hi @lekoala,
Thank you for your response. I downloaded the update of the module. Unfortunately I’m still not sure how to get the button to appear. You said tot create a ModelAdmin, but even looking at the documentation I’m still not 100% sure what I need to do. I’m finding a lot of this somewhat overwhelming. I realize this is probably basic stuff, but after pouring through all the documentation on the website I feel like I’m still missing how everything ties together.

I apologize, but are you able to walk me through the steps for me to get my “example” task working? As in, what files I need to create and what the code should look like in them? Even if the button appears and doesn’t do anything I’m fine with that. I just need to get to a place where I can get something showing up on the screen.

As of now, the button should always appear. You should add your code on a page or on a data object. The button should appear. If not I would need some example to test why it’s not working.

Sorry, I’m probably still misunderstanding fundamentals here, but I’m not sure what you mean when you say “add your code on a page or on a data object”. Can you clarify? Right now I have a \app\src\ folder that I’m putting a new ButtonTest.php file into. The code I’ve put into ButtonTest.php is:

<?php

use SilverStripe\Admin\ModelAdmin;

class ButtonTest extends ModelAdmin
{
    public function getCMSActions()
	{
	    $actions = parent::getCMSActions();

	    $actions->push(new CustomAction("doCustomAction", "My custom action"));

	    return $actions;
	}

	public function doCustomAction() {
	    return 'Done!';
	}

}

I then visit \dev\build\ but when I visit \admin?flush=all it comes back with " LeftAndMain subclasses (ButtonTest) must have url_segment"

I tried adding this line to my php file to make the error go away:

private static $url_segment = 'buttonTest';

and now a new menu item is showing in the navigation on the left, but trying to edit a page still doesn’t show my custom button. I feel like I just don’t get the fundamentals of how the CMS works. Do I need to create additional .php files in the \app\src\ folder? Or update app.yml in the _config folder?

Hi @derekairdrie,

getCMSActions apply to DataObject’s. so you can edit your page for instance

class Page extends SiteTree {
public function getCMSActions()
{
$actions = parent::getCMSActions();

    $actions->push(new CustomAction("doCustomAction", "My custom action"));

    return $actions;
}

public function doCustomAction() {
    return 'Done!';
}

}

and you should see my custom action next to your save/publish buttons

in model admin, if you have a dataobject, you do

class MyDataObject extends DataObject{
public function getCMSActions()
{
$actions = parent::getCMSActions();

    $actions->push(new CustomAction("doCustomAction", "My custom action"));

    return $actions;
}

public function doCustomAction() {
    return 'Done!';
}

}

and you define the managed_models = [‘MyDataObject’] on the model admin => read the documentation about model admin if that’s unclear

Awesome, thanks! I was able to get the button to appear by editing Page.php in my app/src/ folder. Thanks again for your help.