View source
<?php
declare (strict_types=1);
namespace Drupal\og\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\og\OgAccessInterface;
use Drupal\og\OgMembershipInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class GroupSubscribeForm extends ContentEntityForm {
protected $ogAccess;
public function __construct(OgAccessInterface $og_access, EntityRepositoryInterface $entity_repository, ?EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, ?TimeInterface $time = NULL) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->ogAccess = $og_access;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('og.access'), $container
->get('entity.repository'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'));
}
public function getFormId() {
return 'og_subscribe_confirm_form';
}
public function getQuestion() {
$membership = $this->entity;
$group = $membership
->getGroup();
$label = $group
->access('view') ? $group
->label() : $this
->t('Private group');
$message = $this
->isStateActive() ? $this
->t('Are you sure you want to join the group %label?', [
'%label' => $label,
]) : $this
->t('Are you sure you want to request a subscription to the group %label?', [
'%label' => $label,
]);
return $message;
}
public function getConfirmText() {
return $this
->isStateActive() ? $this
->t('Join') : $this
->t('Request membership');
}
public function getCancelUrl() {
$group = $this->entity
->getGroup();
return $group
->access('view') ? $group
->toUrl() : new Url('<front>');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$state = $this
->isStateActive() ? OgMembershipInterface::STATE_ACTIVE : OgMembershipInterface::STATE_PENDING;
$membership = $this->entity;
$membership
->setState($state);
$form['#title'] = $this
->getQuestion();
$form['#attributes']['class'][] = 'confirmation';
$form['description'] = [
'#markup' => $this
->t('This action cannot be undone.'),
];
$form['confirm'] = [
'#type' => 'hidden',
'#value' => 1,
];
if (!isset($form['#theme'])) {
$form['#theme'] = 'confirm_form';
}
$form = parent::buildForm($form, $form_state);
if ($this
->isStateActive() && !empty($form[OgMembershipInterface::REQUEST_FIELD])) {
$form[OgMembershipInterface::REQUEST_FIELD]['#access'] = FALSE;
}
return $form;
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this
->getConfirmText();
$actions['cancel'] = [
'#type' => 'link',
'#title' => $this
->t('Cancel'),
'#attributes' => [
'class' => [
'button',
],
],
'#url' => $this
->getCancelUrl(),
'#cache' => [
'contexts' => [
'url.query_args:destination',
],
],
];
return $actions;
}
public function isStateActive() {
$membership = $this
->getEntity();
$group = $membership
->getGroup();
$user = $membership
->getOwner();
$skip_approval = $this->ogAccess
->userAccess($group, 'subscribe without approval', $user)
->isAllowed();
$state = $skip_approval ? OgMembershipInterface::STATE_ACTIVE : OgMembershipInterface::STATE_PENDING;
if (!$group
->access('view', $user) && $state === OgMembershipInterface::STATE_ACTIVE) {
$state = $this
->config('og.settings')
->get('deny_subscribe_without_approval') ? OgMembershipInterface::STATE_PENDING : OgMembershipInterface::STATE_ACTIVE;
}
return $state === OgMembershipInterface::STATE_ACTIVE;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$membership = $this
->getEntity();
$group = $membership
->getGroup();
$message = $membership
->isActive() ? $this
->t('You are now subscribed to the group.') : $this
->t('Your subscription request has been sent.');
$this
->messenger()
->addMessage($message);
$redirect = $group
->access('view') ? $group
->toUrl() : Url::fromRoute('<front>');
$form_state
->setRedirectUrl($redirect);
}
}