You are here

public static function Website::validateValue in Social Media Links Block and Field 8.2

Validates the user input of a platform before the value is saved.

Return value

mixed The result of the validation.

Overrides PlatformBase::validateValue

File

src/Plugin/SocialMediaLinks/Platform/Website.php, line 22

Class

Website
Provides 'website' platform.

Namespace

Drupal\social_media_links\Plugin\SocialMediaLinks\Platform

Code

public static function validateValue(array &$element, FormStateInterface $form_state, array $form) {
  if (!empty($element['#value'])) {
    $default_protocol = 'http://';
    $new_value = $element['#value'];

    // Append the default protocol in case the user didn't add it.
    if (!preg_match('/^http(s)?:\\/\\//', $new_value)) {
      $new_value = $default_protocol . $new_value;
      $form_state
        ->setValueForElement($element, $new_value);
    }
    if (!UrlHelper::isValid($new_value, TRUE)) {
      $form_state
        ->setError($element, t('The website must be a valid URL'));
    }
  }
}