diff --git a/src/Plugin/Validation/Constraint/TweetVisibleConstraintValidator.php b/src/Plugin/Validation/Constraint/TweetVisibleConstraintValidator.php index ccf13fe..81967da 100644 --- a/src/Plugin/Validation/Constraint/TweetVisibleConstraintValidator.php +++ b/src/Plugin/Validation/Constraint/TweetVisibleConstraintValidator.php @@ -3,9 +3,11 @@ namespace Drupal\media_entity_twitter\Plugin\Validation\Constraint; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; +use Drupal\Core\Field\FieldItemListInterface; use Drupal\media_entity_twitter\Plugin\media\Source\Twitter; use Drupal\Core\Field\FieldItemInterface; use GuzzleHttp\Client; +use GuzzleHttp\Exception\ClientException; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; @@ -47,7 +49,7 @@ class TweetVisibleConstraintValidator extends ConstraintValidator implements Con if (is_string($value)) { $data = $value; } - elseif ($value instanceof FieldItemList) { + elseif ($value instanceof FieldItemListInterface) { $fieldtype = $value->getFieldDefinition()->getType(); $field_value = $value->getValue(); if ($fieldtype == 'link') { @@ -76,7 +78,13 @@ class TweetVisibleConstraintValidator extends ConstraintValidator implements Con } // Fetch content from the given url. - $response = $this->httpClient->get($matches[0][0], ['allow_redirects' => FALSE]); + try { + $response = $this->httpClient->get($matches[0][0], ['allow_redirects' => FALSE]); + } + catch (ClientException $e) { + $this->context->addViolation($constraint->message); + return; + } if ($response->getStatusCode() == 302 && ($location = $response->getHeader('location'))) { $effective_url_parts = parse_url($location[0]); diff --git a/tests/src/Functional/TweetEmbedFormatterTest.php b/tests/src/Functional/TweetEmbedFormatterTest.php index ebbc13b..9f07028 100644 --- a/tests/src/Functional/TweetEmbedFormatterTest.php +++ b/tests/src/Functional/TweetEmbedFormatterTest.php @@ -107,7 +107,7 @@ class TweetEmbedFormatterTest extends MediaFunctionalTestBase { $this->drupalGet('media/add/' . $bundle->id()); // Random image url from twitter. - $tweet_url = 'https://twitter.com/RamzyStinson/status/670650348319576064'; + $tweet_url = 'https://twitter.com/DrupalConEur/status/1176518741208817664'; // Random image from twitter. $tweet = '

' . @@ -130,7 +130,7 @@ class TweetEmbedFormatterTest extends MediaFunctionalTestBase { // Assert that the link url formatter exists on this page. $this->assertText('Tweet URL'); - $this->assertRaw('', 'Link in embedded Tweet found.'); + $this->assertRaw('', 'Link in embedded Tweet found.'); // Assert that the string_long code formatter exists on this page. $this->assertText('Embed code');