public function OpmlFeedAdd::submitForm

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

array $form_state: An associative array containing the current state of the form.

Overrides FormInterface::submitForm

File

drupal/core/modules/aggregator/lib/Drupal/aggregator/Form/OpmlFeedAdd.php, line 156
Contains \Drupal\aggregator\Form\OpmlFeedAdd.

Class

OpmlFeedAdd
Imports feeds from OPML.

Namespace

Drupal\aggregator\Form

Code

public function submitForm(array &$form, array &$form_state) {
  $data = '';
  $validators = array(
    'file_validate_extensions' => array(
      'opml xml',
    ),
  );
  if ($file = file_save_upload('upload', $validators, FALSE, 0)) {
    $data = file_get_contents($file->uri);
  }
  else {

    // @todo Move this to a fetcher implementation.
    try {
      $response = $this->httpClient
        ->get($form_state['values']['remote'])
        ->send();
      $data = $response
        ->getBody(TRUE);
    } catch (BadResponseException $e) {
      $response = $e
        ->getResponse();
      watchdog('aggregator', 'Failed to download OPML file due to "%error".', array(
        '%error' => $response
          ->getStatusCode() . ' ' . $response
          ->getReasonPhrase(),
      ), WATCHDOG_WARNING);
      drupal_set_message(t('Failed to download OPML file due to "%error".', array(
        '%error' => $response
          ->getStatusCode() . ' ' . $response
          ->getReasonPhrase(),
      )));
      return;
    } catch (RequestException $e) {
      watchdog('aggregator', 'Failed to download OPML file due to "%error".', array(
        '%error' => $e
          ->getMessage(),
      ), WATCHDOG_WARNING);
      drupal_set_message(t('Failed to download OPML file due to "%error".', array(
        '%error' => $e
          ->getMessage(),
      )));
      return;
    }
  }
  $feeds = $this
    ->parseOpml($data);
  if (empty($feeds)) {
    drupal_set_message(t('No new feed has been added.'));
    return;
  }

  // @todo Move this functionality to a processor.
  foreach ($feeds as $feed) {

    // Ensure URL is valid.
    if (!valid_url($feed['url'], TRUE)) {
      drupal_set_message(t('The URL %url is invalid.', array(
        '%url' => $feed['url'],
      )), 'warning');
      continue;
    }

    // Check for duplicate titles or URLs.
    $query = $this->queryFactory
      ->get('aggregator_feed');
    $condition = $query
      ->orConditionGroup()
      ->condition('title', $feed['title'])
      ->condition('url', $feed['url']);
    $ids = $query
      ->condition($condition)
      ->execute();
    $result = $this->entityManager
      ->getStorageController('aggregator_feed')
      ->load($ids);
    foreach ($result as $old) {
      if (strcasecmp($old
        ->label(), $feed['title']) == 0) {
        drupal_set_message(t('A feed named %title already exists.', array(
          '%title' => $old
            ->label(),
        )), 'warning');
        continue 2;
      }
      if (strcasecmp($old->url->value, $feed['url']) == 0) {
        drupal_set_message(t('A feed with the URL %url already exists.', array(
          '%url' => $old->url->value,
        )), 'warning');
        continue 2;
      }
    }
    $new_feed = $this->entityManager
      ->getStorageController('aggregator_feed')
      ->create(array(
      'title' => $feed['title'],
      'url' => $feed['url'],
      'refresh' => $form_state['values']['refresh'],
      'block' => $form_state['values']['block'],
    ));
    $new_feed->categories = $form_state['values']['category'];
    $new_feed
      ->save();
  }
  $form_state['redirect'] = 'admin/config/services/aggregator';
}