View source
<?php
namespace Drupal\mailing_list\Form;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
class SubscriptionForm extends ContentEntityForm {
protected $message;
protected $customId;
protected $destination;
public function buildForm(array $form, FormStateInterface $form_state) {
$subscription = $this->entity;
$mailing_list = $subscription
->getList();
$form = parent::buildForm($form, $form_state);
if ($subscription
->isNew() && $mailing_list) {
$t_args = [
'%name' => $mailing_list
->label(),
];
$form['#title'] = $this
->currentUser()
->hasPermission('administer mailing list subscriptions') ? $this
->t('Add subscription to %name mailing list', $t_args) : $this
->t('Subscribe to %name mailing list', $t_args);
}
$message_text = $this->message ?: $mailing_list
->getHelp();
if (!empty($message_text) && $this->message != '<none>') {
$form['message'] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $message_text,
'#weight' => -10,
];
}
if ($this
->currentUser()
->hasPermission('administer mailing list subscriptions')) {
$form['advanced'] = [
'#type' => 'vertical_tabs',
'#weight' => 99,
];
$form['subscription_authoring'] = [
'#type' => 'details',
'#title' => $this
->t('Subscription authoring'),
'#open' => TRUE,
'#group' => 'advanced',
];
$form['uid']['#group'] = 'subscription_authoring';
$form['created']['#group'] = 'subscription_authoring';
$form['subscription_status'] = [
'#type' => 'details',
'#title' => $this
->t('Subscription status'),
'#open' => TRUE,
'#group' => 'advanced',
];
$form['status']['#group'] = 'subscription_status';
}
return $form;
}
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$list = $entity
->getList();
$status = parent::save($form, $form_state);
switch ($status) {
case SAVED_NEW:
if ($message = $list
->getOnSubscriptionMessage()) {
drupal_set_message($message);
}
elseif ($this
->currentUser()
->id() == $entity
->getOwnerId()) {
drupal_set_message($this
->t('Your subscription %label to the %list mailing list has been processed. Check your email for further details.', [
'%label' => $entity
->label(),
'%list' => $list
->label(),
]));
}
else {
drupal_set_message($this
->t('Subscription %label to the %list mailing list has been processed.', [
'%label' => $entity
->label(),
'%list' => $list
->label(),
]));
}
break;
default:
drupal_set_message($this
->t('Subscription %label to the %list mailing list has been updated.', [
'%label' => $entity
->label(),
'%list' => $list
->label(),
]));
}
$url = $entity
->toUrl($this->destination ?: 'form-destination');
$form_state
->setRedirect($url
->getRouteName(), $url
->getRouteParameters());
}
public function setMessage($message) {
$this->message = $message;
}
public function setCustomId($custom_id) {
$this->customId = $custom_id;
}
public function setFormDestination($destination) {
$this->destination = $destination;
}
public function getFormId() {
if (empty($this->customId)) {
return parent::getFormId();
}
$form_id = 'mailing_list_subscription_' . $this->entity
->bundle() . '_' . $this->customId;
if ($this->operation != 'default') {
$form_id = $form_id . '_' . $this->operation;
}
$form_id = trim(preg_replace('/_+/', '_', $form_id), '_');
return $form_id . '_form';
}
}