【问题标题】:ajax submit forms subscribe simpleNews in drupal8ajax提交表单在drupal8中订阅simpleNews
【发布时间】:2018-03-01 12:13:49
【问题描述】:

如何让 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


【解决方案1】:

我尝试使用以下代码,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);'),
      ];
}

【讨论】:

  • 我尝试使用自定义表单提交来提交 AJAX simplenews。它运作良好。我希望它会帮助某人。财政年度:thirstysix.com/… .
【解决方案2】:
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; 
}

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-31
  • 2016-06-23
  • 2016-03-06
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
  • 2011-06-17
相关资源
最近更新 更多