You are here

public static function EditorIconDialog::validateIconName in Font Awesome Icons 8.2

Validate the Font Awesome icon name.

File

src/Form/EditorIconDialog.php, line 371

Class

EditorIconDialog
Provides a Font Awesome icon dialog for text editors.

Namespace

Drupal\fontawesome\Form

Code

public static function validateIconName($element, FormStateInterface $form_state) {

  // Load the configuration settings.
  $configuration_settings = \Drupal::config('fontawesome.settings');

  // Check if we need to bypass.
  if ($configuration_settings
    ->get('bypass_validation')) {
    return;
  }
  $value = $element['#value'];
  if (strlen($value) == 0) {
    $form_state
      ->setValueForElement($element, '');
    return;
  }

  // Remove the prefix if the user accidentally added it.
  if (substr($value, 0, 3) == 'fa-') {
    $value = substr($value, 3);
  }

  // Load the icon data so we can check for a valid icon.
  $iconData = \Drupal::service('fontawesome.font_awesome_manager')
    ->getIconMetadata($value);
  if (!isset($iconData['name'])) {
    $form_state
      ->setError($element, t("Invalid icon name %value. Please see @iconLink for correct icon names.", [
      '%value' => $value,
      '@iconLink' => Link::fromTextAndUrl(t('the Font Awesome icon list'), Url::fromUri('https://fontawesome.com/icons'))
        ->toString(),
    ]));
  }
}