public function OpmlFeedAdd::submitForm in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/aggregator/src/Form/OpmlFeedAdd.php \Drupal\aggregator\Form\OpmlFeedAdd::submitForm()
Form submission handler.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Overrides FormInterface::submitForm
File
- core/
modules/ aggregator/ src/ Form/ OpmlFeedAdd.php, line 116 - Contains \Drupal\aggregator\Form\OpmlFeedAdd.
Class
- OpmlFeedAdd
- Imports feeds from OPML.
Namespace
Drupal\aggregator\FormCode
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');
}