You are here

class HttpFetcherFeedForm in Feeds 8.3

Provides a form on the feed edit page for the HttpFetcher.

Hierarchy

Expanded class hierarchy of HttpFetcherFeedForm

File

src/Feeds/Fetcher/Form/HttpFetcherFeedForm.php, line 17

Namespace

Drupal\feeds\Feeds\Fetcher\Form
View source
class HttpFetcherFeedForm extends ExternalPluginFormBase implements ContainerInjectionInterface {

  /**
   * The Guzzle client.
   *
   * @var \GuzzleHttp\ClientInterface
   */
  protected $client;

  /**
   * Constructs an HttpFeedForm object.
   *
   * @param \GuzzleHttp\ClientInterface $client
   *   The HTTP client.
   */
  public function __construct(ClientInterface $client) {
    $this->client = $client;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('http_client'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state, FeedInterface $feed = NULL) {
    $form['source'] = [
      '#title' => $this
        ->t('Feed URL'),
      '#type' => 'url',
      '#default_value' => $feed
        ->getSource(),
      '#maxlength' => 2048,
      '#required' => TRUE,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state, FeedInterface $feed = NULL) {
    try {
      $url = Feed::translateSchemes($form_state
        ->getValue('source'));
    } catch (\InvalidArgumentException $e) {
      $form_state
        ->setError($form['source'], $this
        ->t("The url's scheme is not supported. Supported schemes are: @supported.", [
        '@supported' => implode(', ', Feed::getSupportedSchemes()),
      ]));

      // If the source doesn't have a valid scheme the rest of the validation
      // isn't helpful. Break out early.
      return;
    }
    $form_state
      ->setValue('source', $url);
    if (!$this->plugin
      ->getConfiguration('auto_detect_feeds')) {
      return;
    }
    try {
      $response = $this->client
        ->get($url);
    } catch (RequestException $e) {
      $args = [
        '%site' => $url,
        '%error' => $e
          ->getMessage(),
      ];
      $form_state
        ->setError($form['source'], $this
        ->t('The feed from %site seems to be broken because of error "%error".', $args));
      return;
    }
    if ($url = Feed::getCommonSyndication($form_state
      ->getValue('source'), (string) $response
      ->getBody())) {
      $form_state
        ->setValue('source', $url);
    }
    else {
      $form_state
        ->setError($form['source'], $this
        ->t('Invalid feed URL.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state, FeedInterface $feed = NULL) {
    $feed
      ->setSource($form_state
      ->getValue('source'));
  }

  /**
   * Performs a GET request.
   *
   * @param string $url
   *   The URL to GET.
   *
   * @return \Guzzle\Http\Message\Response
   *   A Guzzle response.
   *
   * @throws \RuntimeException
   *   Thrown if the GET request failed.
   */
  protected function get($url) {
    try {
      $response = $this->client
        ->get(Feed::translateSchemes($url));
    } catch (RequestException $e) {
      $args = [
        '%site' => $url,
        '%error' => $e
          ->getMessage(),
      ];
      throw new \RuntimeException($this
        ->t('The feed from %site seems to be broken because of error "%error".', $args));
    }
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
ExternalPluginFormBase::$plugin protected property The Feeds plugin.
ExternalPluginFormBase::setPlugin public function Sets the plugin for this object. Overrides PluginAwareInterface::setPlugin
HttpFetcherFeedForm::$client protected property The Guzzle client.
HttpFetcherFeedForm::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
HttpFetcherFeedForm::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
HttpFetcherFeedForm::get protected function Performs a GET request.
HttpFetcherFeedForm::submitConfigurationForm public function Form submission handler. Overrides ExternalPluginFormBase::submitConfigurationForm
HttpFetcherFeedForm::validateConfigurationForm public function Form validation handler. Overrides ExternalPluginFormBase::validateConfigurationForm
HttpFetcherFeedForm::__construct public function Constructs an HttpFeedForm object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.