public OpmlFeedAdd::submitForm(array &$form, FormStateInterface $form_state)
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 111
Class
- OpmlFeedAdd
- Imports feeds from OPML.
Namespace
Drupal\aggregator\Form
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 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' ); } |
Please login to continue.