You are here

class AdminSettings in HubSpot 3.x

Same name and namespace in other branches
  1. 8 src/Form/AdminSettings.php \Drupal\hubspot\Form\AdminSettings

Hubspot admin settings form.

@package Drupal\hubspot\Form

Hierarchy

Expanded class hierarchy of AdminSettings

1 string reference to 'AdminSettings'
hubspot.routing.yml in ./hubspot.routing.yml
hubspot.routing.yml

File

src/Form/AdminSettings.php, line 18

Namespace

Drupal\hubspot\Form
View source
class AdminSettings extends ConfigFormBase {

  /**
   * Config settings.
   *
   * @var string
   */
  const SETTINGS = 'hubspot.settings';

  /**
   * The state key/value store.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * Constructs a new AdminSettings object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state key/value store.
   */
  public function __construct(ConfigFactoryInterface $config_factory, StateInterface $state) {
    parent::__construct($config_factory);
    $this->state = $state;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('state'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() : string {
    return 'hubspot_admin_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() : array {
    return [
      static::SETTINGS,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) : array {
    $config = $this
      ->config(static::SETTINGS);
    $form = [];
    $form['settings'] = [
      '#title' => $this
        ->t('Settings'),
      '#type' => 'fieldset',
    ];

    // Settings Tab.
    $form['settings']['hubspot_portal_id'] = [
      '#title' => $this
        ->t('HubSpot Portal ID'),
      '#type' => 'textfield',
      '#required' => TRUE,
      '#default_value' => $config
        ->get('hubspot_portal_id'),
      '#description' => $this
        ->t('Enter the HubSpot Portal ID for this site. <a href=":url" target="_blank">How do I find my portal ID?</a>.', [
        '@url' => 'https://knowledge.hubspot.com/articles/kcs_article/account/where-can-i-find-my-hub-id',
      ]),
    ];
    if ($config
      ->get('hubspot_portal_id')) {
      $form['settings']['hubspot_authentication'] = [
        '#value' => $this
          ->t('Connect HubSpot Account'),
        '#type' => 'submit',
        '#submit' => [
          [
            $this,
            'hubspotOauthSubmitForm',
          ],
        ],
      ];
      if ($this->state
        ->get('hubspot.hubspot_refresh_token')) {
        $form['settings']['hubspot_authentication']['#suffix'] = $this
          ->t('Your HubSpot account is connected.');
        $form['settings']['hubspot_authentication']['#value'] = $this
          ->t('Disconnect HubSpot Account');
        $form['settings']['hubspot_authentication']['#submit'] = [
          [
            $this,
            'hubspotOauthDisconnect',
          ],
        ];
      }
    }

    // Application Settings.
    $form['settings']['app_settings'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('App Settings'),
    ];
    $form['settings']['app_settings']['hubspot_client_id'] = [
      '#title' => $this
        ->t('HubSpot Client ID'),
      '#type' => 'textfield',
      '#required' => TRUE,
      '#default_value' => $config
        ->get('hubspot_client_id'),
      '#description' => $this
        ->t('Enter the HubSpot application Client ID.
        <a href="https://developers.hubspot.com/docs/faq/how-do-i-create-an-app-in-hubspot" target="_blank">How do I find my Client ID?</a>'),
    ];
    $form['settings']['app_settings']['hubspot_client_secret'] = [
      '#title' => $this
        ->t('HubSpot Client Secret'),
      '#type' => 'textfield',
      '#required' => TRUE,
      '#default_value' => $config
        ->get('hubspot_client_secret'),
      '#description' => $this
        ->t('Enter the HubSpot application Client Secret.
      <a href="https://developers.hubspot.com/docs/faq/how-do-i-create-an-app-in-hubspot" target="_blank">How do I find my Client Secret?</a>'),
    ];
    $form['settings']['app_settings']['hubspot_scope'] = [
      '#title' => $this
        ->t('HubSpot Scope'),
      '#type' => 'textfield',
      '#default_value' => $config
        ->get('hubspot_scope'),
      '#description' => $this
        ->t('Enter the scopes required by your app. Click
      <a href="https://developers.hubspot.com/docs/methods/oauth2/initiate-oauth-integration#scopes" target="_blank">here</a>
      to see how to see what scopes are available and how to format them. For example, <em>contacts forms</em> will give you
      access to the contacts and forms API on HubSpot. Note: Your HubSpot App, must have these options checked.'),
    ];

    // Tracking Settings.
    $form['settings']['tracking'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Tracking'),
    ];
    $form['settings']['tracking']['tracking_code_on'] = [
      '#title' => $this
        ->t('Enable Tracking Code'),
      '#type' => 'checkbox',
      '#default_value' => $config
        ->get('tracking_code_on'),
      '#description' => $this
        ->t('If Tracking code is enabled, Javascript
      tracking will be inserted in all/specified pages of the site as configured
       in HubSpot account.'),
    ];

    // Debug Settings.
    $form['settings']['debugging'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Debugging'),
    ];
    $form['settings']['debugging']['hubspot_debug_on'] = [
      '#title' => $this
        ->t('Debugging enabled'),
      '#type' => 'checkbox',
      '#default_value' => $config
        ->get('hubspot_debug_on'),
      '#description' => $this
        ->t('If debugging is enabled, HubSpot errors will be emailed to the address below. Otherwise, they
      will be logged to the regular Drupal error log.'),
    ];
    $form['settings']['debugging']['hubspot_debug_email'] = [
      '#title' => $this
        ->t('Debugging email'),
      '#type' => 'email',
      '#default_value' => $config
        ->get('hubspot_debug_email'),
      '#description' => $this
        ->t('Email error reports to this address if debugging is enabled.'),
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => 'Save Configuration',
    ];
    return $form;
  }

  /**
   * {@inheritDoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->configFactory
      ->getEditable(static::SETTINGS)
      ->set('hubspot_portal_id', $form_state
      ->getValue('hubspot_portal_id'))
      ->set('hubspot_client_id', $form_state
      ->getValue('hubspot_client_id'))
      ->set('hubspot_client_secret', $form_state
      ->getValue('hubspot_client_secret'))
      ->set('hubspot_scope', $form_state
      ->getValue('hubspot_scope'))
      ->set('hubspot_debug_email', $form_state
      ->getValue('hubspot_debug_email'))
      ->set('hubspot_debug_on', $form_state
      ->getValue('hubspot_debug_on'))
      ->set('tracking_code_on', $form_state
      ->getValue([
      'tracking_code_on',
    ]))
      ->save();
    $this
      ->messenger()
      ->addStatus($this
      ->t('The configuration options have been saved.'));
  }

  /**
   * Form submission handler for hubspot_admin_settings().
   *
   * @param array $form
   *   Active form build.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Active form state.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   Ajax response.
   */
  public function hubspotOauthSubmitForm(array &$form, FormStateInterface $form_state) : RedirectResponse {
    $config = $this
      ->config(static::SETTINGS);
    global $base_url;
    $options = [
      'query' => [
        'client_id' => $config
          ->get('hubspot_client_id'),
        'redirect_uri' => $base_url . Url::fromRoute('hubspot.oauth_connect')
          ->toString(),
        'scope' => $config
          ->get('hubspot_scope'),
      ],
    ];
    $redirect_url = Url::fromUri('https://app.hubspot.com/oauth/authorize', $options)
      ->toString();
    $response = new RedirectResponse($redirect_url);
    $response
      ->send();
    return $response;
  }

  /**
   * Deletes Hubspot OAuth tokens.
   *
   * @param array $form
   *   Active form build.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Active form state.
   */
  public function hubspotOauthDisconnect(array &$form, FormStateInterface $form_state) {
    $this
      ->messenger()
      ->addStatus($this
      ->t('Successfully disconnected from HubSpot.'), FALSE);
    $this->state
      ->delete('hubspot.hubspot_refresh_token');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AdminSettings::$state protected property The state key/value store.
AdminSettings::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
AdminSettings::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
AdminSettings::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
AdminSettings::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
AdminSettings::hubspotOauthDisconnect public function Deletes Hubspot OAuth tokens.
AdminSettings::hubspotOauthSubmitForm public function Form submission handler for hubspot_admin_settings().
AdminSettings::SETTINGS constant Config settings.
AdminSettings::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
AdminSettings::__construct public function Constructs a new AdminSettings object. Overrides ConfigFormBase::__construct
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
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. 3
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.
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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
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. 27
MessengerTrait::messenger public function Gets the messenger. 27
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. 4
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.