GroupUnsubscribeConfirmForm.php in Organic groups 8
File
src/Form/GroupUnsubscribeConfirmForm.php
View source
<?php
declare (strict_types=1);
namespace Drupal\og\Form;
use Drupal\Core\Entity\ContentEntityDeleteForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class GroupUnsubscribeConfirmForm extends ContentEntityDeleteForm {
public function getFormId() {
return 'og_unsubscribe_confirm_form';
}
public function getQuestion() {
$membership = $this
->getEntity();
$group = $membership
->getGroup();
return $this
->t('Are you sure you want to unsubscribe from the group %label?', [
'%label' => $group
->label(),
]);
}
public function getConfirmText() {
return $this
->t('Unsubscribe');
}
public function getCancelUrl() {
$group = $this->entity
->getGroup();
return $group
->access('view') ? $group
->toUrl() : new Url('<front>');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$membership = $this
->getEntity();
$group = $membership
->getGroup();
$redirect = $group
->access('view') ? $group
->toUrl() : Url::fromRoute('<front>');
$form_state
->setRedirectUrl($redirect);
$membership
->delete();
$this
->messenger()
->addMessage($this
->t('You have unsubscribed from the group.'));
}
}