AuthUnlinkForm.php in Open Social 8.7
File
modules/custom/social_auth_extra/src/Form/AuthUnlinkForm.php
View source
<?php
namespace Drupal\social_auth_extra\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\user\Entity\User;
class AuthUnlinkForm extends ConfirmFormBase {
protected $socialNetwork;
public function getConfirmText() {
return $this
->t('Unlink');
}
public function getFormId() {
return $this->socialNetwork['id'] . '_auth_unlink_form';
}
public function getQuestion() {
return $this
->t('Unlink @network', [
'@network' => $this->socialNetwork['social_network'],
]);
}
public function getDescription() {
return $this
->t('If you unlink your @network account, you are no longer able to use @network for social log in.', [
'@network' => $this->socialNetwork['social_network'],
]);
}
public function getCancelUrl() {
return Url::fromRoute('entity.user.edit_form', [
'user' => $this
->currentUser()
->id(),
]);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$account = User::load($this
->currentUser()
->id());
$network_manager = \Drupal::service('plugin.network.manager');
$is_connected = FALSE;
foreach ($network_manager
->getDefinitions() as $definition) {
$user_manager = \Drupal::service($definition['id'] . '.user_manager');
$user_manager
->setAccount($account);
if ($definition['id'] !== $this->socialNetwork['id'] && $user_manager
->getAccountId()) {
$is_connected = TRUE;
break;
}
}
$user_manager = \Drupal::service($this->socialNetwork['id'] . '.user_manager');
$user_manager
->setAccount($account);
$user_manager
->setAccountId(NULL);
$account
->save();
if ($is_connected) {
drupal_set_message($this
->t('Your @network account is unlinked. You can still log in with your @community_name account.', [
'@network' => $this->socialNetwork['social_network'],
'@community_name' => \Drupal::config('system.site')
->get('name'),
]));
}
else {
drupal_set_message($this
->t('Your @network account is unlinked. Make sure you set a password or connect another social platform. Please enter a password to be able to continue using @community_name.', [
'@network' => $this->socialNetwork['social_network'],
'@community_name' => \Drupal::config('system.site')
->get('name'),
]), 'warning');
}
$form_state
->setRedirect('entity.user.edit_form', [
'user' => $this
->currentUser()
->id(),
]);
}
public function buildForm(array $form, FormStateInterface $form_state, $network = NULL) {
$network_manager = \Drupal::service('plugin.network.manager');
$definitions = $network_manager
->getDefinitions();
foreach ($definitions as $definition) {
$instance = $network_manager
->createInstance($definition['id']);
if ($network == $instance
->getSocialNetworkKey()) {
$this->socialNetwork = $definition;
break;
}
}
return parent::buildForm($form, $form_state);
}
}