View source
<?php
namespace Drupal\social_group_request\Form;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Url;
use Drupal\grequest\Plugin\GroupContentEnabler\GroupMembershipRequest;
use Drupal\group\Entity\GroupContentInterface;
use Drupal\group\Entity\GroupInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class GroupRequestMembershipRejectForm extends FormBase {
protected $group;
protected $groupContent;
protected $redirectDestination;
protected $cacheTagsInvalidator;
protected $currentUser;
public function __construct(RedirectDestinationInterface $redirect_destination, CacheTagsInvalidatorInterface $cache_tags_invalidator, AccountInterface $current_user, TranslationInterface $string_translation) {
$this->redirectDestination = $redirect_destination;
$this->cacheTagsInvalidator = $cache_tags_invalidator;
$this->currentUser = $current_user;
$this
->setStringTranslation($string_translation);
}
public static function create(ContainerInterface $container) {
return new static($container
->get('redirect.destination'), $container
->get('cache_tags.invalidator'), $container
->get('current_user'), $container
->get('string_translation'));
}
public function getFormId() {
return 'grequest_group_request_membership_reject';
}
private function getCancelUrl() {
return Url::fromUserInput($this->redirectDestination
->get());
}
public function buildForm(array $form, FormStateInterface $form_state, GroupInterface $group = NULL, GroupContentInterface $group_content = NULL) {
$this->group = $group;
$this->groupContent = $group_content;
$form['#attributes']['class'][] = 'form--default';
$form['question'] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $this
->t('Are you sure you want to reject the membership request for @name?', [
'@name' => $group_content
->getEntity()
->getDisplayName(),
]),
'#weight' => 1,
'#prefix' => '<div class="card"><div class="card__block">',
'#suffix' => '</div></div>',
];
$form['actions']['#type'] = 'actions';
$form['actions']['cancel'] = [
'#type' => 'link',
'#title' => $this
->t('Cancel'),
'#attributes' => [
'class' => [
'button',
'button--flat',
'btn',
'btn-flat',
'waves-effect',
'waves-btn',
],
],
'#url' => $this
->getCancelUrl(),
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Yes'),
'#button_type' => 'primary',
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->groupContent
->set('grequest_status', GroupMembershipRequest::REQUEST_REJECTED)
->set('grequest_updated_by', $this->currentUser
->id());
$result = $this->groupContent
->save();
if ($result) {
$this
->messenger()
->addStatus($this
->t('Membership request rejected'));
}
else {
$this
->messenger()
->addError($this
->t('Error updating Request'));
}
$this->cacheTagsInvalidator
->invalidateTags([
'request-membership:' . $this->group
->id(),
]);
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}