announcements_feed.module 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * @file
  4. * Used to create an announcements feed using the JSON data from drupal.org.
  5. */
  6. /**
  7. * Default maximum age of cached feed data in seconds.
  8. */
  9. define('ANNOUNCEMENTS_FEED_DEFAULT_MAX_AGE', 86400);
  10. /**
  11. * Default cron interval for refreshing feed data in seconds.
  12. */
  13. define('ANNOUNCEMENTS_FEED_DEFAULT_CRON_INTERVAL', 21600);
  14. /**
  15. * Default limit for number of feed items to fetch.
  16. */
  17. define('ANNOUNCEMENTS_FEED_DEFAULT_LIMIT', 10);
  18. /**
  19. * Default URL for the announcements JSON feed.
  20. */
  21. define('ANNOUNCEMENTS_FEED_DEFAULT_JSON_URL', 'https://www.drupal.org/announcements.json');
  22. /**
  23. * Default URL for the announcements feed at drupal.org.
  24. */
  25. define('ANNOUNCEMENTS_FEED_DEFAULT_LINK', 'https://www.drupal.org/about/announcements');
  26. /**
  27. * Implements hook_help().
  28. */
  29. function announcements_feed_help($path) {
  30. if ($path == 'admin/help#announcements_feed') {
  31. $output = '';
  32. $output .= '<h3>' . t('About') . '</h3>';
  33. $output .= '<p>' . t('The Announcements module displays announcements from the Drupal community. For more information, see the <a href="@documentation">online documentation for the Announcements module</a>.', array('@documentation' => 'https://www.drupal.org/docs/core-modules-and-themes/core-modules/announcements-feed')) . '</p>';
  34. $output .= '<h3>' . t('Uses') . '</h3>';
  35. $output .= '<dl><dt>' . t('Accessing announcements') . '</dt>';
  36. $output .= '<dd>' . t('Users with the "View drupal.org announcements" permission may click on the "Announcements" item in the administration menu, or access <a href="@link">Announcements</a>, to see all announcements relevant to the Drupal version of your site.', array('@link' => url('admin/announcements_feed'))) . '</dd>';
  37. $output .= '</dl>';
  38. return $output;
  39. }
  40. }
  41. /**
  42. * Implements hook_menu().
  43. */
  44. function announcements_feed_menu() {
  45. $items['admin/announcements_feed'] = array(
  46. 'title' => 'Announcements',
  47. 'description' => 'Announcements and updates posted by the drupal.org community.',
  48. 'page callback' => 'announcements_feed_get_announcements',
  49. 'access arguments' => array('access announcements'),
  50. 'position' => 'left',
  51. 'weight' => 2,
  52. 'file' => 'announcements_feed.inc',
  53. 'options' => array(
  54. 'attributes' => array(
  55. 'class' => array('announcement-menu-class'),
  56. ),
  57. ),
  58. );
  59. return $items;
  60. }
  61. /**
  62. * Implements hook_permission().
  63. */
  64. function announcements_feed_permission() {
  65. return array(
  66. 'access announcements' => array(
  67. 'title' => t('View official announcements related to Drupal'),
  68. ),
  69. );
  70. }
  71. /**
  72. * Implements hook_theme().
  73. */
  74. function announcements_feed_theme() {
  75. return array(
  76. 'announcements_feed' => array(
  77. 'variables' => array(
  78. 'featured' => NULL,
  79. 'standard' => NULL,
  80. 'feed_link' => NULL,
  81. 'count' => 0,
  82. ),
  83. 'template' => 'announcements-feed',
  84. 'path' => drupal_get_path('module', 'announcements_feed'),
  85. ),
  86. );
  87. }
  88. /**
  89. * Processes variables for announcements-feed.tpl.php.
  90. *
  91. * @see announcements-feed.tpl.php
  92. */
  93. function template_preprocess_announcements_feed(&$variables) {
  94. if (!empty($variables['featured'])) {
  95. foreach($variables['featured'] as &$announcement) {
  96. _announcements_feed_preprocess_item($announcement);
  97. }
  98. }
  99. if (!empty($variables['standard'])) {
  100. foreach($variables['standard'] as &$announcement) {
  101. _announcements_feed_preprocess_item($announcement);
  102. }
  103. }
  104. if (isset($variables['feed_link'])) {
  105. $variables['feed_link'] = check_url($variables['feed_link']);
  106. }
  107. }
  108. /**
  109. * Helper to preprocess individual announcement items.
  110. */
  111. function _announcements_feed_preprocess_item(&$item) {
  112. $item['title'] = filter_xss($item['title']);
  113. $item['teaser'] = filter_xss($item['teaser']);
  114. $item['link'] = check_url($item['link']);
  115. $item['date_modified'] = check_plain($item['date_modified']);
  116. $item['date_published'] = check_plain($item['date_published']);
  117. $item['version'] = check_plain($item['version']);
  118. }
  119. /**
  120. * Implements hook_page_alter().
  121. */
  122. function announcements_feed_page_alter(&$page) {
  123. if (isset($page['page_top']['toolbar'])) {
  124. // If the toolbar is available, add a pre-render function to add the class.
  125. $page['page_top']['toolbar']['#pre_render'][] = 'announcements_feed_toolbar_pre_render_alter';
  126. }
  127. }
  128. /**
  129. * Pre-render function for adding default class to announcement link.
  130. */
  131. function announcements_feed_toolbar_pre_render_alter($toolbar) {
  132. $path = drupal_get_path('module', 'announcements_feed');
  133. $toolbar['#attached']['css'][] = $path . '/announcements_feed-toolbar.css';
  134. module_load_include('inc', 'announcements_feed');
  135. foreach ($toolbar['toolbar_menu']['#links'] as &$link) {
  136. if ($link['href'] === 'admin/announcements_feed') {
  137. $link['attributes']['class'][] = 'announcement-default';
  138. }
  139. }
  140. return $toolbar;
  141. }
  142. /**
  143. * Implements hook_cron().
  144. */
  145. function announcements_feed_cron() {
  146. module_load_include('inc', 'announcements_feed', 'announcements_feed');
  147. $cron_interval = variable_get('announcements_feed_cron_interval', ANNOUNCEMENTS_FEED_DEFAULT_CRON_INTERVAL);
  148. $last_check = variable_get('announcements_feed_last_fetch', 0);
  149. $time = time();
  150. if (($time - $last_check) > $cron_interval) {
  151. try {
  152. announcements_feed_fetch(TRUE);
  153. // Update the last_fetch variable to the current time.
  154. variable_set('announcements_feed_last_fetch', $time);
  155. }
  156. catch (Exception $e) {
  157. watchdog('announcements_feed', 'Exception occurred: @message', array('@message' => $e->getMessage()), WATCHDOG_ERROR);
  158. }
  159. }
  160. }