You are here

public function SoundcloudForm::validateSoundcloudUrl in Media entity Soundcloud 3.x

Same name and namespace in other branches
  1. 8.2 src/Form/SoundcloudForm.php \Drupal\media_entity_soundcloud\Form\SoundcloudForm::validateSoundcloudUrl()

Validates a soundcloud url.

Parameters

array $form: The form.

FormStateInterface $form_state: The form state.

Return value

void|TRUE

File

src/Form/SoundcloudForm.php, line 114

Class

SoundcloudForm

Namespace

Drupal\media_entity_soundcloud\Form

Code

public function validateSoundcloudUrl(array &$form, FormStateInterface $form_state) {
  $url = $form_state
    ->getValue('soundcloud_url');
  if (preg_match('/https?:\\/\\/(www\\.)?soundcloud\\.com\\/.*/', $url)) {
    try {
      \Drupal::httpClient()
        ->get($url);
    } catch (ClientException $e) {
      $form_state
        ->setErrorByName('url', $this
        ->t('Could not connect to the track, please make sure that the url is correct.'));
    }
  }
  else {
    $form_state
      ->setErrorByName('url', $this
      ->t('Invalid url.'));
  }
}