SimplesamlphpCustomAttributesDeleteForm.php in SimpleSAMLphp Custom Attribute Mapping 8
File
src/Form/SimplesamlphpCustomAttributesDeleteForm.php
View source
<?php
namespace Drupal\simplesamlphp_custom_attributes\Form;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SimplesamlphpCustomAttributesDeleteForm extends ConfirmFormBase {
protected $mappingConfig;
protected $entityFieldManager;
protected $attributeName;
protected $fieldName;
public function __construct(EntityFieldManagerInterface $entity_field_manager) {
$configFactory = $this
->configFactory();
$this->mappingConfig = $configFactory
->getEditable('simplesamlphp_custom_attributes.mappings');
$this->entityFieldManager = $entity_field_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_field.manager'));
}
public function getFormId() {
return 'simplesamlphp_custom_attributes_delete_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $mapping = FALSE) {
if (is_numeric($mapping)) {
$mappings = $this->mappingConfig
->get('mappings');
$this->attributeName = $mappings[$mapping]['attribute_name'];
$this->fieldName = $mappings[$mapping]['field_name'];
$form_state
->set('simplesamlphp_custom_attributes_mapping', $mapping);
return parent::buildForm($form, $form_state);
}
return FALSE;
}
public function getQuestion() {
$fields = $this->entityFieldManager
->getFieldDefinitions('user', 'user');
if (isset($fields[$this->fieldName])) {
$field_name = $fields[$this->fieldName]
->getLabel();
}
else {
$field_name = $this
->t('Custom');
}
return $this
->t('Are you sure you want to delete the ":attribute > :field" mapping?', [
':attribute' => $this->attributeName,
':field' => $field_name,
]);
}
public function getCancelUrl() {
return new Url('simplesamlphp_custom_attributes.list');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$mappings = $this->mappingConfig
->get('mappings');
unset($mappings[$form_state
->get('simplesamlphp_custom_attributes_mapping')]);
$this->mappingConfig
->set('mappings', $mappings)
->save();
$form_state
->setRedirect('simplesamlphp_custom_attributes.list');
}
}