Is it possible to fire a FormAction button from within CMSFields? I'm currently using the following code but the button click doesn't want to fire the form action. Does this need to be called through a custom entwine function or is there something already built in that I'm missing?
class NotesPage extends Page {
function getCMSFields() {
$fields = parent::getCMSFields();
// Page Sections
$fields->addFieldsToTab('Root.Notes', array(
// Send for approval
LiteralField::create('', '<h3>Notify Admin Of Content To Approve</h3>'),
FormAction::create('NotifyAdmin', 'Notify Admin'),
));
return $fields;
}
}
class NotesPageExtension extends LeftAndMainExtension {
private static $allowed_actions = array(
'NotifyAdmin'
);
// Email the admin about content to moderate
public function NotifyAdmin($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('Notifying admin')
);
return $this->owner->getResponseNegotiator()->respond($this->owner->request);
}
}
Related
I wanted to change the pages 'register user', 'change password' and 'user edit'.
This is not possible in the Drupal frontend, and you also have to pay a lot of attention in the code.
In forums you will usually find little help and if then rather not
satisfactory.
Therefore, I like to share my solution to save the one or other headache.
The three forms are already available from Drupal standard, if you want to change them you have to access the route and redirect.
For this you first create a module like 'my_forms'.
Second the form or a page that you want to hook into the website. e.g.
'ForgotPasswordForm'
Create a folder inside your my_forms 'Routing' and a file like 'RouteSubscriber.php'
code:
<?php
namespace Drupal\my_forms\Routing;
class RouteSubscriber extends RouteSubscriberBase{
protected function alterRoutes( RouteCollection $collection ){
//enter code here
}
}
Now you can change the route to the diffrent pages by adding following -
code:
//edit the 'forgot password' page
if ( $route = $collection->get( 'user.pass' ) ){
$route->setDefault( '_form', '\Drupal\my_forms\Form\ForgotPasswordForm' );
}
The keyword 'user.pass' allows you to change the routing. setDefault redirects the route to your own form.
//edit the 'register' page
if ( $route = $collection->get( 'user.register' ) )
{
$route->setDefaults( array(
'_title' => 'Register',
'_controller' => '\Drupal\myy_forms\Controller\LocalTaskController::offerRegistrationPage'
)
);
}
The keyword for the registration is 'user.register'. By setting the route with setDefaults you can also change the name of the link/button. In this case I used a controller to print out a normal page inside the registration. (more at the end of the post)
if ( $route = $collection->get( 'entity.user.edit_form' ) ){
//enter code here
}
For the 'user edit' page you need to use the keyword 'entity.user.edit_form'.
Last step is to clear your cashes, to register the changes in your Drupal website and refresh your page.
Load simple page with a Drupal Controller:
namespace Drupal\my_forms\Controller;
class LocalTaskController extends ControllerBase{
/** #var NodeStorage $nodeStorage */
protected $nodeStorage;
/** #var EntityViewBuilder $viewBuilder */
protected $viewBuilder;
function __construct( $nodeStorage, $viewBuilder )
{
$this->nodeStorage = $nodeStorage;
$this->viewBuilder = $viewBuilder;
}
public static function create( ContainerInterface $container )
{
$nodeStorage = $container->get( 'entity.manager' )->getStorage( 'node' );
$viewBuilder = $container->get( 'entity_type.manager' )->getViewBuilder( 'node' );
return new static( $nodeStorage, $viewBuilder );
}
public function offerRegistrationPage()
{
$node = $this->nodeStorage->load( 24 );
$renderArray = $this->viewBuilder->view( $node );
return [
'#type' => '#markup',
'#markup' => render( $renderArray ),
];
}
The '24' is the nid of the simple page, which should be displayed instead of the standard registration.
I have a application where after update user should be redirected to previous page from pagination.
let's say there is a gridview and user is at page 3. Then he update some record at that page. There should be a redirect to index page 3. What if, while user is updating record, before save, he opens another controller/action in new tab. Then ReturnUrl is now that new action and after save the record he is updating, he is redirected to that new url.
I've tried to set in every action "index" Url::remember(); and then in action "update" - return $this->goBack().
Also return $this->redirect(Yii::$app->request->referrer);, but it stays at same page.
There is a way to store every index URL in session, but in large project that means many sessions.
You could provide the returnUrl to the link, say:
Url::to(['update','id'=>$model->url,'returnUrl'=> Yii::$app->request->url]);
Then in your controller, use $this->request->queryParams['returnUrl'] to redirect to the previousUrl.
To take it one step further, to always provide the returnUrl, you could extend the Url Helper class:
namespace app\helpers;
use yii\helpers;
class Url extends yii\helpers\Url
public function toRouteAndReturn($route, array $params = [], $scheme = false) {
$params['returnUrl'] = Yii::$app->request->url;
return parent::toRoute($route,$params,$scheme);
}
You could provide in your main config:
'on afterAction' => function($event) {
if(!Yii::$app->getResponse()->isSent && !empty(Yii::$app->getRequest()->queryParams['returnUrl']) {
Yii::$app->getResponse()->redirect(Yii::$app->getRequest()->queryParams['returnUrl']);
}
}
Then you could use app\helpers\Url::toRouteAndReturn() instead of yii\helpers\Url::toRoute() to have it return to the previous url.
You can try below Solution.
First in your index page, get current page url and encode it.
$current_url=base64_encode(\Yii::$app->request->getUrl());
Append this url with your update link as below.
'urlCreator' => function ($action, $model, $key, $index) use ($current_url) {
if ($action === 'update') {
$url = Yii::$app->request->baseUrl . '/controllerName/update?id=' . $model->id.'&prev='.$current_url;
return $url;
}
// ......
}
In Controller, in Update method decode url as below and use for redirection.
public function actionUpdate($id)
{
$model = $this->findModel($id);
$prev=base64_decode($_REQUEST['prev']);
// ......
return $this->redirect($prev); // you will redirect from where update method is called
// ......
}
Isn't it quite easy to pass page param into your update url (<model/update>) like <model>/update?id=<id>&page=<page>?
in your index.php view, edit your ActionColumn as follow:
[
'class' => 'yii\grid\ActionColumn',
'urlCreator' => function ($action, $model, $key, $index) {
return \yii\helpers\Url::to([$action, 'id' => $model->id, 'page' => Yii::$app->request->getQueryParam('page', null)]);
},
],
As you can see, I'm getting page param from request url and pass it to models' action buttons
And when you click to update model, the page that we entered from is stored/placed in url.
Controller:
public function actionUpdate($id, $page = null)
{
$model = $this->findModel($id);
...
if($model->save()) {
return $this->redirect(['index', 'page' => $page]);
}
...
}
Finally, after we successfully update the model, the action redirects us to previous index page.
I'm using Zend Framework 1.12 on a project. I having some problems with the Zend_Form. Some fields are generated dynamically on execution time, but the Zend_Form is static, a element predefined at creation.
So, when the form is sent, the validation doesn't work because new fields were added and the sent form doesn't match the form created.
How do that adaptation?
You should try, following solution: after sending the form, get the $_POST array, then check which fields/values do you have and create/modify form Object with this fields/validation.
I would have done this way :
class MyForm extends Zend_Form
{
public function init()
{
//... Create here the basic elements
}
public function initFromPostValue( $post )
{
if( array_key_exists( 'dynamicsField', $post ) ) {
$el = $this->createElement( 'select', 'dynamicsField' )
->setValidators( array( ... PUT your validators here ) );
$this->addElement( $el );
}
}
}
In the validation action :
public function validationAction()
{
$form = new MyForm();
$form->initFromPostValue( $_POST );
if( $form->isValid( $_POST ) ) {
// Form is valid
} else {
// Form is invalid
}
}
I use the following code over and over in my zend framework application. It is used in action() to check if an article exists. If not, the user shall see an error message:
$article = ArticleQuery::create()->findOneByUrl($this->_getParam('url', ''));
if (!$article) {
$this->getResponse()
->setRawHeader('HTTP/1.1 404 Not Found');
return $this->_forward('error', null, null, array(
'message' => 'Article not found',
));
}
I was wondering how to factor this out into an own method to reduce the code load in all actions.
I came to something like this:
protected function myAction() {
$article = $this->getArticleIfExists($this->_getParam('url', ''));
if ($article == null) {
return;
}
}
protected function getArticleIfExists($url) {
$article = ArticleQuery::create()->findOneByUrl($this->_getParam('url', ''));
if ($article) {
return $article;
} else {
$this->getResponse()
->setRawHeader('HTTP/1.1 404 Not Found');
$this->_forward('error', null, null, array(
'message' => 'Article not found',
));
return nulL;
}
}
I still would like to get rid of the if case in myAction(), but _forward() does not allow to exit the execution (of course, because it still needs to execute the other actions).
Another possibility (I have implemented in some other controllers) is this:
protected function myAction() {
$article = ArticleQuery::create()->findOneByUrl($this->_getParam('url', ''));
if (!$article) {
return $this->notFound('Article does not exist');
}
}
protected function notFound($message) {
$this->getResponse()
->setRawHeader('HTTP/1.1 404 Not Found');
return $this->_forward('error', null, null, array(
'message' => $message,
));
}
Again we have this if check in the action. It’s already better than before, but can we make it even better?
How can I circumvent this? Is there a possibility to do it without losing the current URL? With a Redirector I can of course exit, but then I would lose the current URL (/controller/myaction/url/hello -> /error/error/message/Article%20not%20found)
A possible approach would be to throw an Exception. Because of the Zend_Controller_Plugin_ErrorHandler this will automatically redirect you to the ErrorController without any further code being executed.
If you don't want to get to the ErrorController but only to the current controller's error actions, you can simply modify the plugin in the controller's init method:
public function init()
{
$plugin = Zend_Controller_Front::getInstance()->getPlugin('Zend_Controller_Plugin_ErrorHandler');
$plugin->setErrorHandlerModule('default')
->setErrorHandlerController('MyController')
->setErrorHandlerAction('error');
}
But of course you can also write your own ErrorHandler plugin for a more fine grained error handling. This is described in the Zend Framework Manual on Plugins
For something as simple as just displaying a "* does not exist" against a user request I prefer to leave the user in the application and just hit them with a flashmessenger notice and leave them on the page to make another request (if appropriate):
public function indexAction() {
//get form and pass to view
$form = new Form();
$this->view->form = $form;
try {
//get form values from request object
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost())) {
$data = $form->getValues();
// Do some stuff
}
}
} catch (Zend_Exception $e) {
$this->_helper->flashMessenger->addMessage($e->getMessage());//add message to flashmessenger
$this->_redirect($this->getRequest()->getRequestUri());//perform redirect to originating page so the messenger will flash
}
}
This is simple and works well when the possibility for incorrect user input exists.
I have created this file at My/View/Helper/FormElement.php
<?php
abstract class My_View_Helper_FormElement extends Zend_View_Helper_FormElement
{
protected function _getInfo($name, $value = null, $attribs = null,
$options = null, $listsep = null
) {
$info = parent::_getInfo($name, $value, $attribs, $options, $listsep);
$info['id'] = 'My new ID';
return $info;
}
}
How can i get the normal form elements to use this instead?
Why i want this?
Say that i use the same form multiple times on a page, the 'id='-tag of the formelements will apear multiple times, this is not w3c-valid. So initially i want to prefix the id with the id of the form.
Any better ideas or ways to do this is much apreciated.
Update: Just realized it's the same problem with the decorators :( Don't think this is the right path i've taken.
Create new form class extending Zend_Form and in the init() method use variable $ns to add prefix/suffix to all elements. You can set $ns variable through form constructor.
class Form_Test extends Zend_Form
{
protected $ns;
public function init()
{
$this->setAttrib('id', $this->ns . 'testForm');
$name = new Zend_Form_Element_Text('name');
$name->setAttrib('id', $this->ns . 'name');
$name->setLabel('Name: *')->setRequired(true);
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', $this->ns . 'submitbutton');
$submit->setLabel('Add')->setIgnore(true);
$this->addElements(array($name, $submit));
}
public function setNs($data)
{
$this->ns = $data;
}
}
In the controller or wherever you are calling this forms specify each form instance:
$form1 = new Form_Test(array('ns' => 'prefix1'));
$this->view->form1 = $form1;
$form2 = new Form_Test(array('ns' => 'prefix2'));
$this->view->form2 = $form2;
// Validation if calling from the controller
if ($form1->isValid()) ...
Using multiple instances of same forms on a page can be validated if used as subform.
SubForms prefix all id's with the name/identifier of the subform.