SamlauthMappingDeleteForm.php in SAML Authentication 8.3
File
modules/samlauth_user_fields/src/Form/SamlauthMappingDeleteForm.php
View source
<?php
namespace Drupal\samlauth_user_fields\Form;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\samlauth_user_fields\EventSubscriber\UserFieldsEventSubscriber;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SamlauthMappingDeleteForm extends ConfirmFormBase {
protected $entityFieldManager;
protected $attributeName;
protected $fieldName;
public function __construct(EntityFieldManagerInterface $entity_field_manager) {
$this->entityFieldManager = $entity_field_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_field.manager'));
}
public function getFormId() {
return 'samlauth_user_fields_delete_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $mapping_id = NULL) {
if ($mapping_id !== NULL) {
$mappings = $this
->configFactory()
->get(UserFieldsEventSubscriber::CONFIG_OBJECT_NAME)
->get('field_mappings');
$this->attributeName = $mappings[$mapping_id]['attribute_name'];
$this->fieldName = $mappings[$mapping_id]['field_name'];
$form_state
->set('mapping_id', $mapping_id);
return parent::buildForm($form, $form_state);
}
return [];
}
public function getQuestion() {
$fields = $this->entityFieldManager
->getFieldDefinitions('user', 'user');
$field_name = $fields[$this->fieldName]
->getLabel();
return $this
->t('Are you sure you want to delete the mapping for %attribute > %field?', [
'%attribute' => $this->attributeName,
'%field' => $field_name,
]);
}
public function getCancelUrl() {
return new Url('samlauth_user_fields.list');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->configFactory()
->getEditable(UserFieldsEventSubscriber::CONFIG_OBJECT_NAME);
$mappings = $config
->get('field_mappings');
unset($mappings[$form_state
->get('mapping_id')]);
$config
->set('field_mappings', $mappings)
->save();
$form_state
->setRedirect('samlauth_user_fields.list');
}
}