View source
<?php
namespace Drupal\apigee_edge\Form;
use Apigee\Edge\Api\Management\Controller\EnvironmentController;
use Drupal\apigee_edge\SDKConnectorInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AppAnalyticsSettingsForm extends ConfigFormBase {
protected $environmentController;
public function __construct(ConfigFactoryInterface $config_factory, SDKConnectorInterface $sdk_connector) {
parent::__construct($config_factory);
$this->environmentController = new EnvironmentController($sdk_connector
->getOrganization(), $sdk_connector
->getClient());
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('apigee_edge.sdk_connector'));
}
protected function getEditableConfigNames() {
return [
'apigee_edge.common_app_settings',
];
}
public function getFormId() {
return 'apigee_edge_app_analytics_settings_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$environments = $this->environmentController
->getEntityIds();
$environments = array_combine($environments, $environments);
unset($environments['portal']);
$form['label'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Environment to query for analytics data'),
'#collapsible' => FALSE,
];
$form['label']['available_environments'] = [
'#type' => 'checkboxes',
'#required' => TRUE,
'#title' => $this
->t('Which environments should be displayed on the form to query analytics data?'),
'#default_value' => $this
->config('apigee_edge.common_app_settings')
->get('analytics_available_environments') ?: [],
'#options' => $environments,
];
$form['label']['environment'] = [
'#type' => 'select',
'#required' => TRUE,
'#title' => $this
->t('Which environment should be selected by default?'),
'#default_value' => $this
->config('apigee_edge.common_app_settings')
->get('analytics_environment'),
'#options' => $environments,
];
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
if (!in_array($form_state
->getValue('environment'), array_values(array_filter($form_state
->getValue('available_environments'))))) {
$form_state
->setError($form['label']['environment'], $this
->t('The selected default environment is not available on the form.'));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this
->config('apigee_edge.common_app_settings')
->set('analytics_environment', $form_state
->getValue('environment'))
->set('analytics_available_environments', array_values(array_filter($form_state
->getValue('available_environments'))))
->save();
parent::submitForm($form, $form_state);
}
}