You are here

cronkeychange.module in Cron key change 2.x

Same filename and directory in other branches
  1. 8 cronkeychange.module
  2. 7 cronkeychange.module

Cron Key Change.

File

cronkeychange.module
View source
<?php

/**
 * @file
 * Cron Key Change.
 */
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function cronkeychange_form_system_cron_settings_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['cronkeychange'] = [
    '#type' => 'fieldset',
    '#tree' => FALSE,
    '#title' => t('Change cron key'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  ];
  $form['cronkeychange']['current'] = [
    '#type' => 'item',
    '#title' => t('Current cron key'),
    '#markup' => \Drupal::state()
      ->get('system.cron_key'),
  ];
  $form['cronkeychange']['generate'] = [
    '#type' => 'submit',
    '#value' => t('Generate new key'),
    '#submit' => [
      'cronkeychange_generate_submit',
    ],
  ];
}

/**
 * Generate new cron key.
 */
function cronkeychange_generate_submit(&$form = [], &$form_state = []) {
  $cron_key = Crypt::randomBytesBase64(55);
  \Drupal::state()
    ->set('system.cron_key', $cron_key);
  if (PHP_SAPI !== 'cli') {
    \Drupal::messenger()
      ->addMessage(t('New cron key generated.'));
  }
  \Drupal::logger('cronkeychange')
    ->notice('New cron key generated.');
}

/**
 * Implements hook_help().
 */
function cronkeychange_help($route_name, RouteMatchInterface $arg) {
  switch ($route_name) {
    case 'help.page.cronkeychange':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Cron key change is a module that adds various utility functions for the Drupal cron system. This module helps to generate a new cron key. This might be a useful utility module for anyone who has reason to believe their cron key has been accidentally disclosed. Drush command <strong><em>drush cronkeychange</strong></em> is available to display the newly generated cron key.') . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dt>' . t('Configuring Cron key change') . '</dt>';
      $output .= '<dd>' . t('On the Cron page, you can <a href=":cron-keygen#edit-cronkeychange">generate the new cron key</a>', [
        ':cron-keygen' => Url::fromRoute('system.cron_settings')
          ->toString(),
      ]) . '</dd>';

      // Add a link to the Drupal.org project.
      $output .= '<p>';
      $output .= t('Visit the <a href=":project_link">Cron key change project page</a> on Drupal.org for more information.', [
        ':project_link' => 'https://www.drupal.org/project/cronkeychange',
      ]);
      $output .= '</p>';
      return $output;
  }
}