Drupal: Using #ajax on a form

×

Error message

  • Deprecated function: Return type of DatabaseStatementBase::execute($args = [], $options = []) should either be compatible with PDOStatement::execute(?array $params = null): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2244 of /var/www/html/drupaldeveloper.in/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /var/www/html/drupaldeveloper.in/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /var/www/html/drupaldeveloper.in/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /var/www/html/drupaldeveloper.in/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /var/www/html/drupaldeveloper.in/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /var/www/html/drupaldeveloper.in/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseCondition::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 1652 of /var/www/html/drupaldeveloper.in/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property MergeQuery::$condition is deprecated in MergeQuery->__construct() (line 1344 of /var/www/html/drupaldeveloper.in/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1865 of /var/www/html/drupaldeveloper.in/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1865 of /var/www/html/drupaldeveloper.in/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1865 of /var/www/html/drupaldeveloper.in/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1865 of /var/www/html/drupaldeveloper.in/includes/database/query.inc).

This CodeLet will ajaxify the respective element and will trigger an event. Respective action will be taken by the code and displayed on the form within the specified wrapper

Drupal Ajax form Event
CodeLet: 

<?php
/**
* Implementation of hook_form_alter
*
* @param unknown $form
* @param unknown $form_state
* @param unknown $form_id
* @author DrupalD http://twtitter.com/DrupalD
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {
switch($form_id) {
case 'incident_form_node_form':
$form['client_er'] = array(
'#type' => 'item',
'#title' => t('Client\'s Email Recipients'),
'#description' => t('Once you select a client, you will see the email recipient for that client listed here'),
'#weight' => -49,
);

$form['field_client']['und']['#ajax'] = array(
'callback' => '_mymodule_recipitn_js',
'wrapper' => 'edit-client-er',
);
}
}

/**
* _mymodule_recipitn_js
*
* Return email recipint
* @author DrupalD http://twtitter.com/DrupalD
*/
function _mymodule_recipitn_js($form, $form_state){
$am__recipient = array();

//Get the selected client
$sn__client_nid = $form_state['triggering_element']['#value'];

//Find all the email recipints for that client
$am__recipient = _get_node_email_recipient($sn__client_nid);

if (!empty($am__recipient)) {
$form['client_er'] = array(
'#type' => 'item',
'#markup' => implode('', $am__recipient),
'#title' => t('Client\'s Email Recipients'),
'#description' => t('Here are all the email recipients from selected client.
If you do not add any new email recipints in #email_recipients field below, above details will be used for this incident', array('#email_recipients' => t('Email Recipients'))),
'#prefix' => '',
'#suffix' => '',
);

return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_replace("#edit-client-er", render($form['client_er'])),
),
);
}
else {
return t('No email recipients found for this client');
}
}

/**
* _get_node_email_recipient
*
* @param unknown $sn__client_nid
* @author DrupalD http://twtitter.com/DrupalD
*/
function _get_node_email_recipient($sn__client_nid) {
$result = db_select('field_data_field_email_recipients', 'fer')
->fields('fer', array('field_email_recipients_email'))
->condition('entity_id', $sn__client_nid, '=')
->execute();
while ($am__email_recipient = $result->fetchAssoc()) {
$am__recipient[] = $am__email_recipient['field_email_recipients_email'];
}
return $am__recipient;
}
?>

Compatibility: 
Is CodeLet useful?: 
Drupal: Using #ajax on a form | Drupal Developer

Error

The website encountered an unexpected error. Please try again later.