I want to create a custom form element for email that have a textfield and button and when click button send an email but ajax not working inside form elemen class. I test it in form api and it's ok but when move in form element on button click nothing happend this is my code
<?php
/**
* Created by IntelliJ IDEA.
* User: peter
* Date: 24/12/18
* Time: 13:59
*/
namespace Drupal\aqr_basic\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\FormElement;
/**
* Provides a form element to display a email.
*
*
* @FormElement("aqr_email")
*/
class EmailElement extends FormElement {
public function getInfo() {
$class = get_class($this);
return [
'#pre_render' => [
[$class, 'preRenderSelect'],
],
];
}
public static function preRenderSelect($element) {
dump($element);
$email = [
'#type' => 'email',
'#title' => $element['#emailTitle'],
];
$email = array_merge($email,$element['email']);
$btnSendCode = [
'#type' => 'ajax_button',
'#value' => $element['#btnSendCodeTitle'],
'#ajax' => [ //////this ajax not work
'callback' => '::email',
'event' => 'click',
],
];
$btnSendCode = array_merge($btnSendCode,$element['btnSendCode']);
$verifyGroup = [
'#type' => 'fieldgroup',
'#attributes' => ['class' => ['hidden']],
];
$verifyGroup['emailCode'] = [
'#type' => 'textfield',
'#title' => $element['#emailCodeTitle'],
];
$verifyGroup['emailCode'] = array_merge($verifyGroup['emailCode'],$element['verifyGroup']['emailCode']);
$verifyGroup['btnVerifyCode'] = [
'#type' => 'ajax_button',
'#value' => $element['#btnVerifyCodeTitle'],
'#suffix' => '<div class="verification-desc email_verify">' . $element['#description'] . '</div>',
];
$verifyGroup['btnVerifyCode'] = array_merge($verifyGroup['btnVerifyCode'],$element['verifyGroup']['btnVerifyCode']);
$element = [
// '#attached'=>[
// 'library' => ['aqr_basic/state'],
// 'drupalSettings' => [
// 'id' => $element['#id'],
// 'cities' => json_encode($cities),
// ],
// ],
'email' => $email,
'btnSendCode' => $btnSendCode,
'verifyGroup' => $verifyGroup,
'#attributes' => ['id' => ['email-wrapper-id'], 'class' => ['email_verify']],
];
dump($element);
return $element;
}
/**
* {@inheritdoc}
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
return $input;
}
public static function email(array &$form, FormStateInterface $form_state) {
dump($form);
return $form;
}
}