class OpmlFeedAdd in Drupal 9
Same name and namespace in other branches
- 8 core/modules/aggregator/src/Form/OpmlFeedAdd.php \Drupal\aggregator\Form\OpmlFeedAdd
Imports feeds from OPML.
@internal
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\aggregator\Form\OpmlFeedAdd
Expanded class hierarchy of OpmlFeedAdd
1 string reference to 'OpmlFeedAdd'
- aggregator.routing.yml in core/
modules/ aggregator/ aggregator.routing.yml - core/modules/aggregator/aggregator.routing.yml
File
- core/
modules/ aggregator/ src/ Form/ OpmlFeedAdd.php, line 19
Namespace
Drupal\aggregator\FormView source
class OpmlFeedAdd extends FormBase {
/**
* The feed storage.
*
* @var \Drupal\aggregator\FeedStorageInterface
*/
protected $feedStorage;
/**
* The HTTP client to fetch the feed data with.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $httpClient;
/**
* Constructs a database object.
*
* @param \Drupal\aggregator\FeedStorageInterface $feed_storage
* The feed storage.
* @param \GuzzleHttp\ClientInterface $http_client
* The Guzzle HTTP client.
*/
public function __construct(FeedStorageInterface $feed_storage, ClientInterface $http_client) {
$this->feedStorage = $feed_storage;
$this->httpClient = $http_client;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager')
->getStorage('aggregator_feed'), $container
->get('http_client'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'aggregator_opml_add';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$intervals = [
900,
1800,
3600,
7200,
10800,
21600,
32400,
43200,
64800,
86400,
172800,
259200,
604800,
1209600,
2419200,
];
$period = array_map([
\Drupal::service('date.formatter'),
'formatInterval',
], array_combine($intervals, $intervals));
$form['upload'] = [
'#type' => 'file',
'#title' => $this
->t('OPML File'),
'#description' => $this
->t('Upload an OPML file containing a list of feeds to be imported.'),
];
$form['remote'] = [
'#type' => 'url',
'#title' => $this
->t('OPML Remote URL'),
'#maxlength' => 1024,
'#description' => $this
->t('Enter the URL of an OPML file. This file will be downloaded and processed only once on submission of the form.'),
];
$form['refresh'] = [
'#type' => 'select',
'#title' => $this
->t('Update interval'),
'#default_value' => 3600,
'#options' => $period,
'#description' => $this
->t('The length of time between feed updates. Requires a correctly configured <a href=":cron">cron maintenance task</a>.', [
':cron' => Url::fromRoute('system.status')
->toString(),
]),
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Import'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// If both fields are empty or filled, cancel.
$all_files = $this
->getRequest()->files
->get('files', []);
if ($form_state
->isValueEmpty('remote') == empty($all_files['upload'])) {
$form_state
->setErrorByName('remote', $this
->t('<em>Either</em> upload a file or enter a URL.'));
}
}
/**
* {@inheritdoc}
*/
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 (TransferException $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()
->accessCheck(FALSE);
$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');
}
/**
* Parses an OPML file.
*
* Feeds are recognized as <outline> elements with the attributes "text" and
* "xmlurl" set.
*
* @param string $opml
* The complete contents of an OPML document.
*
* @return array
* An array of feeds, each an associative array with a "title" and a "url"
* element, or NULL if the OPML document failed to be parsed. An empty array
* will be returned if the document is valid but contains no feeds, as some
* OPML documents do.
*
* @todo Move this to a parser in https://www.drupal.org/node/1963540.
*/
protected function parseOpml($opml) {
$feeds = [];
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'utf-8');
if (xml_parse_into_struct($xml_parser, $opml, $values)) {
foreach ($values as $entry) {
if ($entry['tag'] == 'OUTLINE' && isset($entry['attributes'])) {
$item = $entry['attributes'];
if (!empty($item['XMLURL']) && !empty($item['TEXT'])) {
$feeds[] = [
'title' => $item['TEXT'],
'url' => $item['XMLURL'],
];
}
}
}
}
xml_parser_free($xml_parser);
return $feeds;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function | Returns a redirect response object for the specified route. | |
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
OpmlFeedAdd:: |
protected | property | The feed storage. | |
OpmlFeedAdd:: |
protected | property | The HTTP client to fetch the feed data with. | |
OpmlFeedAdd:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
OpmlFeedAdd:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
OpmlFeedAdd:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
OpmlFeedAdd:: |
protected | function | Parses an OPML file. | |
OpmlFeedAdd:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
OpmlFeedAdd:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
OpmlFeedAdd:: |
public | function | Constructs a database object. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |