annotate core/modules/aggregator/aggregator.module @ 2:5311817fb629

Theme updates
author Chris Cannam
date Tue, 10 Jul 2018 13:19:18 +0000
parents c75dbcec494b
children 12f9dff5fda9
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * Used to aggregate syndicated content (RSS, RDF, and Atom).
Chris@0 6 */
Chris@0 7
Chris@0 8 use Drupal\aggregator\Entity\Feed;
Chris@0 9 use Drupal\Core\Routing\RouteMatchInterface;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Denotes that a feed's items should never expire.
Chris@0 13 *
Chris@0 14 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
Chris@0 15 * Use \Drupal\aggregator\FeedStorageInterface::CLEAR_NEVER instead.
Chris@0 16 *
Chris@0 17 * @see https://www.drupal.org/node/2831620
Chris@0 18 */
Chris@0 19 const AGGREGATOR_CLEAR_NEVER = 0;
Chris@0 20
Chris@0 21 /**
Chris@0 22 * Implements hook_help().
Chris@0 23 */
Chris@0 24 function aggregator_help($route_name, RouteMatchInterface $route_match) {
Chris@0 25 switch ($route_name) {
Chris@0 26 case 'help.page.aggregator':
Chris@0 27 $path_validator = \Drupal::pathValidator();
Chris@0 28 $output = '';
Chris@0 29 $output .= '<h3>' . t('About') . '</h3>';
Chris@0 30 $output .= '<p>' . t('The Aggregator module is an on-site syndicator and news reader that gathers and displays fresh content from RSS-, RDF-, and Atom-based feeds made available across the web. Thousands of sites (particularly news sites and blogs) publish their latest headlines in feeds, using a number of standardized XML-based formats. For more information, see the <a href=":aggregator-module">online documentation for the Aggregator module</a>.', [':aggregator-module' => 'https://www.drupal.org/documentation/modules/aggregator']) . '</p>';
Chris@0 31 $output .= '<h3>' . t('Uses') . '</h3>';
Chris@0 32 $output .= '<dl>';
Chris@0 33 // Check if the aggregator sources View is enabled.
Chris@0 34 if ($url = $path_validator->getUrlIfValid('aggregator/sources')) {
Chris@0 35 $output .= '<dt>' . t('Viewing feeds') . '</dt>';
Chris@0 36 $output .= '<dd>' . t('Users view feed content in the <a href=":aggregator">main aggregator display</a>, or by <a href=":aggregator-sources">their source</a> (usually via an RSS feed reader). The most recent content in a feed can be displayed as a block through the <a href=":admin-block">Blocks administration page</a>.', [':aggregator' => \Drupal::url('aggregator.page_last'), ':aggregator-sources' => $url->toString(), ':admin-block' => (\Drupal::moduleHandler()->moduleExists('block')) ? \Drupal::url('block.admin_display') : '#']) . '</dd>';
Chris@0 37 }
Chris@0 38 $output .= '<dt>' . t('Adding, editing, and deleting feeds') . '</dt>';
Chris@0 39 $output .= '<dd>' . t('Administrators can add, edit, and delete feeds, and choose how often to check each feed for newly updated items on the <a href=":feededit">Aggregator administration page</a>.', [':feededit' => \Drupal::url('aggregator.admin_overview')]) . '</dd>';
Chris@0 40 $output .= '<dt>' . t('Configuring the display of feed items') . '</dt>';
Chris@0 41 $output .= '<dd>' . t('Administrators can choose how many items are displayed in the listing pages, which HTML tags are allowed in the content of feed items, and whether they should be trimmed to a maximum number of characters on the <a href=":settings">Aggregator settings page</a>.', [':settings' => \Drupal::url('aggregator.admin_settings')]) . '</dd>';
Chris@0 42 $output .= '<dt>' . t('Discarding old feed items') . '</dt>';
Chris@0 43 $output .= '<dd>' . t('Administrators can choose whether to discard feed items that are older than a specified period of time on the <a href=":settings">Aggregator settings page</a>. This requires a correctly configured cron maintenance task (see below).', [':settings' => \Drupal::url('aggregator.admin_settings')]) . '<dd>';
Chris@0 44
Chris@0 45 $output .= '<dt>' . t('<abbr title="Outline Processor Markup Language">OPML</abbr> integration') . '</dt>';
Chris@0 46 // Check if the aggregator opml View is enabled.
Chris@0 47 if ($url = $path_validator->getUrlIfValid('aggregator/opml')) {
Chris@0 48 $output .= '<dd>' . t('A <a href=":aggregator-opml">machine-readable OPML file</a> of all feeds is available. OPML is an XML-based file format used to share outline-structured information such as a list of RSS feeds. Feeds can also be <a href=":import-opml">imported via an OPML file</a>.', [':aggregator-opml' => $url->toString(), ':import-opml' => \Drupal::url('aggregator.opml_add')]) . '</dd>';
Chris@0 49 }
Chris@0 50 $output .= '<dt>' . t('Configuring cron') . '</dt>';
Chris@0 51 $output .= '<dd>' . t('A working <a href=":cron">cron maintenance task</a> is required to update feeds automatically.', [':cron' => \Drupal::url('system.cron_settings')]) . '</dd>';
Chris@0 52 $output .= '</dl>';
Chris@0 53 return $output;
Chris@0 54
Chris@0 55 case 'aggregator.admin_overview':
Chris@0 56 // Don't use placeholders for possibility to change URLs for translators.
Chris@0 57 $output = '<p>' . t('Many sites publish their headlines and posts in feeds, using a number of standardized XML-based formats. The aggregator supports <a href="http://en.wikipedia.org/wiki/Rss">RSS</a>, <a href="http://en.wikipedia.org/wiki/Resource_Description_Framework">RDF</a>, and <a href="http://en.wikipedia.org/wiki/Atom_%28standard%29">Atom</a>.') . '</p>';
Chris@0 58 $output .= '<p>' . t('Current feeds are listed below, and <a href=":addfeed">new feeds may be added</a>. For each feed, the <em>latest items</em> block may be enabled at the <a href=":block">blocks administration page</a>.', [':addfeed' => \Drupal::url('aggregator.feed_add'), ':block' => (\Drupal::moduleHandler()->moduleExists('block')) ? \Drupal::url('block.admin_display') : '#']) . '</p>';
Chris@0 59 return $output;
Chris@0 60
Chris@0 61 case 'aggregator.feed_add':
Chris@0 62 return '<p>' . t('Add a feed in RSS, RDF or Atom format. A feed may only have one entry.') . '</p>';
Chris@0 63
Chris@0 64 case 'aggregator.opml_add':
Chris@0 65 return '<p>' . t('<abbr title="Outline Processor Markup Language">OPML</abbr> is an XML format for exchanging feeds between aggregators. A single OPML document may contain many feeds. Aggregator uses this file to import all feeds at once. Upload a file from your computer or enter a URL where the OPML file can be downloaded.') . '</p>';
Chris@0 66 }
Chris@0 67 }
Chris@0 68
Chris@0 69 /**
Chris@0 70 * Implements hook_theme().
Chris@0 71 */
Chris@0 72 function aggregator_theme() {
Chris@0 73 return [
Chris@0 74 'aggregator_feed' => [
Chris@0 75 'render element' => 'elements',
Chris@0 76 'file' => 'aggregator.theme.inc',
Chris@0 77 ],
Chris@0 78 'aggregator_item' => [
Chris@0 79 'render element' => 'elements',
Chris@0 80 'file' => 'aggregator.theme.inc',
Chris@0 81 ],
Chris@0 82 ];
Chris@0 83 }
Chris@0 84
Chris@0 85 /**
Chris@0 86 * Implements hook_entity_extra_field_info().
Chris@0 87 */
Chris@0 88 function aggregator_entity_extra_field_info() {
Chris@0 89 $extra = [];
Chris@0 90
Chris@0 91 $extra['aggregator_feed']['aggregator_feed'] = [
Chris@0 92 'display' => [
Chris@0 93 'items' => [
Chris@0 94 'label' => t('Items'),
Chris@0 95 'description' => t('Items associated with this feed'),
Chris@0 96 'weight' => 0,
Chris@0 97 ],
Chris@0 98 // @todo Move to a formatter at https://www.drupal.org/node/2339917.
Chris@0 99 'image' => [
Chris@0 100 'label' => t('Image'),
Chris@0 101 'description' => t('The feed image'),
Chris@0 102 'weight' => 2,
Chris@0 103 ],
Chris@0 104 // @todo Move to a formatter at https://www.drupal.org/node/2149845.
Chris@0 105 'description' => [
Chris@0 106 'label' => t('Description'),
Chris@0 107 'description' => t('The description of this feed'),
Chris@0 108 'weight' => 3,
Chris@0 109 ],
Chris@0 110 'more_link' => [
Chris@0 111 'label' => t('More link'),
Chris@0 112 'description' => t('A more link to the feed detail page'),
Chris@0 113 'weight' => 5,
Chris@0 114 ],
Chris@0 115 'feed_icon' => [
Chris@0 116 'label' => t('Feed icon'),
Chris@0 117 'description' => t('An icon that links to the feed URL'),
Chris@0 118 'weight' => 6,
Chris@0 119 ],
Chris@0 120 ],
Chris@0 121 ];
Chris@0 122
Chris@0 123 $extra['aggregator_item']['aggregator_item'] = [
Chris@0 124 'display' => [
Chris@0 125 // @todo Move to a formatter at https://www.drupal.org/node/2149845.
Chris@0 126 'description' => [
Chris@0 127 'label' => t('Description'),
Chris@0 128 'description' => t('The description of this feed item'),
Chris@0 129 'weight' => 2,
Chris@0 130 ],
Chris@0 131 ],
Chris@0 132 ];
Chris@0 133
Chris@0 134 return $extra;
Chris@0 135 }
Chris@0 136
Chris@0 137 /**
Chris@0 138 * Implements hook_cron().
Chris@0 139 *
Chris@0 140 * Queues news feeds for updates once their refresh interval has elapsed.
Chris@0 141 */
Chris@0 142 function aggregator_cron() {
Chris@0 143 $queue = \Drupal::queue('aggregator_feeds');
Chris@0 144
Chris@0 145 $ids = \Drupal::entityManager()->getStorage('aggregator_feed')->getFeedIdsToRefresh();
Chris@0 146 foreach (Feed::loadMultiple($ids) as $feed) {
Chris@0 147 if ($queue->createItem($feed)) {
Chris@0 148 // Add timestamp to avoid queueing item more than once.
Chris@0 149 $feed->setQueuedTime(REQUEST_TIME);
Chris@0 150 $feed->save();
Chris@0 151 }
Chris@0 152 }
Chris@0 153
Chris@0 154 // Delete queued timestamp after 6 hours assuming the update has failed.
Chris@0 155 $ids = \Drupal::entityQuery('aggregator_feed')
Chris@0 156 ->condition('queued', REQUEST_TIME - (3600 * 6), '<')
Chris@0 157 ->execute();
Chris@0 158
Chris@0 159 if ($ids) {
Chris@0 160 $feeds = Feed::loadMultiple($ids);
Chris@0 161 foreach ($feeds as $feed) {
Chris@0 162 $feed->setQueuedTime(0);
Chris@0 163 $feed->save();
Chris@0 164 }
Chris@0 165 }
Chris@0 166 }
Chris@0 167
Chris@0 168 /**
Chris@0 169 * Gets the list of allowed tags.
Chris@0 170 *
Chris@0 171 * @return array
Chris@0 172 * The list of allowed tags.
Chris@0 173 *
Chris@0 174 * @internal
Chris@0 175 */
Chris@0 176 function _aggregator_allowed_tags() {
Chris@0 177 return preg_split('/\s+|<|>/', \Drupal::config('aggregator.settings')->get('items.allowed_html'), -1, PREG_SPLIT_NO_EMPTY);
Chris@0 178 }
Chris@0 179
Chris@0 180 /**
Chris@0 181 * Implements hook_preprocess_HOOK() for block templates.
Chris@0 182 */
Chris@0 183 function aggregator_preprocess_block(&$variables) {
Chris@0 184 if ($variables['configuration']['provider'] == 'aggregator') {
Chris@0 185 $variables['attributes']['role'] = 'complementary';
Chris@0 186 }
Chris@0 187 }