You are here

class ExtlinkAdminSettingsForm in External Links 8

Displays the extlink settings form.

Hierarchy

Expanded class hierarchy of ExtlinkAdminSettingsForm

1 string reference to 'ExtlinkAdminSettingsForm'
extlink.routing.yml in ./extlink.routing.yml
extlink.routing.yml

File

src/Form/ExtlinkAdminSettingsForm.php, line 14

Namespace

Drupal\extlink\Form
View source
class ExtlinkAdminSettingsForm extends ConfigFormBase {

  /**
   * Drupal\Core\Render\Renderer definition.
   *
   * @var Drupal\Core\Render\Renderer
   */
  protected $renderer;

  /**
   * Class constructor.
   */
  public function __construct(ConfigFactoryInterface $config_factory, Renderer $renderer) {
    parent::__construct($config_factory);
    $this->renderer = $renderer;
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('extlink.settings');
    $renderer = $this->renderer;
    $form['extlink_exclude_admin_routes'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Disable on admin routes.'),
      '#default_value' => $config
        ->get('extlink_exclude_admin_routes'),
      '#description' => $this
        ->t('Whether the extlink module should be disabled on admin routes.'),
    ];
    $form['extlink_use_external_js_file'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Load exclusions and inclusions externally.'),
      '#default_value' => $config
        ->get('extlink_use_external_js_file'),
      '#description' => $this
        ->t('Whether the extlink JS settings should be added to the page via an external settings file. In the case of a large number of patterns, this will reduce the amount of markup added to each page.'),
    ];
    $form['extlink_class'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Place an icon next to external links.'),
      '#return_value' => 'ext',
      '#default_value' => $config
        ->get('extlink_class'),
      '#description' => $this
        ->t('Places an <span class="ext"> </span>&nbsp; icon next to external links.'),
    ];
    $form['extlink_mailto_class'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Place an icon next to mailto links.'),
      '#return_value' => 'mailto',
      '#default_value' => $config
        ->get('extlink_mailto_class'),
      '#description' => $this
        ->t('Places an <span class="mailto"> </span>&nbsp; icon next to mailto links.'),
    ];
    $form['extlink_img_class'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Place an icon next to image links.'),
      '#default_value' => $config
        ->get('extlink_img_class', FALSE),
      '#description' => $this
        ->t('If checked, images wrapped in an anchor tag will be treated as external links.'),
    ];
    $form['extlink_use_font_awesome'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use Font Awesome icons instead of images.'),
      '#default_value' => $config
        ->get('extlink_use_font_awesome', FALSE),
      '#description' => $this
        ->t('Add Font Awesome classes to the link as well as an i tag rather than images.'),
    ];
    $form['extlink_font_awesome_classes'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Font Awesome icon classes'),
      '#tree' => TRUE,
      '#states' => [
        'visible' => [
          ':input[name="extlink_use_font_awesome"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['extlink_font_awesome_classes']['links'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Font Awesome External Links Classes'),
      '#default_value' => $config
        ->get('extlink_font_awesome_classes.links') ?: 'fa fa-external-link',
      '#states' => [
        'visible' => [
          ':input[name="extlink_class"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['extlink_font_awesome_classes']['mailto'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Font Awesome mailto Links Classes'),
      '#default_value' => $config
        ->get('extlink_font_awesome_classes.mailto') ?: 'fa fa-envelope-o',
      '#states' => [
        'visible' => [
          ':input[name="extlink_mailto_class"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['extlink_icon_placement'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Add icon in front of any processed link.'),
      '#return_value' => 'prepend',
      '#default_value' => $config
        ->get('extlink_icon_placement', 'append'),
      '#description' => $this
        ->t('If checked, the icon will be placed in front of any external link, otherwise it will be placed behind it.'),
    ];
    $form['extlink_subdomains'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Exclude links with the same primary domain.'),
      '#default_value' => $config
        ->get('extlink_subdomains'),
      '#description' => $this
        ->t("For example, a link from 'www.example.com' to the subdomain of 'my.example.com' would be excluded."),
    ];
    $form['extlink_target'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Open external links in a new window or tab.'),
      '#default_value' => $config
        ->get('extlink_target'),
      '#description' => $this
        ->t('A link will open in a new window or tab (depending on which web browser is used and how it is configured).'),
    ];
    $form['extlink_target_no_override'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Do not alter links with a default target value'),
      '#default_value' => $config
        ->get('extlink_target_no_override'),
      '#description' => $this
        ->t("A link that specifies target='_self' will not be changed to target='_blank'."),
      '#states' => [
        'visible' => [
          ':input[name="extlink_target"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['extlink_noreferrer'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Tag external links as "noreferrer".'),
      '#default_value' => $config
        ->get('extlink_noreferrer'),
    ];
    $form['extlink_nofollow'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Tag external links as "no follow".'),
      '#default_value' => $config
        ->get('extlink_nofollow'),
    ];
    $form['extlink_follow_no_override'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Do not override rel="follow", if set'),
      '#default_value' => $config
        ->get('extlink_follow_no_override'),
      '#states' => [
        'visible' => [
          ':input[name="extlink_nofollow"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['extlink_alert'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Display a pop-up warning when any external link is clicked.'),
      '#default_value' => $config
        ->get('extlink_alert'),
    ];
    $form['extlink_alert_text'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Text to display in the pop-up warning box.'),
      '#rows' => 3,
      '#default_value' => $config
        ->get('extlink_alert_text'),
      '#description' => $this
        ->t('Text to display in the pop-up external link warning box.'),
      '#wysiwyg' => FALSE,
      '#states' => [
        // Only show this field when user opts to display a pop-up warning.
        'visible' => [
          ':input[name="extlink_alert"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['whitelisted_domains'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Whitelisted domains.'),
      '#maxlength' => NULL,
      '#default_value' => implode(PHP_EOL, (array) $config
        ->get('whitelisted_domains')),
      '#description' => $this
        ->t('Enter a line-separated list of whitelisted domains (ie "example.com").'),
    ];
    $patterns = [
      '#theme' => 'item_list',
      '#items' => [
        [
          '#markup' => '<code>(example\\.com)</code> ' . $this
            ->t('Matches example.com.'),
        ],
        [
          '#markup' => '<code>(example\\.com)|(example\\.net)</code> ' . $this
            ->t('Multiple patterns can be strung together by using a pipe. Matches example.com OR example.net.'),
        ],
        [
          '#markup' => '<code>(links/goto/[0-9]+/[0-9]+)</code> ' . $this
            ->t('Matches links that go through the <a target="Links-module" href="http://drupal.org/project/links">Links module</a> redirect.'),
        ],
      ],
    ];
    $wildcards = [
      '#theme' => 'item_list',
      '#items' => [
        [
          '#markup' => '<code>.</code> ' . $this
            ->t('Matches any character.'),
        ],
        [
          '#markup' => '<code>?</code> ' . $this
            ->t('The previous character or set is optional.'),
        ],
        [
          '#markup' => '<code>\\d</code> ' . $this
            ->t('Matches any digit (0-9).'),
        ],
        [
          '#markup' => '<code>[a-z]</code> ' . $this
            ->t('Brackets may be used to match a custom set of characters. This matches any alphabetic letter.'),
        ],
      ],
    ];
    $form['patterns'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Pattern matching.'),
      '#description' => '<p>' . $this
        ->t('External links uses patterns (regular expressions) to match the "href" property of links.') . '</p>' . $this
        ->t('Here are some common patterns.') . $renderer
        ->render($patterns) . $this
        ->t('Common special characters:') . $renderer
        ->render($wildcards) . '<p>' . $this
        ->t('All special characters (<code>@characters</code>) must also be escaped with backslashes. Patterns are not case-sensitive. Any <a target="pattern supported by JavaScript" href="http://www.javascriptkit.com/javatutors/redev2.shtml">pattern supported by JavaScript</a> may be used.', [
        '@characters' => '^ $ . ? ( ) | * +',
      ]) . '</p>',
      '#open' => FALSE,
    ];
    $form['patterns']['extlink_exclude'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Exclude links matching the pattern.'),
      '#maxlength' => NULL,
      '#default_value' => $config
        ->get('extlink_exclude'),
      '#description' => $this
        ->t('Enter a regular expression for links that you wish to exclude from being considered external.'),
    ];
    $form['patterns']['extlink_include'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Include links matching the pattern.'),
      '#maxlength' => NULL,
      '#default_value' => $config
        ->get('extlink_include'),
      '#description' => $this
        ->t('Enter a regular expression for internal links that you wish to be considered external.'),
    ];
    $form['css_matching'] = [
      '#tree' => FALSE,
      '#type' => 'fieldset',
      '#title' => $this
        ->t('CSS Matching.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#description' => '<p>' . $this
        ->t('Use CSS selectors to exclude entirely or only look inside explicitly specified classes and IDs for external links.  These will be passed straight to jQuery for matching.') . '</p>',
    ];
    $form['css_matching']['extlink_css_exclude'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Exclude links inside these CSS selectors.'),
      '#maxlength' => NULL,
      '#default_value' => $config
        ->get('extlink_css_exclude', ''),
      '#description' => $this
        ->t('Enter a comma-separated list of CSS selectors (ie "#block-block-2 .content, ul.menu").'),
    ];
    $form['css_matching']['extlink_css_explicit'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Only look for links inside these CSS selectors.'),
      '#maxlength' => NULL,
      '#default_value' => $config
        ->get('extlink_css_explicit', ''),
      '#description' => $this
        ->t('Enter a comma-separated list of CSS selectors (ie "#block-block-2 .content, ul.menu").'),
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $whitelisted_domains = explode(PHP_EOL, $values['whitelisted_domains']);
    $whitelisted_domains = array_map('trim', $whitelisted_domains);
    $whitelisted_domains = array_filter($whitelisted_domains, function ($value) {
      return !empty($value);
    });
    $this
      ->config('extlink.settings')
      ->set('extlink_use_external_js_file', $values['extlink_use_external_js_file'])
      ->set('extlink_exclude_admin_routes', $values['extlink_exclude_admin_routes'])
      ->set('extlink_include', $values['extlink_include'])
      ->set('extlink_exclude', $values['extlink_exclude'])
      ->set('extlink_alert_text', $values['extlink_alert_text'])
      ->set('extlink_alert', $values['extlink_alert'])
      ->set('extlink_target', $values['extlink_target'])
      ->set('extlink_target_no_override', $values['extlink_target_no_override'])
      ->set('extlink_nofollow', $values['extlink_nofollow'])
      ->set('extlink_noreferrer', $values['extlink_noreferrer'])
      ->set('extlink_follow_no_override', $values['extlink_follow_no_override'])
      ->set('extlink_subdomains', $values['extlink_subdomains'])
      ->set('extlink_mailto_class', $values['extlink_mailto_class'])
      ->set('extlink_img_class', $values['extlink_img_class'])
      ->set('extlink_class', $values['extlink_class'])
      ->set('extlink_css_exclude', $values['extlink_css_exclude'])
      ->set('extlink_css_explicit', $values['extlink_css_explicit'])
      ->set('extlink_use_font_awesome', $values['extlink_use_font_awesome'])
      ->set('extlink_icon_placement', $values['extlink_icon_placement'])
      ->set('extlink_use_font_awesome', $values['extlink_use_font_awesome'])
      ->set('extlink_font_awesome_classes.links', $values['extlink_font_awesome_classes']['links'])
      ->set('extlink_font_awesome_classes.mailto', $values['extlink_font_awesome_classes']['mailto'])
      ->set('whitelisted_domains', $whitelisted_domains)
      ->save();
    parent::submitForm($form, $form_state);
  }

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

}

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
ExtlinkAdminSettingsForm::$renderer protected property Drupal\Core\Render\Renderer definition.
ExtlinkAdminSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
ExtlinkAdminSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
ExtlinkAdminSettingsForm::getEditableConfigNames public function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
ExtlinkAdminSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ExtlinkAdminSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
ExtlinkAdminSettingsForm::__construct public function Class constructor. 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.