You are here

function _syntaxhighlighter_validate_input in Syntax Highlighter 8

Same name and namespace in other branches
  1. 6.2 syntaxhighlighter.module \_syntaxhighlighter_validate_input()
  2. 6 syntaxhighlighter.module \_syntaxhighlighter_validate_input()
  3. 7.2 syntaxhighlighter.module \_syntaxhighlighter_validate_input()
  4. 7 syntaxhighlighter.module \_syntaxhighlighter_validate_input()

Validate the input form text.

Parameters

\Drupal\Core\Form\FormStateInterface $form_state: The form being validated.

string $field: The input field being checked, used in the error message if any error is found.

string $text: The input text to validate.

2 calls to _syntaxhighlighter_validate_input()
_syntaxhighlighter_comment_validate in ./syntaxhighlighter.module
Validate the comment input text to be sure that there are no bad tags.
_syntaxhighlighter_node_validate in ./syntaxhighlighter.module
Validate the node input text to be sure that there are no bad tags.

File

./syntaxhighlighter.module, line 321
Syntax highlight code using the SyntaxHighlighter Javascript library.

Code

function _syntaxhighlighter_validate_input(FormStateInterface $form_state, $field, $text) {
  $config = \Drupal::config('syntaxhighlighter.settings');
  $tag_name = $config
    ->get('tagname');

  // Check that the number of open and close tags is the same.
  preg_match_all("#<{$tag_name}\\s*[^>]*>#", $text, $matches_open);
  preg_match_all("#</\\s*{$tag_name}\\s*>#", $text, $matches_close);
  if (count($matches_open[0]) != count($matches_close[0])) {
    $error = t('Unbalanced @tag tags.', [
      '@tag' => Markup::create("<{$tag_name}>"),
    ]);
    $form_state
      ->setErrorByName($field, (string) $error);
  }
  else {

    // If they are, make sure that the tags are also in the proper order.
    preg_match_all("#<{$tag_name}\\s*[^>]*>.*</\\s*{$tag_name}\\s*>#sU", $text, $matches_pair);
    if (count($matches_pair[0]) != count($matches_open[0]) || count($matches_pair[0]) != count($matches_close[0])) {
      $error = t('@tag tags in the wrong order (e.g. unbalanced or nested).', [
        '@tag' => Markup::create("<{$tag_name}>"),
      ]);
      $form_state
        ->setErrorByName($field, (string) $error);
    }
  }
}