OAuthDeleteConsumerForm.php in OAuth 1.0 8.2
File
src/Form/OAuthDeleteConsumerForm.php
View source
<?php
namespace Drupal\oauth\Form;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\user\UserDataInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OAuthDeleteConsumerForm extends ConfirmFormBase implements ContainerInjectionInterface {
const NAME = 'oauth_delete_consumer_form';
protected $account;
protected $user_data;
public static function create(ContainerInterface $container) {
$current_user = $container
->get('current_user');
$user_data = $container
->get('user.data');
return new static($current_user, $user_data);
}
public function __construct(AccountProxyInterface $account, UserDataInterface $user_data) {
$this->account = $account;
$this->user_data = $user_data;
}
public function getFormID() {
return static::NAME;
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete this OAuth consumer?');
}
public function getDescription() {
return $this
->t('This action cannot be undone.');
}
public function getCancelUrl() {
return new Url('oauth.user_consumer', [
'user' => \Drupal::currentUser()
->id(),
]);
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function getCancelText() {
return $this
->t('Cancel');
}
public function getFormName() {
return static::NAME;
}
public function buildForm(array $form, FormStateInterface $form_state, AccountInterface $user = NULL, $key = NULL) {
$form['key'] = array(
'#type' => 'hidden',
'#value' => $key,
);
$form['uid'] = array(
'#type' => 'hidden',
'#value' => $user
->id(),
);
$form = parent::buildForm($form, $form_state);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$key = $values['key'];
$uid = $values['uid'];
$this->user_data
->delete('oauth', $uid, $key);
$this
->messenger()
->addStatus($this
->t('OAuth consumer deleted.'));
Cache::invalidateTags([
'oauth:' . $uid,
]);
$form_state
->setRedirect('oauth.user_consumer', array(
'user' => $form_state
->getValue('uid'),
));
}
}