ajax 提交表单订阅 drupal8 中的 simpleNews

问题描述 投票:0回答:3

如何让 Ajax 在 SimpleNews 模块的订阅块中发送订阅?

我这样做了,但不起作用。

function simpleNewsAlter_simplenews_subscriptions_block_ico_subscription_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $form['system_messages'] = [
    '#markup' => '<div id="' . Html::getClass($form_id) . '-messages"></div>',
    '#weight' => -100,
  ];
  $form['actions']['subscribe']['#ajax']  = [
    'callback' => '\Drupal\simplenews\Form\SubscriptionsBlockForm::submitSubscribe',
    'event' => 'click',
    'progress' => [
      'type' => 'throbber',
    ],
  ];
}
drupal-8 hook-form-alter
3个回答
1
投票

下面的代码可以帮助我显示 Simplenews 模块的默认状态消息。在自定义模块的 .module 文件中包含以下代码。

use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Form\FormStateInterface;

function MODULENAME_form_FORM_ID_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['#prefix'] = '<div class="text-msg">';
  $form['#suffix'] = '<span class="simplenews-result-message"></span></div>';
  $form['status_messages'] = [
    '#type' => 'status_messages',
    '#weight' => -10,
  ];
  $form['actions']['subscribe']['#ajax'] = [
    'event' => 'click',
    'method' => 'replace',
    'effect' => 'fade',
    'disable-refocus' => FALSE,
    'callback' => 'MODULENAME_simplenews_submit',
    'progress' => [
      'type' => 'throbber',
    ],
    'options' => [
      'query' => [
        'ajax_form' => 1,
      ],
    ],
  ];
}

function MODULENAME_simplenews_submit(&$form, &$form_state) {
  $email = $form['mail']['widget'][0]['value']["#value"];
  $response = new AjaxResponse();
  if (!\Drupal::service('email.validator')->isValid($email)) {
    $msg = t("The email you entered is not valid");
    $response->addCommand(new HtmlCommand('.simplenews-result-message',
      $msg));
    return $response;
  }
}

0
投票

我尝试使用下面的代码,Ajax 提交工作正常。但是,我如何显示块内的错误消息?

use Drupal\Core\Form\FormStateInterface;
use Drupal\simplenews\Form\SubscriptionsFormBase;
use Drupal\simplenews\SubscriberInterface;
use Drupal\simplenews\Subscription\SubscriptionManager;

function MODULENAME_form_FORM_ID_alter(&$form, FormStateInterface $form_state, $form_id) {

    $form['message'] = [
      '#type' => 'markup',
      '#markup' => '<div id="result-message" class="result_message"></div>'
    ];

    $form['actions']['subscribe']['#ajax']  = [
        'callback' => '\Drupal\simplenews\Form\SubscriptionsFormBase::submitForm',
        //'callback' => array($this, '\Drupal\simplenews\Form\SubscriptionsBlockForm::submitSubscribe'),
        'event' => 'click',
        'method' => 'replace',
        'effect' => 'fade',
        'disable-refocus' => FALSE,
        'wrapper' => 'result-message', //Html::getClass($form_id) . '-messages',
        //'callback' => '\Drupal\custom\Form\FormController::setMessage',
        'progress' => [
          'type' => 'throbber',
        ],
        //'#attributes' => array('onclick' => 'return (false);'),
      ];
}

0
投票
use Drupal\Core\Ajax\HtmlCommand
    
function MODULENAME_form_FORM_ID_alter(&$form, FormStateInterface 
$form_state, $form_id) {
$form['#prefix'] = '<div class="text-msg">';
$form['#suffix'] = '<span class="simplenews-result-message"></span> 
</div>';      
$form['actions']['subscribe']['#ajax']  = [
'event' => 'click',
'method' => 'replace',
'effect' => 'fade',
'disable-refocus' => FALSE,
'callback' => 'custom_inscription_simplenews_submit',
'progress' => [
  'type' => 'throbber',
],
'options' => ['query' => ['ajax_form' => 1]],
];

// Rebuild the form
$form_state->setRebuild(TRUE);

}

function MODULENAME_simplenews_submit(&$form, &$form_state) {
$email = $form['mail']['widget'][0]['value']["#value"];
if (!\Drupal::service('email.validator')->isValid($email)) {
   $msg = t("The email you entered is not valid");
   $response = new AjaxResponse();
   $response->addCommand(new HtmlCommand('.simplenews-result-message', 
$msg));
}else{
   $msg = t("Thank you for subscribing!");
   $response = new AjaxResponse();
   $response->addCommand(new HtmlCommand('.simplenews-result-message', 
$msg)); 
}
return $response; 
}
© www.soinside.com 2019 - 2024. All rights reserved.