input = $configuration['input']; $this->videoId = $this->getIdFromInput($configuration['input']); $this->httpClient = $http_client; } /** * Get the ID of the video. * * @return string * The video ID. */ protected function getVideoId() { return $this->videoId; } /** * Get the input which caused this plugin to be selected. * * @return string * The raw input from the user. */ protected function getInput() { return $this->input; } /** * {@inheritdoc} */ public static function isApplicable($input) { $id = static::getIdFromInput($input); return !empty($id); } /** * {@inheritdoc} */ public function renderThumbnail($image_style, $link_url) { $output = [ '#theme' => 'image', '#uri' => $this->getLocalThumbnailUri(), ]; if (!empty($image_style)) { $output['#theme'] = 'image_style'; $output['#style_name'] = $image_style; } if ($link_url) { $output = [ '#type' => 'link', '#title' => $output, '#url' => $link_url, ]; } return $output; } /** * {@inheritdoc} */ public function downloadThumbnail() { $local_uri = $this->getLocalThumbnailUri(); if (!file_exists($local_uri)) { file_prepare_directory($this->thumbsDirectory, FILE_CREATE_DIRECTORY); try { $thumbnail = $this->httpClient->request('GET', $this->getRemoteThumbnailUrl()); file_unmanaged_save_data((string) $thumbnail->getBody(), $local_uri); } catch (\Exception $e) { } } } /** * {@inheritdoc} */ public function getLocalThumbnailUri() { return $this->thumbsDirectory . '/' . $this->getVideoId() . '.jpg'; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static($configuration, $plugin_id, $plugin_definition, $container->get('http_client')); } /** * {@inheritdoc} */ public function getName() { return $this->t('@provider Video (@id)', ['@provider' => $this->getPluginDefinition()['title'], '@id' => $this->getVideoId()]); } }