Drupal Views Ajax Block Validation Message - php

I have a views block (Views 3 / Drupal 7) with a exposed filter form and the ajax mode is enabled. It works fine. I added in hook_form_alter() a validation function. It works too, but the form_set_error message shows only on a page refresh. How can I set it up the message without page reload?
function hook_form_alter(&$form, &$form_state, $form_id) {
if($form['#id'] === 'id_from_views') {
array_unshift($form['#validate'], '_custom_form_validate');
}
}
function _custom_form_validate($form, &$form_state) {
if(!empty($form_state['values']['field'])) {
form_set_error('field', t('Custom error message.'));
}
}

I had a similar problem. The answer is to use ajax callback in the hook_form_alter.
function hook_form_alter(&$form, &$form_state, $form_id) {
if($form['#id'] === 'id_from_views') {
$form['submit']['#ajax'] = array('callback' => '_custom_form_validate');
}
}

Related

It is possible to alter the $form_state['redirect'] of a previous submit callback?

function module_form_alter(&$form, $form_state, $form_id) {
if($form_id == "user_login"){
$form['#submit'][] = 'module_userlogin_callback';
}
}
function module_userlogin_callback($form, $form_state){
global $base_url;
$current_path = $base_url . '/' .'about';
$form_state['redirect']=$current_path;
}
Thing is, the original submit callback user_login_submit has already a $form_state['redirect'] to user profile page and i want to alter that and redirect to about page with out overriding the original submit callback function.is it possible?currently $form_state['redirect'] given by me is not working.how can i unset it and redirect to about page?
I had similar issue but I fixed it by writing my form_alter function in template.php, like this:
your_theme_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'user_login') {
$form['#submit'][] = 'your_theme_userlogin_callback';
}
}
function your_theme_userlogin_callback(&$form, &$form_state) {
$form_state['redirect'] = '$url';//your redirect url path
}

hook_form_alter to unset fields according to a user's role

I am facing a problem when I try to use hook_form_alter to hide fields according to user's roles. I have used unset() and removed the field from the $form array, but it is still showing when the form is rendered.
Here is my code:
function mymodule_form_alter($form, $form_state, $form_id){
global $user;
if($form_id == 'my_content_type'){
if(array_key_exists(5,$user->roles) && !array_key_exists(3,$user->roles)){
if(empty($form_state['field']['args'][0]->title)){
unset($form['field_body']);
}
}
}
}
Instead of using unset() to hide a form element, you should set the #access property to FALSE. This keeps the form build tree intact, which avoids problems if other modules try to access or alter that information. Source
function MYMODULE_form_alter($form, $form_state, $form_id) {
global $user;
$account = $user;
if ($form_id == 'MYCONTENTTYPE_node_form') {
if (user_has_role(5, $account) && !user_has_role(3, $account)) {
if (empty($form_state['field']['args'][0]->title)) {
$form['field_body']['#access'] = FALSE;
}
}
}
}
If this is still not working, double-check your if-requests. Are they really doing something? Are you currently logged-in as a corresponding user?
I have found the solution. I need just added the & with $form and $form_state in hook_form_alter parameters. Like hook_form_alter(&$form, &$form_state, $form_id)
It exists a module which allows to hide fields. For Drupal 9 it handles per-roles, hide-or-disable, exceptions (such as not at creation…).
Look at: https://www.drupal.org/project/jammer

How to hide Fivestar field when editing a comment from another user?

My comments on articles have a required Fivestar rating field called 'Stars' and I hid it with the following custom module (see: https://drupal.stackexchange.com/questions/90629/how-to-hide-rating-field-when-adding-comment-to-own-node):
function hiderating_form_alter(&$form, &$form_state, $form_id) {
global $user;
if ($form_id == "comment_node_article_form") {
if ($form['#node']->uid == $user->uid) {
unset($form['field_stars']);
}
}
}
As an administrator, I've permission to edit comments from other users. Suppose that a user commented on his own article. That means he didn't have to set the 'Stars' field, due to the code above. But when I try to edit that comment, I do have to select a value for the 'Stars'.
How can I prevent this? It's sufficient to check that the uid from the user who wrote the comment differs from the uid from the user who edits the comment. Finally, mark that the obligation to select stars when I leave a new comment myself must be preserved!
Edit: I tried the following code:
function hiderating_form_alter(&$form, &$form_state, $form_id) {
global $user;
$comment->uid = $form_state['values']['uid'];
if ($form_id == "comment_node_article_form") {
if ($comment->uid != $user->uid) {
unset($form['field_stars']);
}
}
}
Apparently, $form_state['values'] isn't well defined, because I get the following error:
"Notice: Undefined index: values in hiderating_form_alter()".
What's the correct code?
The code in the edit doesn't work, because $form_state['values'] isn't present before the submit. This is the correct code:
function hiderating_form_alter(&$form, &$form_state, $form_id) {
global $user;
if ($form_id == "comment_node_article_form") {
if ($form['uid']['#value'] != $user->uid AND $form['uid']['#value'] != 0) {
unset($form['field_stars']);
}
}
}
Using dpm($form), I discovered that $form['uid']['#value'] returns the uid from the user who wrote the comment. The value is only different from 0 if a comment is edited. When a user writes a new comment, the uid from the form is 0. That's why the AND in the second if is necessary.

how to redirect contact form

How can I redirect the contact form in drupal 7 to a thank you page? There are modules to do this ini drupal 6 but not for drupal 7 and I cant find how to do this with rules, there is no option for a rule when submitting the contact form. Thank you.
Create your custom module with hook_form_alter implementation. Then type something like
$form_state['#redirect'] = 'path/to/redirect';
Hope this helps.
try something like this ,
<?php
function mymodule_form_alter(&$form, $form_state, $form_id){
if($form_id == 'user_register') {
$form['#submit'] []= 'mymodule_node_form_submit_handler';
}
}
function mymodule_node_form_submit_handler($form, &$form_state) {
unset($_REQUEST['destination']);
unset($form['#redirect']);
$form_state['redirect'] = 'http://google.com';
}
?>

Retrieve products in prestashop module and place module after check out page

I am writing a module, let's call it base, which will show a button after vistor has clicked on buy+confirmed. That is the page where total price is displayed, a thank you message and an email will be send.
On that page, I would like to add my module with the button, which on-click sends product details to another web service. Now I have several questions:
Which hook can I use to place that button on the confirm (after checkout) page. As you can see I am using several hooks just to see if the button appears. It only appears in the leftCollumn
What do you think in general of the code. Would getProducts() be the right method, just copied from another standard module. Do you have an example for me?
please ignore global. I will refactor later.
base.php (only important part of it)
<?php
if ( !defined( '_PS_VERSION_' ) )
exit;
class Base extends Module
{
public function install() {
return parent::install() && $this->registerHook('payment') && $this->registerHook('invoice') && $this->registerHook('leftColumn');
}
public function uninstall() {
parent::uninstall();
}
public function getContent() {
return '<h2>'.$this->displayName.'</h2> <div>nothing to configure</div>';
}
public function hookPayment($params) {
if (!$this->active)
return;
global $smarty;
$smarty->assign('buttonText', $this->l('Send to my base'));
return $this->display(__FILE__, 'base.tpl');
}
public function ajaxCall($params) {
if (Configuration::get('PS_CATALOG_MODE'))
return "return;";
return $params['cart']->getProducts(true);
}
}
products.php
include(dirname(__FILE__).'/../../config/config.inc.php');
include(dirname(__FILE__).'/../../init.php');
include(dirname(__FILE__).'/base.php');
$cart = new Cart((int)($cookie->id_cart));
$cart->id_lang = (int)($cookie->id_lang);
$base = new Base();
var_dump( $base->hookAjaxCall(array('cookie' => $cookie, 'cart' => $cart)) );
You can use this hooks :
{$HOOK_ORDER_CONFIRMATION}
{$HOOK_PAYMENT_RETURN}
This hooks are displayed in order-confimation.tpl after payment validation.

Categories