You are here

class ContentHubSettingsForm in Acquia Content Hub 8

Same name and namespace in other branches
  1. 8.2 src/Form/ContentHubSettingsForm.php \Drupal\acquia_contenthub\Form\ContentHubSettingsForm

Defines the form to configure the Content Hub connection settings.

Hierarchy

Expanded class hierarchy of ContentHubSettingsForm

1 string reference to 'ContentHubSettingsForm'
acquia_contenthub.routing.yml in ./acquia_contenthub.routing.yml
acquia_contenthub.routing.yml

File

src/Form/ContentHubSettingsForm.php, line 17

Namespace

Drupal\acquia_contenthub\Form
View 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

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
ContentHubSettingsForm::$clientManager protected property The entity manager.
ContentHubSettingsForm::$configFactory protected property Config Factory. Overrides FormBase::$configFactory
ContentHubSettingsForm::$contentHubSubscription protected property Content Hub Subscription.
ContentHubSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
ContentHubSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
ContentHubSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
ContentHubSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ContentHubSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
ContentHubSettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
ContentHubSettingsForm::__construct public function ContentHubSettingsForm constructor. Overrides ConfigFormBase::__construct
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.