SubscriptionsAccountForm.php in Simplenews 8.2
File
src/Form/SubscriptionsAccountForm.php
View source
<?php
namespace Drupal\simplenews\Form;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\simplenews\Entity\Subscriber;
use Drupal\user\UserInterface;
class SubscriptionsAccountForm extends SubscriptionsFormBase {
public function buildForm(array $form, FormStateInterface $form_state, UserInterface $user = NULL) {
if (isset($user)) {
$form_state
->set('user', $user);
$this
->setEntity(Subscriber::loadByUid($user
->id(), 'create'));
}
return parent::buildForm($form, $form_state);
}
protected function getSubmitMessage(FormStateInterface $form_state, $op, $confirm) {
$user = $form_state
->get('user');
if (\Drupal::currentUser()
->id() == $user
->id()) {
return $this
->t('Your newsletter subscriptions have been updated.');
}
return $this
->t('The newsletter subscriptions for user %account have been updated.', [
'%account' => $user
->label(),
]);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$form_state
->setRedirectUrl($this->entity
->getUser()
->toUrl());
}
public function checkAccess(UserInterface $user) {
$account = $this
->currentUser();
if ($user
->isAnonymous()) {
return AccessResult::forbidden();
}
return AccessResult::allowedIfHasPermission($account, 'administer simplenews subscriptions')
->orIf(AccessResult::allowedIfHasPermission($account, 'subscribe to newsletters')
->andIf(AccessResult::allowedIf($user
->id() == $account
->id())));
}
}