View source  
  <?php
namespace Drupal\webform_attachment\Plugin\WebformElement;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Form\FormStateInterface;
use GuzzleHttp\Exception\ClientException;
class WebformAttachmentUrl extends WebformAttachmentBase {
  
  protected function defineDefaultProperties() {
    return [
      'url' => '',
    ] + parent::defineDefaultProperties();
  }
  
  protected function defineTranslatableProperties() {
    return array_merge(parent::defineTranslatableProperties(), [
      'url',
    ]);
  }
  
  
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
    $form['attachment']['url'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('URL/path'),
      '#description' => $this
        ->t("Make sure the attachment URL/Path is publicly accessible. The attachment's URL/path will never be displayed to end users."),
      '#required' => TRUE,
      '#element_validate' => [
        [
          get_class($this),
          'validateAttachmentUrl',
        ],
      ],
    ];
    if (function_exists('imce_process_url_element')) {
      $url_element =& $form['attachment']['url'];
      imce_process_url_element($url_element, 'link');
      $form['#attached']['library'][] = 'webform/imce.input';
    }
    return $form;
  }
  
  public static function validateAttachmentUrl(&$element, FormStateInterface &$form_state) {
    $value = trim($element['#value']);
    $form_state
      ->setValueForElement($element, $value);
    
    if (strpos($value, '/') === 0) {
      $value = \Drupal::request()
        ->getSchemeAndHttpHost() . $value;
    }
    
    if (strpos($value, '[webform_submission:') !== FALSE) {
      return;
    }
    
    if ($value !== '' && !UrlHelper::isValid($value, TRUE)) {
      $form_state
        ->setError($element, t('The URL %url is not valid.', [
        '%url' => $value,
      ]));
    }
    
    try {
      \Drupal::httpClient()
        ->head($value);
    } catch (ClientException $e) {
      $form_state
        ->setError($element, t('The URL <a href=":url">@url</a> is not available.', [
        ':url' => $value,
        '@url' => $value,
      ]));
    }
  }
}