You are here

public function DisqusSettingsForm::buildForm in Disqus 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/DisqusSettingsForm.php, line 87

Class

DisqusSettingsForm
Provides Disqus settings form.

Namespace

Drupal\disqus\Form

Code

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);
}