AuthenticationForm.php in Apigee Edge 8
File
src/Form/AuthenticationForm.php
View source
<?php
namespace Drupal\apigee_edge\Form;
use Drupal\apigee_edge\Exception\KeyProviderRequirementsException;
use Drupal\apigee_edge\Plugin\KeyProviderRequirementsInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\key\Form\KeyEditForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AuthenticationForm extends KeyEditForm {
public const CONFIG_NAME = 'apigee_edge.auth';
public const DEFAULT_KEY_ENTITY_ID = 'apigee_edge_connection_default';
public function __construct(ConfigEntityStorageInterface $key_storage, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
parent::__construct($key_storage);
$this->configFactory = $config_factory;
$this->moduleHandler = $module_handler;
$config = $config_factory
->get(static::CONFIG_NAME);
if (!($active_key_id = $config
->get('active_key')) || !($active_key = $key_storage
->load($active_key_id))) {
$active_key = $key_storage
->create([
'id' => static::DEFAULT_KEY_ENTITY_ID,
'label' => $this
->t('Apigee Edge connection'),
'description' => $this
->t('Contains the credentials for connecting to Apigee Edge.'),
'key_type' => 'apigee_auth',
'key_input' => 'apigee_auth_input',
'key_provider' => 'apigee_edge_private_file',
]);
}
$this->entity = $active_key;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager')
->getStorage('key'), $container
->get('config.factory'), $container
->get('module_handler'));
}
public function getFormId() {
return 'apigee_edge_authentication_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
if ($this->entity
->isNew() && !$form_state
->isRebuilding()) {
$form_state
->set('key_value', [
'original' => NULL,
'processed_original' => NULL,
'obscured' => NULL,
'current' => '',
]);
}
if (!$this->editConfirmed) {
$this
->confirmEdit($form, $form_state);
$form_state
->setRebuild(FALSE);
}
$form = parent::buildForm($form, $form_state);
unset($form['#title']);
$form['label']['#access'] = FALSE;
$form['id']['#access'] = FALSE;
$form['description']['#access'] = FALSE;
$form['settings']['type_section']['#access'] = FALSE;
$form['settings']['input_section']['#title'] = $this
->t('Apigee Edge connection settings');
$form['settings']['input_section']['#weight'] = 0;
$form['settings']['provider_section']['#title'] = $this
->t('Advanced settings');
$key_provider = $this->entity
->getKeyProvider();
$key_provider_requirements_error = FALSE;
if ($key_provider instanceof KeyProviderRequirementsInterface) {
try {
$key_provider
->checkRequirements($this->entity);
} catch (KeyProviderRequirementsException $exception) {
$key_provider_requirements_error = TRUE;
$form['settings']['provider_section']['key_provider']['#attributes']['class'][] = 'error';
}
}
$form['settings']['provider_section']['#open'] = $key_provider_requirements_error || $form_state
->getTriggeringElement() && $form_state
->getTriggeringElement()['#name'] === 'key_provider';
$form['settings']['provider_section']['#weight'] = $form['settings']['input_section']['#weight'] + 1;
$form['actions']['submit']['#value'] = $this
->t('Save configuration');
$form['actions']['delete']['#access'] = FALSE;
$form['#attached']['library'][] = 'apigee_edge/apigee_edge.admin';
return $form;
}
public function save(array $form, FormStateInterface $form_state) {
$status = parent::save($form, $form_state);
$this->configFactory
->getEditable(static::CONFIG_NAME)
->set('active_key', $this->entity
->id())
->save();
$form_state
->setRedirect('apigee_edge.settings');
return $status;
}
}