Website.php in Social Media Links Block and Field 8.2
File
src/Plugin/SocialMediaLinks/Platform/Website.php
View source
<?php
namespace Drupal\social_media_links\Plugin\SocialMediaLinks\Platform;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\social_media_links\PlatformBase;
class Website extends PlatformBase {
public static function validateValue(array &$element, FormStateInterface $form_state, array $form) {
if (!empty($element['#value'])) {
$default_protocol = 'http://';
$new_value = $element['#value'];
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'));
}
}
}
}