deactivated vuejs for simplenews pages = fixed unsubscribe bug

This commit is contained in:
2022-03-31 20:27:59 +02:00
parent 1f6d1398c2
commit 2b48c867ea
13 changed files with 295 additions and 30 deletions

View File

@@ -3,8 +3,11 @@
namespace Drupal\materio_simplenews\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\simplenews\Form\SubscriptionsFormBase;
// ! NOT USED ANY MORE
/**
* Configure simplenews subscriptions of the logged user.
*/
@@ -19,6 +22,27 @@ class MaterioSubscriptionsBlockForm extends SubscriptionsFormBase {
*/
public $message;
/**
* The newsletters available to select from.
*
* @var string[]
*/
protected $newsletterIds = [];
/**
* The default newsletters.
*
* @var string[]
*/
protected $defaultNewsletterIds = [];
/**
* Whether to show "Manage existing" link.
*
* @var bool
*/
protected $showManage = FALSE;
/**
* {@inheritdoc}
*/
@@ -34,23 +58,95 @@ class MaterioSubscriptionsBlockForm extends SubscriptionsFormBase {
*/
public function setUniqueId($id) {
$this->uniqueId = $id;
return $this;
}
/**
* Set message.
*
* @param string $message
* Message to use as description for the block.
*
* @return $this
*/
public function setMessage($message) {
$this->message = $message;
return $this;
}
/**
* Set the newsletters available to select from.
*
* @param string[] $newsletters
* Newsletter IDs available to select from.
* @param string[] $defaults
* (optional) Newsletter IDs selected by default.
*
* @return $this
*/
public function setNewsletterIds(array $newsletters, array $defaults = []) {
$visible = array_keys(simplenews_newsletter_get_visible());
// Exclude newsletters already subscribed.
$subscribed = $this->entity->getSubscribedNewsletterIds();
$this->newsletterIds = array_diff(array_intersect($newsletters, $visible), $subscribed);
$this->defaultNewsletterIds = array_diff(array_intersect($defaults, $visible), $subscribed);
return $this;
}
/**
* Returns the newsletters available to select from.
*
* @return string[]
* The newsletter IDs available to select from, as an indexed array.
*/
public function getNewsletterIds() {
return $this->newsletterIds;
}
/**
* Set whether to show "Manage existing" link.
*
* @param bool $show
* TRUE to show "Manage existing" link, FALSE to hide.
*
* @return $this
*/
public function setShowManage($show) {
$this->showManage = $show;
return $this;
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
// Hide subscription widget if only one newsletter available.
if (count($this->getNewsletters()) == 1) {
$this->getSubscriptionWidget($form_state)->setHidden();
$this->getSubscriptionWidget($form_state)->setAvailableNewsletterIds($this->newsletterIds);
if (!$form_state->getUserInput()) {
// Set defaults.
foreach ($this->defaultNewsletterIds as $newsletter_id) {
$this->entity->subscribe($newsletter_id, SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED, 'website');
}
}
$form = parent::form($form, $form_state);
$form['subscriptions']['widget']['#title'] = $this->t('Manage your newsletter subscriptions');
$form['subscriptions']['widget']['#description'] = $this->t('Select the newsletter(s) to which you want to subscribe.');
$hidden_default_ids = array_diff($this->defaultNewsletterIds, $this->getNewsletterIds());
$form['subscriptions']['widget']['#required'] = empty($hidden_default_ids);
$form['subscriptions']['widget']['#access'] = !empty($this->newsletterIds);
$form['message'] = array(
'#type' => 'item',
'#markup' => $this->message,
);
if (!$this->newsletterIds && !$this->defaultNewsletterIds) {
$this->message = $this->t('You are already subscribed');
}
if ($this->message) {
$form['message'] = [
'#type' => 'item',
'#markup' => $this->message,
];
}
unset($form['subscriptions']['widget']['#title']);
unset($form['subscriptions']['widget']['#description']);
@@ -63,41 +159,84 @@ class MaterioSubscriptionsBlockForm extends SubscriptionsFormBase {
unset($form['mail']['widget'][0]['value']['#title']);
unset($form['mail']['widget'][0]['value']['#description']);
$mailvalue = $form['mail']['widget'][0];
$test='test';
return $form;
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions[static::SUBMIT_UPDATE]['#value'] = $this->t('Update');
$actions['submit']['#value'] = $this->t('Subscribe');
if (!$this->newsletterIds && !$this->defaultNewsletterIds) {
$actions['submit']['#attributes']['disabled'] = TRUE;
}
if ($this->showManage) {
$user = \Drupal::currentUser();
$link = $user->isAuthenticated() ? Url::fromRoute('simplenews.newsletter_subscriptions_user', ['user' => $user->id()]) : Url::fromRoute('simplenews.newsletter_validate');
$actions['manage'] = [
'#title' => $this->t('Manage existing'),
'#type' => 'link',
'#url' => $link,
];
}
return $actions;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$mail = $form_state->getValue(['mail', 0, 'value']);
if ($this->entity->isNew() && $subscriber = Subscriber::loadByMail($mail)) {
$this->setEntity($subscriber);
}
parent::validateForm($form, $form_state);
$mail = $form_state->getValue(['mail', 0, 'value']);
// Cannot subscribe blocked users.
if (($user = user_load_by_mail($mail)) && $user->isBlocked()) {
$message = $this->t('The email address %mail belongs to a blocked user.', ['%mail' => $mail]);
$form_state->setErrorByName('mail', $message);
}
}
/**
* Submit callback that subscribes to selected newsletters.
*
* @param array $form
* The form structure.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
*/
public function submitExtra(array $form, FormStateInterface $form_state) {
/** @var \Drupal\simplenews\Subscription\SubscriptionManagerInterface $subscription_manager */
$subscription_manager = \Drupal::service('simplenews.subscription_manager');
// Subscribe the selected newsletters and any defaults that are hidden.
$selected_ids = $this->extractNewsletterIds($form_state, TRUE);
$hidden_default_ids = array_diff($this->defaultNewsletterIds, $this->getNewsletterIds());
foreach (array_unique(array_merge($selected_ids, $hidden_default_ids)) as $newsletter_id) {
$subscription_manager->subscribe($this->entity->getMail(), $newsletter_id, NULL, 'website');
}
$sent = $subscription_manager->sendConfirmations();
$this->messenger()->addMessage($this->getSubmitMessage($form_state, $sent));
}
/**
* {@inheritdoc}
*/
protected function getSubmitMessage(FormStateInterface $form_state, $op, $confirm) {
switch ($op) {
case static::SUBMIT_UPDATE:
return $this->t('The newsletter subscriptions for %mail have been updated.', array('%mail' => $form_state->getValue('mail')[0]['value']));
case static::SUBMIT_SUBSCRIBE:
if ($confirm) {
return $this->t('You will receive a confirmation e-mail shortly containing further instructions on how to complete your subscription.');
}
return $this->t('You have been subscribed.');
case static::SUBMIT_UNSUBSCRIBE:
if ($confirm) {
return $this->t('You will receive a confirmation e-mail shortly containing further instructions on how to cancel your subscription.');
}
return $this->t('You have been unsubscribed.');
protected function getSubmitMessage(FormStateInterface $form_state, $confirm) {
if ($confirm) {
return $this->t('You will receive a confirmation e-mail shortly containing further instructions on how to complete your subscription.');
}
return $this->t('You have been subscribed.');
}
}

View File

@@ -12,6 +12,8 @@ use Drupal\Core\Session\AccountInterface;
use Drupal\simplenews\Entity\Subscriber;
use Symfony\Component\DependencyInjection\ContainerInterface;
// ! NOT USED ANY MORE
/**
* Provides an 'Simplenews subscription' block with all available newsletters and an email field.
*