You are here

public function AuthenticationForm::__construct in Apigee Edge 8

AuthenticationForm constructor.

Parameters

\Drupal\Core\Config\Entity\ConfigEntityStorageInterface $key_storage: The key storage.

\Drupal\Core\Config\ConfigFactoryInterface $config_factory: The config factory.

\Drupal\Core\Extension\ModuleHandlerInterface $module_handler: The module handler.

Overrides KeyFormBase::__construct

File

src/Form/AuthenticationForm.php, line 61

Class

AuthenticationForm
Provides a form for saving the Apigee Edge API authentication key.

Namespace

Drupal\apigee_edge\Form

Code

public function __construct(ConfigEntityStorageInterface $key_storage, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
  parent::__construct($key_storage);
  $this->configFactory = $config_factory;

  // Module handler must be set but Key does not set it.
  $this->moduleHandler = $module_handler;

  // If we use `$this->config()`, config overrides won't be considered.
  $config = $config_factory
    ->get(static::CONFIG_NAME);

  // Loads to the key entity that belongs to the active key or creates a
  // new one _without_ saving it.
  if (!($active_key_id = $config
    ->get('active_key')) || !($active_key = $key_storage
    ->load($active_key_id))) {

    /** @var \Drupal\key\KeyInterface $active_key */
    $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',
    ]);
  }

  // Sets the entity object for the form. This is the best place where we
  // can do that if we do not want to override n+1 methods inherited from the
  // EntityForm.
  $this->entity = $active_key;
}