You are here

class DisqusSettingsForm in Disqus 8

Provides Disqus settings form.

Hierarchy

Expanded class hierarchy of DisqusSettingsForm

1 string reference to 'DisqusSettingsForm'
disqus.routing.yml in ./disqus.routing.yml
disqus.routing.yml

File

src/Form/DisqusSettingsForm.php, line 16

Namespace

Drupal\disqus\Form
View source
class DisqusSettingsForm extends ConfigFormBase {

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * A database backend file usage overridable.
   *
   * @var \Drupal\file\FileUsage\FileUsageInterface
   */
  protected $fileUsage;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a \Drupal\disqus\DisqusSettingsForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\file\FileUsage\FileUsageInterface $file_usage
   *   The file usage overridable.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, FileUsageInterface $file_usage, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($config_factory);
    $this->moduleHandler = $module_handler;
    $this->fileUsage = $file_usage;
    $this->entityTypeManager = $entity_type_manager;
  }

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

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'disqus_settings_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'disqus.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $disqus_config = $this
      ->config('disqus.settings');
    $form['disqus_domain'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Shortname'),
      '#description' => $this
        ->t('The website shortname that you registered Disqus with. If you registered http://example.disqus.com, you would enter "example" here.'),
      '#default_value' => $disqus_config
        ->get('disqus_domain'),
    ];
    $form['settings'] = [
      '#type' => 'vertical_tabs',
      '#attached' => [
        'library' => [
          'disqus/disqus.settings',
        ],
      ],
      '#weight' => 50,
    ];

    // Behavior settings.
    $form['behavior'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Behavior'),
      '#group' => 'settings',
    ];
    $form['behavior']['disqus_localization'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Localization support'),
      '#description' => $this
        ->t("When enabled, overrides the language set by Disqus with the language provided by the site."),
      '#default_value' => $disqus_config
        ->get('behavior.disqus_localization'),
    ];
    $form['behavior']['disqus_inherit_login'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Inherit User Credentials'),
      '#description' => $this
        ->t("When enabled and a user is logged in, the Disqus 'Post as Guest' login form will be pre-filled with the user's name and email address."),
      '#default_value' => $disqus_config
        ->get('behavior.disqus_inherit_login'),
    ];
    $form['behavior']['disqus_track_newcomment_ga'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Track new comments in Google Analytics'),
      '#description' => $this
        ->t('When enabled, sends tracking information to Google Analytics. This will work only if you have installed the <a href=":url">google_analytics</a> module.', [
        ':url' => 'https://www.drupal.org/project/google_analytics',
      ]),
      '#default_value' => $disqus_config
        ->get('behavior.disqus_track_newcomment_ga'),
    ];

    // Advanced settings.
    $form['advanced'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Advanced'),
      '#group' => 'settings',
      '#description' => $this
        ->t('Use these settings to configure the more advanced uses of Disqus. You can find more information about these in the <a href=":applications">Applications</a> section of Disqus. To enable some of these features, you will require a <a href=":addons">Disqus Add-on Package</a>.', [
        ':applications' => 'http://disqus.com/api/applications/',
        ':addons' => 'http://disqus.com/addons/',
      ]),
    ];
    $form['advanced']['disqus_useraccesstoken'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('User Access Token'),
      '#default_value' => $disqus_config
        ->get('advanced.disqus_useraccesstoken'),
    ];
    $form['advanced']['disqus_publickey'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Public Key'),
      '#default_value' => $disqus_config
        ->get('advanced.disqus_publickey'),
    ];
    $form['advanced']['disqus_secretkey'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Secret Key'),
      '#default_value' => $disqus_config
        ->get('advanced.disqus_secretkey'),
    ];
    $form['advanced']['api'] = [
      '#weight' => 4,
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Disqus API Settings'),
      '#description' => $this
        ->t('These setting pertain to the official Disqus PHP API. You will need to install the <a href=":composer-manager">Composer Manager module</a> and run the composer-manager\'s install command to download the api files and enable api functionality. Check the <a href=":disqus">Disqus module</a> project page for more information.', [
        ':composer-manager' => 'https://www.drupal.org/project/composer_manager',
        ':disqus' => 'https://www.drupal.org/project/disqus',
      ]),
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
    ];

    // API integration settings.
    if (class_exists('DisqusAPI')) {
      $form['advanced']['api']['disqus_api_update'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Update Threads'),
        '#description' => $this
          ->t('Update node titles and links via the disqus api when saving. (Requires your user access token.)'),
        '#default_value' => $disqus_config
          ->get('advanced.api.disqus_api_update'),
        '#states' => [
          'enabled' => [
            'input[name="disqus_useraccesstoken"]' => [
              'empty' => FALSE,
            ],
          ],
        ],
      ];
      $form['advanced']['api']['disqus_api_delete'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Close/Remove Threads'),
        '#description' => $this
          ->t('Action to take when deleting a node. (Requires your user access token.)'),
        '#default_value' => $disqus_config
          ->get('advanced.api.disqus_api_delete'),
        '#options' => [
          DISQUS_API_NO_ACTION => $this
            ->t('No Action'),
          DISQUS_API_CLOSE => $this
            ->t('Close Thread'),
          DISQUS_API_REMOVE => $this
            ->t('Remove Thread'),
        ],
        '#states' => [
          'enabled' => [
            'input[name="disqus_useraccesstoken"]' => [
              'empty' => FALSE,
            ],
          ],
        ],
      ];
    }

    // Single Sign-on settings.
    $form['advanced']['sso'] = [
      '#weight' => 5,
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Single Sign-on'),
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
      '#states' => [
        'visible' => [
          'input[name="disqus_publickey"]' => [
            'empty' => FALSE,
          ],
          'input[name="disqus_secretkey"]' => [
            'empty' => FALSE,
          ],
        ],
      ],
    ];
    $form['advanced']['sso']['disqus_sso'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use Single Sign-On'),
      '#description' => $this
        ->t('Provide <a href=":sso">Single Sign-On</a> access to your site.', [
        ':sso' => 'http://disqus.com/api/sso/',
      ]),
      '#default_value' => $disqus_config
        ->get('advanced.sso.disqus_sso'),
    ];
    $form['advanced']['sso']['disqus_use_site_logo'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use Site Logo'),
      '#description' => $this
        ->t('Pass the site logo to Disqus for use as SSO login button.'),
      '#default_value' => $disqus_config
        ->get('advanced.sso.disqus_use_site_logo'),
      '#states' => [
        'disabled' => [
          'input[name="disqus_sso"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['advanced']['sso']['disqus_logo'] = [
      '#type' => 'managed_file',
      '#title' => $this
        ->t('Custom Logo'),
      '#upload_location' => 'public://images',
      '#default_value' => [
        $disqus_config
          ->get('advanced.sso.disqus_logo'),
      ],
      '#upload_validators' => [
        'file_validate_extensions' => [
          'gif png jpg jpeg',
        ],
        // Disqus recommends the login button resolution as (143x32)
        // https://help.disqus.com/customer/portal/articles/236206-integrating-single-sign-on
        'file_validate_image_resolution' => [
          '143x32',
        ],
      ],
      '#states' => [
        'disabled' => [
          'input[name="disqus_sso"]' => [
            'checked' => FALSE,
          ],
        ],
        'visible' => [
          'input[name="disqus_use_site_logo"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('disqus.settings');
    $config
      ->set('disqus_domain', $form_state
      ->getValue('disqus_domain'))
      ->set('behavior.disqus_localization', $form_state
      ->getValue('disqus_localization'))
      ->set('behavior.disqus_inherit_login', $form_state
      ->getValue('disqus_inherit_login'))
      ->set('behavior.disqus_track_newcomment_ga', $form_state
      ->getValue('disqus_track_newcomment_ga'))
      ->set('advanced.disqus_useraccesstoken', $form_state
      ->getValue('disqus_useraccesstoken'))
      ->set('advanced.disqus_publickey', $form_state
      ->getValue('disqus_publickey'))
      ->set('advanced.disqus_secretkey', $form_state
      ->getValue('disqus_secretkey'))
      ->set('advanced.sso.disqus_sso', $form_state
      ->getValue('disqus_sso'))
      ->set('advanced.sso.disqus_use_site_logo', $form_state
      ->getValue('disqus_use_site_logo'))
      ->save();
    if ($form_state
      ->hasValue('disqus_api_update')) {
      $config
        ->set('advanced.api.disqus_api_update', $form_state
        ->getValue('disqus_api_update'))
        ->save();
    }
    if ($form_state
      ->hasValue('disqus_api_delete')) {
      $config
        ->set('advanced.api.disqus_api_delete', $form_state
        ->getValue('disqus_api_delete'))
        ->save();
    }
    $old_logo = $config
      ->get('advanced.sso.disqus_logo');
    $new_logo = !$form_state
      ->isValueEmpty('disqus_logo') ? $form_state
      ->getValue([
      'disqus_logo',
      0,
    ]) : '';

    // Ignore if the file hasn't changed.
    if ($new_logo != $old_logo) {

      // Remove the old file and usage if previously set.
      if (!empty($old_logo)) {
        $file = $this->entityTypeManager
          ->getStorage('file')
          ->load($old_logo);
        $this->fileUsage
          ->delete($file, 'disqus', 'disqus');
      }

      // Update the new file and usage.
      if (!empty($new_logo)) {
        $file = $this->entityTypeManager
          ->getStorage('file')
          ->load($new_logo);
        $this->fileUsage
          ->add($file, 'disqus', 'disqus', 1);
      }
    }
    $config
      ->set('advanced.sso.disqus_logo', $new_logo)
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
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
DisqusSettingsForm::$entityTypeManager protected property The entity type manager.
DisqusSettingsForm::$fileUsage protected property A database backend file usage overridable.
DisqusSettingsForm::$moduleHandler protected property The module handler.
DisqusSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
DisqusSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
DisqusSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
DisqusSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
DisqusSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
DisqusSettingsForm::__construct public function Constructs a \Drupal\disqus\DisqusSettingsForm object. Overrides ConfigFormBase::__construct
FormBase::$configFactory protected property The config factory. 1
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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
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.