You are here

public function YouTubeDefaultWidget::validateInput in YouTube Field 8

Validate video URL.

File

src/Plugin/Field/FieldWidget/YouTubeDefaultWidget.php, line 96

Class

YouTubeDefaultWidget
Plugin implementation of the 'youtube_default' widget.

Namespace

Drupal\youtube\Plugin\Field\FieldWidget

Code

public function validateInput(&$element, FormStateInterface &$form_state, $form) {
  $input = $element['#value'];
  $video_id = youtube_get_video_id($input);
  if ($video_id && strlen($video_id) <= 20) {
    $video_id_element = [
      '#parents' => $element['#parents'],
    ];
    array_pop($video_id_element['#parents']);
    $video_id_element['#parents'][] = 'video_id';
    $form_state
      ->setValueForElement($video_id_element, $video_id);
  }
  elseif (!empty($input)) {
    $form_state
      ->setError($element, $this
      ->t('Please provide a valid YouTube URL.'));
  }
}