class ContentHubSettingsForm in Acquia Content Hub 8
Same name and namespace in other branches
- 8.2 src/Form/ContentHubSettingsForm.php \Drupal\acquia_contenthub\Form\ContentHubSettingsForm
Defines the form to configure the Content Hub connection settings.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\acquia_contenthub\Form\ContentHubSettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of ContentHubSettingsForm
1 string reference to 'ContentHubSettingsForm'
File
- src/
Form/ ContentHubSettingsForm.php, line 17
Namespace
Drupal\acquia_contenthub\FormView source
class ContentHubSettingsForm extends ConfigFormBase {
/**
* The entity manager.
*
* @var |Drupal\acquia_contenthub\Client\ClientManagerInterface
*/
protected $clientManager;
/**
* Content Hub Subscription.
*
* @var \Drupal\acquia_contenthub\ContentHubSubscription
*/
protected $contentHubSubscription;
/**
* Config Factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'acquia_contenthub.admin_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'acquia_contenthub.admin_settings',
];
}
/**
* ContentHubSettingsForm constructor.
*
* @param \Drupal\acquia_contenthub\Client\ClientManagerInterface $client_manager
* The client manager.
* @param \Drupal\acquia_contenthub\ContentHubSubscription $contenthub_subscription
* The content hub subscription.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
*/
public function __construct(ClientManagerInterface $client_manager, ContentHubSubscription $contenthub_subscription, ConfigFactoryInterface $config_factory) {
$this->clientManager = $client_manager;
$this->contentHubSubscription = $contenthub_subscription;
$this->configFactory = $config_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
/** @var \Drupal\acquia_contenthub\Client\ClientManagerInterface $client_manager */
$client_manager = $container
->get('acquia_contenthub.client_manager');
/** @var \Drupal\acquia_contenthub\ContentHubSubscription $contenthub_subscription */
$contenthub_subscription = $container
->get('acquia_contenthub.acquia_contenthub_subscription');
/** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
$config_factory = $container
->get('config.factory');
return new static($client_manager, $contenthub_subscription, $config_factory);
}
/**
* {@inheritdoc}
*/
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);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$hostname = NULL;
if (UrlHelper::isValid($form_state
->getValue('hostname'), TRUE)) {
// Remove trailing slash at end of URL.
$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;
// Important. This should never validate if it is an UUID. Lift 3 does not
// use UUIDs for the api_key but they are valid for Content Hub.
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 = '';
}
// Validate that the client name does not exist yet.
$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);
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
// We assume here all inserted values have passed validation.
// First Register the site to Content Hub.
$client_name = $form_state
->getValue('client_name');
if ($this->contentHubSubscription
->registerClient($client_name)) {
// Registration was successful. Save the rest of the values.
// Get the admin config.
$config = $this
->config('acquia_contenthub.admin_settings');
if ($form_state
->hasValue('hostname')) {
// Remove trailing slash at end of URL.
$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 {
// Get the admin config.
$config = $this
->config('acquia_contenthub.admin_settings');
// Call drupal_get_messages, to override the dsm. Otherwise,
// on save it will show two messages.
// @todo I don't believe this line is needed with the messenger service.
$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.'));
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
ContentHubSettingsForm:: |
protected | property | The entity manager. | |
ContentHubSettingsForm:: |
protected | property |
Config Factory. Overrides FormBase:: |
|
ContentHubSettingsForm:: |
protected | property | Content Hub Subscription. | |
ContentHubSettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
ContentHubSettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
ContentHubSettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
ContentHubSettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ContentHubSettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
ContentHubSettingsForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
ContentHubSettingsForm:: |
public | function |
ContentHubSettingsForm constructor. Overrides ConfigFormBase:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |