View source
<?php
namespace Drupal\acquia_contenthub\Form;
use Drupal\acquia_contenthub\Client\ClientManagerInterface;
use Drupal\acquia_contenthub\ContentHubSubscription;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Component\Uuid\Uuid;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContentHubSettingsForm extends ConfigFormBase {
protected $clientManager;
protected $contentHubSubscription;
protected $configFactory;
public function getFormId() {
return 'acquia_contenthub.admin_settings';
}
protected function getEditableConfigNames() {
return [
'acquia_contenthub.admin_settings',
];
}
public function __construct(ClientManagerInterface $client_manager, ContentHubSubscription $contenthub_subscription, ConfigFactoryInterface $config_factory) {
$this->clientManager = $client_manager;
$this->contentHubSubscription = $contenthub_subscription;
$this->configFactory = $config_factory;
}
public static function create(ContainerInterface $container) {
$client_manager = $container
->get('acquia_contenthub.client_manager');
$contenthub_subscription = $container
->get('acquia_contenthub.acquia_contenthub_subscription');
$config_factory = $container
->get('config.factory');
return new static($client_manager, $contenthub_subscription, $config_factory);
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->configFactory
->get('acquia_contenthub.admin_settings');
$form['settings'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Connection Settings'),
'#collapsible' => TRUE,
'#description' => $this
->t('Settings for connection to Acquia Content Hub'),
];
$form['settings']['hostname'] = [
'#type' => 'textfield',
'#title' => $this
->t('Acquia Content Hub Hostname'),
'#description' => $this
->t('The hostname of the Acquia Content Hub API without trailing slash at end of URL, e.g. http://localhost:5000'),
'#default_value' => $config
->get('hostname'),
'#required' => TRUE,
];
$form['settings']['api_key'] = [
'#type' => 'textfield',
'#title' => $this
->t('API Key'),
'#default_value' => $config
->get('api_key'),
'#required' => TRUE,
];
$form['settings']['secret_key'] = [
'#type' => 'password',
'#title' => $this
->t('Secret Key'),
'#default_value' => $config
->get('secret_key'),
];
$client_name = $config
->get('client_name');
$form['settings']['hmac_version'] = [
'#type' => 'select',
'#title' => 'HMAC Version',
'#options' => [
'V1' => $this
->t('Version 1'),
'V2' => $this
->t('Version 2'),
],
'#default' => 'V1',
'#description' => $this
->t('Choose which HMAC version your subscribers and publisher talk. Version 1 Is the only supported option, Version 2 is coming soon.'),
'#attributes' => [
'disabled' => TRUE,
],
];
$readonly = empty($client_name) ? [] : [
'readonly' => TRUE,
];
$form['settings']['client_name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Client Name'),
'#default_value' => $client_name,
'#required' => TRUE,
'#description' => $this
->t('A unique client name by which Acquia Content Hub will identify this site. The name of this client site cannot be changed once set.'),
'#attributes' => $readonly,
];
$form['settings']['origin'] = [
'#type' => 'item',
'#title' => $this
->t("Site's Origin UUID"),
'#markup' => $config
->get('origin'),
];
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$hostname = NULL;
if (UrlHelper::isValid($form_state
->getValue('hostname'), TRUE)) {
$hostname = rtrim($form_state
->getValue('hostname'), '/');
}
else {
return $form_state
->setErrorByName('hostname', $this
->t('This is not a valid URL. Please insert it again.'));
}
$api = NULL;
if ($form_state
->getValue('api_key')) {
$api = $form_state
->getValue('api_key');
}
else {
return $form_state
->setErrorByName('api_key', $this
->t('Please insert an API Key.'));
}
$secret = NULL;
if ($form_state
->hasValue('secret_key')) {
$secret = $form_state
->getValue('secret_key');
}
else {
return $form_state
->setErrorByName('secret_key', $this
->t('Please insert a Secret Key.'));
}
if ($form_state
->hasValue('client_name')) {
$client_name = $form_state
->getValue('client_name');
}
else {
return $form_state
->setErrorByName('client_name', $this
->t('Please insert a Client Name.'));
}
if (Uuid::isValid($form_state
->getValue('origin'))) {
$origin = $form_state
->getValue('origin');
}
else {
$origin = '';
}
$this->clientManager
->resetConnection([
'hostname' => $hostname,
'api' => $api,
'secret' => $secret,
'origin' => $origin,
]);
if ($this->clientManager
->isClientNameAvailable($client_name) === FALSE) {
$message = $this
->t('The client name "%name" is already being used. Please insert another one.', [
'%name' => $client_name,
]);
return $form_state
->setErrorByName('client_name', $message);
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$client_name = $form_state
->getValue('client_name');
if ($this->contentHubSubscription
->registerClient($client_name)) {
$config = $this
->config('acquia_contenthub.admin_settings');
if ($form_state
->hasValue('hostname')) {
$hostname = rtrim($form_state
->getValue('hostname'), '/');
$config
->set('hostname', $hostname);
}
if ($form_state
->hasValue('api_key')) {
$config
->set('api_key', $form_state
->getValue('api_key'));
}
if ($form_state
->hasValue('secret_key')) {
$secret = $form_state
->getValue('secret_key');
$config
->set('secret_key', $secret);
}
$config
->save();
}
else {
$config = $this
->config('acquia_contenthub.admin_settings');
$this->messenger
->all();
if (!empty($config
->get('origin'))) {
$this
->messenger()
->addError($this
->t('Client is already registered with Acquia Content Hub.'));
}
else {
$this
->messenger()
->addError($this
->t('There is a problem connecting to Acquia Content Hub. Please ensure that your hostname and credentials are correct.'));
}
}
}
}