function OpmlFeedAdd::submitForm
Same name in other branches
- 9 core/modules/aggregator/src/Form/OpmlFeedAdd.php \Drupal\aggregator\Form\OpmlFeedAdd::submitForm()
Overrides FormInterface::submitForm
File
-
core/
modules/ aggregator/ src/ Form/ OpmlFeedAdd.php, line 114
Class
- OpmlFeedAdd
- Imports feeds from OPML.
Namespace
Drupal\aggregator\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
$validators = [
'file_validate_extensions' => [
'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".', [
'%error' => $e->getMessage(),
]);
$this->messenger()
->addStatus($this->t('Failed to download OPML file due to "%error".', [
'%error' => $e->getMessage(),
]));
return;
}
}
$feeds = $this->parseOpml($data);
if (empty($feeds)) {
$this->messenger()
->addStatus($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)) {
$this->messenger()
->addWarning($this->t('The URL %url is invalid.', [
'%url' => $feed['url'],
]));
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) {
$this->messenger()
->addWarning($this->t('A feed named %title already exists.', [
'%title' => $old->label(),
]));
continue 2;
}
if (strcasecmp($old->getUrl(), $feed['url']) == 0) {
$this->messenger()
->addWarning($this->t('A feed with the URL %url already exists.', [
'%url' => $old->getUrl(),
]));
continue 2;
}
}
$new_feed = $this->feedStorage
->create([
'title' => $feed['title'],
'url' => $feed['url'],
'refresh' => $form_state->getValue('refresh'),
]);
$new_feed->save();
}
$form_state->setRedirect('aggregator.admin_overview');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.