You are here

class MathjaxSettingsForm in MathJax: LaTeX for Drupal 8.2

Same name and namespace in other branches
  1. 3.0.x src/Form/MathjaxSettingsForm.php \Drupal\mathjax\Form\MathjaxSettingsForm

Presents the module settings form.

Hierarchy

Expanded class hierarchy of MathjaxSettingsForm

1 string reference to 'MathjaxSettingsForm'
mathjax.routing.yml in ./mathjax.routing.yml
mathjax.routing.yml

File

src/Form/MathjaxSettingsForm.php, line 12

Namespace

Drupal\mathjax\Form
View source
class MathjaxSettingsForm extends ConfigFormBase {

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('mathjax.settings');
    $form['test'] = array(
      '#type' => 'fieldset',
      '#title' => 'MathJax Test',
    );
    $form['test']['library'] = array(
      '#type' => 'item',
      '#markup' => '<div class="tex2jax_process"><p>If the MathJax library is installed properly, you should see the square root of x here: $ \\sqrt{x} $ and the square root of y here: \\(\\sqrt{y}\\)</p><p>$$\\text{The quadratic formula should appear here: } x = \\frac {-b \\pm \\sqrt {b^2 - 4ac}}{2a}$$</p><p>\\[\\text{The cubic equation should appear here: } a x^3\\; +\\; b x^2\\; +\\; c x\\; +\\; d\\; =\\; 0\\]</p></div>',
    );
    $form['use_cdn'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use MathJax Content Delivery Network (CDN)'),
      '#default_value' => $config
        ->get('use_cdn'),
      '#description' => t('Check this box to load MathJax source from MathJax servers (recommended) or from the link you can provide below. If you do not check this box, see the README about configuring a local MathJax source. <em>MathJax CDN services are provided subject to its <a href=":url">Terms of Service</a> (TOS). By accessing and using the MathJax CDN, you accept and agree to be bound by the terms and provisions of the TOS</em>.', [
        ':url' => 'https://www.mathjax.org/mathjax-cdn-terms-of-service/',
      ]),
    );
    $form['cdn_url'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('MathJax CDN URL'),
      '#default_value' => $config
        ->get('cdn_url'),
      '#description' => $this
        ->t("Enter the Mathjax CDN url here or leave it unchanged to use the one provided by <a target='_blank' href=':url'>www.mathjax.org</a>.", array(
        ':url' => 'http://www.mathjax.org',
      )),
      '#states' => array(
        'invisible' => array(
          ':input[name="use_cdn"]' => array(
            'checked' => FALSE,
          ),
        ),
      ),
    );
    $form['config_type'] = array(
      '#type' => 'radios',
      '#title' => $this
        ->t('Configuration Type'),
      '#options' => array(
        0 => $this
          ->t('Text Format (Recommended&mdash;Add the MathJax filter to a <a href=":textformats">text format</a>.)', array(
          ':textformats' => Url::fromRoute('filter.admin_overview')
            ->toString(),
        )),
        1 => $this
          ->t('Custom'),
      ),
      '#default_value' => $config
        ->get('config_type'),
    );
    $form['mathjax_note_default'] = array(
      '#type' => 'item',
      '#prefix' => '<span class="tex2jax_ignore">',
      '#markup' => $this
        ->t('MathJax
      will be available as a text filter. Mathematics inside the
      default delimiters will be rendered by MathJax. The
      default math delimiters are $$...$$ and \\[...\\] for displayed mathematics,
      and $...$ and \\(...\\) for in-line mathematics. <strong>You must add
      the MathJax filter to a <a href=":textformats">text format</a> and put
      MathJax at the bottom of the filter processing order.</strong>', array(
        ':textformats' => Url::fromRoute('filter.admin_overview')
          ->toString(),
      )),
      '#suffix' => '</span>',
      '#states' => array(
        'invisible' => array(
          ':input[name="config_type"]' => array(
            'value' => 1,
          ),
        ),
      ),
    );
    $form['config_string'] = array(
      '#type' => 'textarea',
      '#title' => $this
        ->t('Custom configuration'),
      '#default_value' => $config
        ->get('config_string') ? $config
        ->get('config_string') : $config
        ->get('default_config_string'),
      '#description' => $this
        ->t("Enter a JSON configuration string as documented on  <a target='_blank' href=':mathjax-help'>MathJax help</a>. Use with caution as you may introduce JavaScript errors.", array(
        ':mathjax-help' => 'http://docs.mathjax.org/en/latest/',
      )),
      '#states' => array(
        'invisible' => array(
          ':input[name="config_type"]' => array(
            'value' => 0,
          ),
        ),
      ),
    );
    $config_type = $config
      ->get('config_type');
    if ($config_type == 0) {
      $config_string = $config
        ->get('default_config_string');
    }
    else {
      $config_string = $config
        ->get('config_string');
    }
    $form['#attached'] = [
      'library' => [
        'mathjax/source',
      ],
      'drupalSettings' => [
        'mathjax' => [
          'config_type' => $config_type,
          'config' => json_decode($config_string),
        ],
      ],
    ];
    $form['enable_for_admin'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable on admin pages.'),
      '#description' => $this
        ->t('Embeds the MathJax code on admin pages.'),
      '#default_value' => $config
        ->get('enable_for_admin'),
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this
      ->config('mathjax.settings')
      ->set('use_cdn', $form_state
      ->getValue('use_cdn'))
      ->set('cdn_url', $form_state
      ->getValue('cdn_url'))
      ->set('config_type', $form_state
      ->getValue('config_type'))
      ->set('config_string', $form_state
      ->getValue('config_string'))
      ->set('enable_for_admin', $form_state
      ->getValue('enable_for_admin'))
      ->save();
    drupal_flush_all_caches();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
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
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.
MathjaxSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
MathjaxSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
MathjaxSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
MathjaxSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
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.