Skip to content
Snippets Groups Projects

Resolve #3482567 "Convert announcements feed"

Open nicxvan requested to merge issue/drupal-3482567:3482567-convert-announcements-feed into 11.x
5 unresolved threads
3 files
+ 149
139
Compare changes
  • Side-by-side
  • Inline
Files
3
<?php
namespace Drupal\announcements_feed\Hook;
    • Comment on lines +1 to +3

      Are strict types needed, or will it break something?

      Suggested change
      1 <?php
      2
      3 namespace Drupal\announcements_feed\Hook;
      1 <?php
      2
      3 declare(strict_types=1);
      4
      5 namespace Drupal\announcements_feed\Hook;
      • Current .module files don't have it. There is a risk adding it will break things since this initiate it more about covering as is which is why we can just regenerate the baseline.

      • Please register or sign in to reply
Please register or sign in to reply
use Drupal\announcements_feed\RenderCallbacks;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Announcements Feed Hook Implementations.
*/
class AnnouncementsFeedHooks {
/**
* Implements hook_help().
*/
#[Hook('help')]
public function help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.announcements_feed':
$output = '';
$output .= '<h2>' . \t('About') . '</h2>';
$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>.', [':documentation' => 'https://www.drupal.org/docs/core-modules-and-themes/core-modules/announcements-feed']) . '</p>';
$output .= '<h2>' . \t('Uses') . '</h2>';
$output .= '<dl><dt>' . \t('Accessing announcements') . '</dt>';
$output .= '<dd>' . \t('Users with the "View drupal.org announcements" permission may click on the "Announcements" item in the administration toolbar, or access @link, to see all announcements relevant to the Drupal version of your site.', ['@link' => Link::createFromRoute(\t('Announcements'), 'announcements_feed.announcement')->toString()]) . '</dd>';
$output .= '</dl>';
return $output;
}
}
/**
* Implements hook_toolbar().
*/
#[Hook('toolbar')]
public function toolbar() {
if (!\Drupal::currentUser()->hasPermission('access announcements')) {
return ['#cache' => ['contexts' => ['user.permissions']]];
}
$items['announcement'] = [
'#type' => 'toolbar_item',
'tab' => [
'#lazy_builder' => [
'announcements_feed.lazy_builders:renderAnnouncements',
[],
],
'#create_placeholder' => \TRUE,
Please register or sign in to reply
'#cache' => [
'tags' => [
'announcements_feed:feed',
],
],
],
'#wrapper_attributes' => [
'class' => [
'announce-toolbar-tab',
],
],
'#cache' => [
'contexts' => [
'user.permissions',
],
],
'#weight' => 3399,
];
// \Drupal\toolbar\Element\ToolbarItem::preRenderToolbarItem adds an
// #attributes property to each toolbar item's tab child automatically.
// Lazy builders don't support an #attributes property so we need to
// add another render callback to remove the #attributes property. We start by
// adding the defaults, and then we append our own pre render callback.
$items['announcement'] += \Drupal::service('plugin.manager.element_info')->getInfo('toolbar_item');
$items['announcement']['#pre_render'][] = [RenderCallbacks::class, 'removeTabAttributes'];
return $items;
}
/**
* Implements hook_toolbar_alter().
*/
#[Hook('toolbar_alter')]
public function toolbarAlter(&$items) {
// As the "Announcements" link is shown already in the top toolbar bar, we
// don't need it again in the administration menu tray, so hide it.
if (!empty($items['administration']['tray'])) {
$callable = function (array $element) {
unset($element['administration_menu']['#items']['announcements_feed.announcement']);
return $element;
};
$items['administration']['tray']['toolbar_administration']['#pre_render'][] = $callable;
}
}
/**
* Implements hook_theme().
*/
#[Hook('theme')]
public function theme($existing, $type, $theme, $path) {
return [
'announcements_feed' => [
'variables' => [
'featured' => \NULL,
'standard' => \NULL,
    • Comment on lines +101 to +102

      Is there a reason for the \ before NULL?

      Suggested change
      101 'featured' => \NULL,
      102 'standard' => \NULL,
      101 'featured' => NULL,
      102 'standard' => NULL,
      • This is a rector thing.

        There is no current coding standard against it and am issue where adding it may become standard for some global functions.

        The other strong consideration is this is a complex automated process, the more manual steps the more likely a mistake will be made when regenerating from one of the inevitable conflicts.

      • Please register or sign in to reply
Please register or sign in to reply
'count' => 0,
'feed_link' => '',
],
],
'announcements_feed_admin' => [
'variables' => [
'featured' => \NULL,
'standard' => \NULL,
    • Comment on lines +109 to +110

      same as above

      Suggested change
      109 'featured' => \NULL,
      110 'standard' => \NULL,
      109 'featured' => NULL,
      110 'standard' => NULL,
Please register or sign in to reply
'count' => 0,
'feed_link' => '',
],
],
];
}
/**
* Implements hook_cron().
*/
#[Hook('cron')]
public function cron() {
$config = \Drupal::config('announcements_feed.settings');
$interval = $config->get('cron_interval');
$last_check = \Drupal::state()->get('announcements_feed.last_fetch', 0);
$time = \Drupal::time()->getRequestTime();
if (($time - $last_check) > $interval) {
\Drupal::service('announcements_feed.fetcher')->fetch(\TRUE);
    • Suggested change
      128 \Drupal::service('announcements_feed.fetcher')->fetch(\TRUE);
      128 \Drupal::service('announcements_feed.fetcher')->fetch(TRUE);
Please register or sign in to reply
\Drupal::state()->set('announcements_feed.last_fetch', $time);
}
}
}
Loading