Verified Commit 8aeb2ca5 authored by Dave Long's avatar Dave Long
Browse files

Issue #3483599 by nicxvan, ghost of drupal past, catch, longwave, fabianx:...

Issue #3483599 by nicxvan, ghost of drupal past, catch, longwave, fabianx: Convert all procedural hook implementations to Hook classes
parent 55bab340
Loading
Loading
Loading
Loading
Loading
+3542 −3494

File changed.

Preview size limit exceeded, changes collapsed.

+1 −1
Original line number Diff line number Diff line
@@ -62,7 +62,7 @@ public function renderBarePage(array $content, $title, $page_theme_property, arr

    // Add the bare minimum of attachments from the system module and the
    // current maintenance theme.
    system_page_attachments($html['page']);
    _system_page_attachments($html['page']);
    $this->renderer->renderRoot($html);

    $response = new HtmlResponse();
+0 −123
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Fetch community announcements from www.drupal.org feed.
 */

use Drupal\announcements_feed\RenderCallbacks;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function announcements_feed_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().
 */
function announcements_feed_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,
      '#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().
 */
function announcements_feed_toolbar_alter(&$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().
 */
function announcements_feed_theme($existing, $type, $theme, $path): array {
  return [
    'announcements_feed' => [
      'variables' => [
        'featured' => NULL,
        'standard' => NULL,
        'count' => 0,
        'feed_link' => '',
      ],
    ],
    'announcements_feed_admin' => [
      'variables' => [
        'featured' => NULL,
        'standard' => NULL,
        'count' => 0,
        'feed_link' => '',
      ],
    ],
  ];
}

/**
 * Implements hook_cron().
 */
function announcements_feed_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);
    \Drupal::state()->set('announcements_feed.last_fetch', $time);
  }
}
+137 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\announcements_feed\Hook;

use Drupal\announcements_feed\RenderCallbacks;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Hook\Attribute\Hook;

/**
 * Hook implementations for announcements_feed.
 */
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,
        '#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) : array {
    return [
      'announcements_feed' => [
        'variables' => [
          'featured' => NULL,
          'standard' => NULL,
          'count' => 0,
          'feed_link' => '',
        ],
      ],
      'announcements_feed_admin' => [
        'variables' => [
          'featured' => NULL,
          'standard' => NULL,
          '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);
      \Drupal::state()->set('announcements_feed.last_fetch', $time);
    }
  }

}
+0 −45
Original line number Diff line number Diff line
@@ -2,55 +2,10 @@

/**
 * @file
 * Provides an automated cron by executing it at the end of a response.
 */

use Drupal\Core\Url;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_help().
 */
function automated_cron_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.automated_cron':
      $output = '';
      $output .= '<h2>' . t('About') . '</h2>';
      $output .= '<p>' . t('The Automated Cron module runs cron operations for your site using normal browser/page requests instead of having to set up a separate cron job. The Automated Cron module checks at the end of each server response when cron operation was last ran and, if it has been too long since last run, it executes the cron tasks after sending a server response. For more information, see the <a href=":automated_cron-documentation">online documentation for the Automated Cron module</a>.', [':automated_cron-documentation' => 'https://www.drupal.org/documentation/modules/automated_cron']) . '</p>';
      $output .= '<h2>' . t('Uses') . '</h2>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Configuring Automated Cron') . '</dt>';
      $output .= '<dd>' . t('On the <a href=":cron-settings">Cron page</a>, you can set the frequency (time interval) for running cron jobs.', [':cron-settings' => Url::fromRoute('system.cron_settings')->toString()]) . '</dd>';
      $output .= '<dt>' . t('Disabling Automated Cron') . '</dt>';
      $output .= '<dd>' . t('To disable automated cron, the recommended method is to uninstall the module, to reduce site overhead. If you only want to disable it temporarily, you can set the frequency to Never on the Cron page, and then change the frequency back when you want to start it up again.') . '</dd>';
      $output .= '</dl>';
      return $output;
  }
}

/**
 * Implements hook_form_FORM_ID_alter() for the system_cron_settings() form.
 */
function automated_cron_form_system_cron_settings_alter(&$form, &$form_state): void {
  $automated_cron_settings = \Drupal::config('automated_cron.settings');

  $options = [3600, 10800, 21600, 43200, 86400, 604800];
  $form['cron']['interval'] = [
    '#type' => 'select',
    '#title' => t('Run cron every'),
    '#description' => t('More information about setting up scheduled tasks can be found by <a href=":url">reading the cron tutorial on drupal.org</a>.', [':url' => 'https://www.drupal.org/docs/8/administering-a-drupal-8-site/cron-automated-tasks']),
    '#default_value' => $automated_cron_settings->get('interval'),
    '#options' => [0 => t('Never')] + array_map([\Drupal::service('date.formatter'), 'formatInterval'], array_combine($options, $options)),
  ];

  // Add submit callback.
  $form['#submit'][] = 'automated_cron_settings_submit';

  // Theme this form as a config form.
  $form['#theme'] = 'system_config_form';
}

/**
 * Form submission handler for system_cron_settings().
 */
Loading