How to add Custom button and its functionality in Admin Silverstripe? - php

How to add Custom button and its functionality in Admin Silverstripe?
Please tell me solution.
Custom Button add only in one menu.

Like #wmk mentioned in the comments, you can just take the framework code for GridFieldPrintButton as a base and go from there. SilverStripe also have a basic tutorial for creating a custom ActionProvider.
Rather than rehash the tutorial here, I will provide you a very basic custom action provider that you can copy and extend to do what you need. While you don't note the exact result you are wanting from the button, I will provide just a very generic class.
This code is a stripped down version of the GridFieldPrintButton that #wmk mentioned. It supports both the button itself invoking the custom code as well as the URL.
I've noted in the code a reference that I have kept to "grid-print-button", this is to make your button sit nicely next to the print rather than likely sitting on another line (as it did in my testing on an older 3.1 site I built).
class GridFieldCustomButton implements GridField_HTMLProvider, GridField_ActionProvider, GridField_URLHandler {
protected $targetFragment;
protected $someCustomConstructData;
//TargetFragment is just for positioning control of the HTML fragment
//SomeCustomConstructData is just an example of providing some default options into your butotn
public function __construct($targetFragment = "after", $someCustomConstructData = null) {
$this->targetFragment = $targetFragment;
$this->someCustomConstructData = $someCustomConstructData;
}
//Generate the HTML fragment for the GridField
public function getHTMLFragments($gridField) {
$button = new GridField_FormAction(
$gridField,
'custom',
'Custom Action',
'custom',
null
);
return array(
//Note: "grid-print-button" is used here to match the styling of the buttons in ModelAdmin
$this->targetFragment => '<p class="grid-print-button">' . $button->Field() . '</p>',
);
}
public function getActions($gridField) {
return array('myCustomAction');
}
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
if($actionName == 'myCustomAction') {
return $this->handleMyCustomAction();
}
}
//For accessing the custom action from the URL
public function getURLHandlers($gridField) {
return array(
'myCustomAction' => 'handleMyCustomAction',
);
}
//Handle the custom action, for both the action button and the URL
public function handleMyCustomAction($gridField, $request = null) {
//Do your stuff here!
}
}
Continuing on from the discussion in the comments, you will need to modify your custom ModelAdmin to add new components to its GridField.
class MyCustomAdmin extends ModelAdmin
{
private static $managed_models = array(
'MyCustomObject'
);
private static $url_segment = 'custom-admin';
private static $menu_title = 'All Custom Objects';
public function getEditForm($ID = null, $Fields = null)
{
$form = parent::getEditForm($ID, $Fields);
$fields = $form->Fields();
$gridField = $fields->fieldByName('MyCustomObject');
$gridFieldConfig = $gridField->getConfig();
$gridFieldConfig->addComponent(new GridFieldCustomButton());
return $form;
}
}
Specifically, the line $gridFieldConfig->addComponent(new GridFieldCustomButton()) does the work, taking your custom button as I have shown above and added it to the ModelAdmin. You can also specify where it should go in the GridField too by providing "buttons-before-left" as the first argument in the GridFieldCustomButton constructor.
eg. $gridFieldConfig->addComponent(new GridFieldCustomButton("buttons-before-left"))
More information regarding GridField fragments can be found in the SilverStripe developer documentation.

Related

How to add sortable column for member list in Silverstripe admin?

I am struggling to add sort functionality on one of my member summary fields in admin.
I have extended the Silverstripe member class using:
class MyMemberExtension extends DataExtension
I have added a few fields to the default gridfield in admin:
private static $db = array(
'Organisation' => 'Varchar(100)'
);
private static $summary_fields = array(
'FirstName' => 'First Name',
'Surname' => 'Surname',
'Email' => 'Email',
'OrganisationName' => 'Organisation Name',
'LastVisited' => 'Last Visited',
'NumVisit' => 'Num Visits'
);
private static $casting = array(
'OrganisationName' => 'Varchar(100)'
);
public function getOrganisationName() {
return $this->owner->Organisation;
}
...and that all works nicely.
However, only the core fields like LastVisited are giving me sort arrows on the column headers.
I'm currently stuck as to how to implement the sort on my Organisation field. I tried adding :
public function getCMSFields()
{
$fields = parent::getCMSFields();
$grid = $fields->dataFieldByName('Organisation');
$gridConfig = $grid->getConfig();
$gridConfig->addComponent(new GridFieldSortableHeader());
return $fields;
}
public function getEditForm($id = null, $fields = null) {
$form=parent::getEditForm($id, $fields);
$model = singleton($this->modelClass);
// add sorting if we have a field for...
if (class_exists('GridFieldSortableRows')
&& $model->hasField('Organisation')
&& $gridField=$form->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass))) {
if($gridField instanceof GridField) {
$gridField->getConfig()->addComponent(new GridFieldSortableRows('Organisation'));
}
}
return $form;
}
...to my class, but I'm not convinced these are even being called, as even if I just return null from these two functions nothing changes.
I have found a few answers that deal with extensions to ModelAdmin, but not for the core Member list. Thanks!
First of all, I'm not sure why you chose to have a getter named OrganisationName, where you could just as well use Organisation directly? That being said, I think your question is valid and might apply to different scenarios and/or field-types.
The Form-field that is being used to edit members is the Members GridField within SecurityAdmin. Luckily, there's an extension hook (updateEditForm) to modify the form fields of SecurityAdmin.
So in order to modify the sorting of the Members GridField, create an Extension like the following:
<?php
class MemberAdminExtension extends Extension
{
public function updateEditForm(Form $form)
{
/** #var GridField $memberGridField */
if ($memberGridField = $form->Fields()->dataFieldByName('Members')) {
/** #var GridFieldSortableHeader $sortHeader */
if ($sortHeader = $memberGridField->getConfig()->getComponentByType('GridFieldSortableHeader')) {
// Map OrganisationName to the Organisation field
$sortHeader->setFieldSorting([
'OrganisationName' => 'Organisation'
]);
}
}
}
}
And apply the extension via config to SecurityAdmin:
# Within _config/config.yml
SecurityAdmin:
extensions:
- MemberAdminExtension
After a dev/build your Member table should be sortable by Organisation Name as well…

Silverstripe: Member Profile - Update details on Frontend

I'm trying to allow users to update their details from a frontend 'dashboard' type of page. I've got the form displaying, but when I click 'Update details', the form redirects to a blank page, and the fields haven't changed. Can someone point me in the right direction.
<?php
class MemberProfile extends Page {
private static $description = 'Page that displays the current user\'s profile';
function getCMSFields() {
$fields = parent::getCMSFields();
// we don't want the Content editor for this page type
$fields->removeFieldFromTab('Root.Main', 'Content');
return $fields;
}
}
class MemberProfile_Controller extends Page_Controller {
private static $allowed_actions = array(
'UpdateDetails'
);
public function UpdateDetails() {
$form = Form::create(
$this,
__FUNCTION__,
FieldList::create(
TextField::create('FirstName','First name:')
->setAttribute('value', Member::CurrentUser()->FirstName),
TextField::create('Surname','Last name:')
->setAttribute('value', Member::CurrentUser()->Surname)
),
FieldList::create(
FormAction::create('saveDetails','Update details')
)
);
return $form;
}
public function saveDetails($data,$form) {
$CurrentMember = Member::CurrentMember();
$CurrentMember->FirstName = $data['FirstName'];
$CurrentMember->Surname = $data['Surname'];
$form->saveInto($CurrentMember);
$CurrentMember->write();
return $this->redirectBack();
}
}
My answer would be a dev/build is needed to include the static allowed actions... that is normally what it is for me.
However my main answer would be to use this module for management of user profiles, it allows for many customisations and will save you alot of time....
https://github.com/silverstripe-australia/silverstripe-memberprofiles
"A new page type is added in the CMS called a "Member Profile Page". This allows you to create a page that allows users to register and/or manage their profile. Registration can be enabled or disabled in the "Behaviour" tab."

Custom Silverstripe Meta Field not saving

I created a Meta Title using the code below but it works for majority of my websites but one particular website will not save the the Meta Title so when I edit it shows my previously entered title, same code for all websites but one is not saving?
class Page extends SiteTree {
private static $db = array(
'MetaTitle' => 'Varchar(255)'
);
private static $has_one = array(
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main', TextField::create('MetaTitle')
->setRightTitle('Shown at the top of the browser window and used as the "linked text" by search engines.')
->addExtraClass('help')
, 'MetaDescription');
return $fields;
}
}
If you do not /dev/build after adding a new $db property, changes will not save properly.

Zend Framework 2: Extend ZfcUser with own fields

I am working on my first Zend Framework 2 Project. I needed a User Module and integrated ZfcUser for this. Because I have a slight difference in my User Table, I had to use my own User Entity and User Mapper. I created a new Module called ZfcUserExtension.
I then copied a lot of files from the original ZfcUSer Module like:
Entity/User.php
Entity/UserInterface.php
Factory/Entity/IndexControllerFactory.php
Factory/Mapper/UserHydratorFactory.php
Mapper/Exeption/ExceptionInterface
Mapper/Exeption/InvalidArgumentException.php
Mapper/Exeption/RuntimeException.php Mapper/HydratorInterface.php
Mapper/User.php Mapper/UserHydrator.php Mapper/UserHydrator.php
Mapper/UserInterface.php
In zfcuser.global.php I set the user_entity_class to use my own Entity.
'user_entity_class' => 'ZfcUserExtension\Entity\User',
In the module.config.php from the ZfcUserExtension I add the below to make sure that I use my own User Mapper and UserHydrator. The reason for that was that I use "id" as a Primary Key in my User table instead of "user_id", so I had to make sure that this gets overwritten as well.
<?php
return array(
'controllers' => array(
'factories' => array(
'ZfcUserExtension\Controller\Index' => function(Zend\Mvc \Controller\ControllerManager $cm) {
$sm = $cm->getServiceLocator();
return new \ZfcUserExtension\Controller\IndexController(
$sm->get("doctrine.entitymanager.orm_default")
);
}
),
),
'service_manager' => array(
'factories' => array(
'zfcuser_user_mapper' => function ($sm) {
$options = $sm->get('zfcuser_module_options');
$mapper = new \ZfcUserExtension\Mapper\User();
// No db adapter present add below line
$mapper->setDbAdapter($sm->get('zfcuser_zend_db_adapter'));
$entityClass = $options->getUserEntityClass();
// No entity prototype set add below line
$mapper->setEntityPrototype(new $entityClass);
$mapper->setHydrator($sm->get('zfcuser_user_hydrator'));
$mapper->setTableName($options->getTableName());
return $mapper;
},
// 'zfcuserextension_change_password_form' => 'ZfcUserExtension\Factory\Form\ChangePhoneFormFactory',
),
),
I finally got all this to work, till I now run into another problem. I want some additional fields for the User like Phone Number. How would I approach this? I know there are some ideas on the Internet, but I am mainly interested to know how I would actually offer the option to have a "Change Phone" Form. I have created a Form, similar to the "Change Password and "Change Email". I have then created a IndexController.php in my ZfcUSerExtension, again followed the set-up of the UserController from the ZfcUser Module
class IndexController extends AbstractActionController {
const ROUTE_LOGIN = 'zfcuser/login';
/**
* #var \Doctrine\ORM\EntityManager
*/
protected $em;
public function __construct(\Doctrine\ORM\EntityManager $em)
{
$this->em = $em;
}
/**
* #var Form
*/
protected $changeEmailForm;
public function indexAction() {
if (!$this->zfcUserAuthentication()->hasIdentity()) {
return $this->redirect()->toRoute(static::ROUTE_LOGIN);
}
return new ViewModel();
}
public function changephoneAction() {
// if the user isn't logged in, we can't change phone
if (!$this->zfcUserAuthentication()->hasIdentity()) {
return $this->redirect()->toRoute(static::ROUTE_LOGIN);
}
$form = $this->getChangePhoneForm();
$request = $this->getRequest();
$request->getPost()->set('PrevPhone', $this->getUserService()->getAuthService()->getIdentity()->getPrevPhone());
return array(
'status' => false,
'changePhoneForm' => $form,
);
$fm = $this->flashMessenger()->setNamespace('change-phone')->getMessages();
if (isset($fm[0])) {
$status = $fm[0];
} else {
$status = null;
}
$prg = $this->prg(static::ROUTE_LOGIN);
if ($prg instanceof Response) {
return $prg;
} elseif ($prg === false) {
return array(
'status' => $status,
'changePhoneForm' => $form,
);
}
$form->setData($prg);
if (!$form->isValid()) {
return array(
'status' => false,
'changePhoneForm' => $form,
);
}
$change = $this->getUserService()->changeEmail($prg);
if (!$change) {
$this->flashMessenger()->setNamespace('change-email')->addMessage(false);
return array(
'status' => false,
'changeEmailForm' => $form,
);
}
$this->flashMessenger()->setNamespace('change-email')->addMessage(true);
return $this->redirect()->toRoute(static::ROUTE_CHANGEEMAIL);
}
public function getChangePhoneForm()
{
$sl = $this->getServiceLocator();
$this->setChangePhoneForm($sl->get('zfcuserextension_change_phone_form'));
return $this->changePhoneForm;
}
public function setChangePhoneForm($changePhoneForm)
{
$this->changePhoneForm = $changePhoneForm;
return $this;
}
I now noticed that I will face a problem with the User Service Service/User.php. The Service offers a changePassword() and changeEmail() Method. I now thought that I need to copy this file into my own Modules. Am I right that if I extend the User Service from ZfcUser then the Methods changePassword() and changeEmail() will still be available, so I would delete it from the just copied file and just add changePhone()?
And if I am right with my thoughts, the User Service currently starts like this:
class User extends EventProvider implements ServiceManagerAwareInterface
How would I have to change it that I extend the original User Service? I hope somebody can help, I am still rather confused with all this. Thanky you very much in advance.
There are two possible methods:
Build custom classes extending ZfcUser's entity, form and input filter and add your custom fields. In the ZfcUser configuration change aliases or override factories to ensure your custom classes are instantiated rather than the built in ones.
If you are OK with having the custom profile fields stored and accessed separately from the ZfcUser user entity, check out my module on GitHub: LdcUserProfile. It provides a profile system for ZfcUser but also makes it easy to add your own custom profile fieldsets linked to a user.

Creating a more flexible view in Zend Framework

Given an html/javascript 'widget' which needs to have certain fields customized before use. For example, the css class ids need to be unique as the widget may appear more than once on the same page.
Let's say I want to keep the markup (js/html) of the widget stored as a template so that I can fill in the values that need to be customized during resuse.
I know that Zend Framework's views give you at least part of this functionality, but each view is generally associated with a particular controller. Given that this widget could be created from any controller, yes still needs to be able to access some properties stored in a controller (or model). Where should I put the widget markup and how then do I fill in the custom values?
Can I create a custom view that can be reused within the same page (appear more than once) as well as on other pages? If so, how do I set that up?
Sounds like you need a ViewHelper http://framework.zend.com/manual/en/zend.view.helpers.html. Create a custom helper that will fetch the data from a model and just simply output it. This way it won't depend on any controller, can be called in either the layout or in any view script. Example:
// views/helpers/Widget.php
class Zend_View_Helper_Widget extends Zend_View_Helper_Abstract
{
protected $_model = null;
protected $_view = null;
public function widget()
{
$data = $this->_getDataFromModel();
return $this->_view->partial('widget.phtml', array('data' => $data));
}
public function setView(Zend_View_Interface $view)
{
if($this->_view === null) {
$this->_view = $view;
}
return $this->_view;
}
protected function _getDataFromModel()
{
$this->_model = $this->_getModel();
return $this->_model->getDataForWidget();
}
protected function _getModel()
{
if($this->_model === null) {
$this->_model = new Model_Widget(); // or whatever it's called
}
return $this->_model;
}
The partial script:
// views/scripts/widget.phtml
<div class="widget-class"><?php echo $this->data; ?></div>
And when you need it in your views just call it like <?php echo $this->widget(); ?>
Note that I'm rendering the widget in a separate partial view script, just to avoid having html/css in the helper itself.
Hope this helps to get you started :)
Zend_View_Helper_Partial
Example:
<?php echo $this->partial('partial.phtml', array(
'css_id' => 'foobar')); ?>
To run this from any other module:
<?php echo $this->partial('partial.phtml', 'partials_module', array(
'css_id' => 'foobar')); ?>
In your partial view script (partial.html) you would then have access to $this->css_id.

Categories