/** * {@inheritdoc} */ public function blockForm($form, FormStateInterface $form_state) { $feeds = $this->feedStorage->loadMultiple(); $options = array(); foreach ($feeds as $feed) { $options[$feed->id()] = $feed->label(); } $form['feed'] = array('#type' => 'select', '#title' => t('Select the feed that should be displayed'), '#default_value' => $this->configuration['feed'], '#options' => $options); $range = range(2, 20); $form['block_count'] = array('#type' => 'select', '#title' => t('Number of news items in block'), '#default_value' => $this->configuration['block_count'], '#options' => array_combine($range, $range)); return $form; }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $validators = array('file_validate_extensions' => array('opml xml')); if ($file = file_save_upload('upload', $validators, FALSE, 0)) { $data = file_get_contents($file->getFileUri()); } else { // @todo Move this to a fetcher implementation. try { $response = $this->httpClient->get($form_state->getValue('remote')); $data = (string) $response->getBody(); } catch (RequestException $e) { $this->logger('aggregator')->warning('Failed to download OPML file due to "%error".', array('%error' => $e->getMessage())); drupal_set_message($this->t('Failed to download OPML file due to "%error".', array('%error' => $e->getMessage()))); return; } } $feeds = $this->parseOpml($data); if (empty($feeds)) { drupal_set_message($this->t('No new feed has been added.')); return; } // @todo Move this functionality to a processor. foreach ($feeds as $feed) { // Ensure URL is valid. if (!UrlHelper::isValid($feed['url'], TRUE)) { drupal_set_message($this->t('The URL %url is invalid.', array('%url' => $feed['url'])), 'warning'); continue; } // Check for duplicate titles or URLs. $query = $this->feedStorage->getQuery(); $condition = $query->orConditionGroup()->condition('title', $feed['title'])->condition('url', $feed['url']); $ids = $query->condition($condition)->execute(); $result = $this->feedStorage->loadMultiple($ids); foreach ($result as $old) { if (strcasecmp($old->label(), $feed['title']) == 0) { drupal_set_message($this->t('A feed named %title already exists.', array('%title' => $old->label())), 'warning'); continue 2; } if (strcasecmp($old->getUrl(), $feed['url']) == 0) { drupal_set_message($this->t('A feed with the URL %url already exists.', array('%url' => $old->getUrl())), 'warning'); continue 2; } } $new_feed = $this->feedStorage->create(array('title' => $feed['title'], 'url' => $feed['url'], 'refresh' => $form_state->getValue('refresh'))); $new_feed->save(); } $form_state->setRedirect('aggregator.admin_overview'); }