better_revisions.module in Better Revisions 8
Same filename and directory in other branches
Main enhanced revisions module file.
File
better_revisions.moduleView source
<?php
/**
* @file
* Main enhanced revisions module file.
*/
use Drupal\Component\Utility\Xss;
/**
* Implements hook_help().
*/
function better_revisions_help($path, $arg) {
switch ($path) {
// Main module help for the block module.
case 'admin/help#better_revisions':
return t('<p>Changes the built-in revisions system to a less confusing drop-down.</p>');
}
}
/**
* Implements hook_permission().
*/
function better_revisions_permission() {
return [
'administer better revisions' => [
'title' => t('Administer better revisions'),
'description' => t('Configure better revisions settings.'),
],
];
}
/**
* Implements hook_form_alter().
*/
function better_revisions_form_alter(&$form, &$form_state, $form_id) {
if (isset($form['revision_log'])) {
$config = \Drupal::config('better_revisions.settings');
$req = $config
->get('br_require');
$condition = [
':input[name="revision"]' => [
'checked' => TRUE,
],
];
$form['revision_log']['#title'] = $config
->get('br_list_title');
if ($desc = $config
->get('br_list_help')) {
$form['revision_log']['#description'] = $desc;
}
$form['revision_log']['#type'] = 'select';
$form['revision_log']['#options'] = _better_revisions_options();
$form['revision_log']['#group'] = 'revision_information';
unset($form['revision_log']['#rows']);
$form['revision_log']['#weight'] = 1;
$form['revision_information']['revision_log'] = $form['revision_log'];
unset($form['revision_log']);
$area = $config
->get('br_add_txt');
if ($area != 0) {
$form['revision_log_extra'] = [
'#type' => 'textarea',
'#default_value' => '',
'#title' => $config
->get('br_area_title'),
'#states' => [
'visible' => $condition,
],
'#weight' => 2,
'#group' => 'revision_information',
];
}
$actions = [
'submit',
'publish',
'unpublish',
];
foreach ($actions as $action) {
@array_unshift($form['actions'][$action]['#submit'], 'better_revisions_log_combine');
}
if ($req || $area == 2) {
$form['#validate'][] = '_better_revisions_validate';
}
}
}
/**
* Custom submit handler to combine list and text area values.
*/
function better_revisions_log_combine($form, &$form_state) {
if ($form_state
->getValue('revision_log_extra')) {
$log = [
'#type' => 'inline_template',
'#template' => '{{ wrapper }}: {{ log }}',
'#context' => [
'wrapper' => Xss::filter($form_state
->getValue('revision_log_wrapper')),
'log' => Xss::filter($form_state
->getValue('revision_log_extra')),
],
];
$form_state
->setValue([
'revision_log',
0,
'value',
], \Drupal::service('renderer')
->render($log));
$form_state
->unsetValue('revision_log_extra');
}
}
/**
* Custom validate function.
*/
function _better_revisions_validate($form, &$form_state) {
$config = \Drupal::config('better_revisions.settings');
if (!$form_state
->getValue('revision')) {
return;
}
$req = $config
->get('br_require');
$area = $config
->get('br_add_txt') == 2;
if ($req) {
if (!$form_state
->getValue('revision_log')) {
$msg = $config
->get('br_list_title') . ' is required';
$form_state
->setErrorByName('revision_log', t($msg));
}
}
if ($area) {
if (!$form_state
->getValue('revision_log_extra')) {
$msg = $config
->get('br_area_title') . ' is required';
$form_state
->setErrorByName('revision_log_extra', t($msg));
}
}
}
/**
* Custom list options.
*/
function _better_revisions_options() {
$config = \Drupal::config('better_revisions.settings');
$opts = $config
->get('br_list_options');
$options = explode("\n", $opts);
$options_array = [
'' => t('Please select'),
];
foreach ($options as $option) {
$option = trim($option);
$matches = [];
if (preg_match('/(.*)\\|(.*)/', $option, $matches)) {
$options_array[$matches[1]] = $matches[2];
}
else {
$options_array[$option] = $option;
}
}
return $options_array;
}
Functions
Name | Description |
---|---|
better_revisions_form_alter | Implements hook_form_alter(). |
better_revisions_help | Implements hook_help(). |
better_revisions_log_combine | Custom submit handler to combine list and text area values. |
better_revisions_permission | Implements hook_permission(). |
_better_revisions_options | Custom list options. |
_better_revisions_validate | Custom validate function. |