jammer.module in Jammer 1.0.x
Same filename and directory in other branches
General functions and hook implementations.
File
jammer.moduleView source
<?php
/**
* @file
* General functions and hook implementations.
*/
// should be moved to a common place (also used in src/Form/GConfigForm.php)
define('JAMMER_ALWAYS', 0);
define('JAMMER_NOTCREATION', 1);
define('JAMMER_CREATION', 2);
define('JAMMER_EMPTY', 3);
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
// returns true/false if current $roles match conditions in $entry
function jammer_matchRoles($roles, $entry) {
$inList = FALSE;
foreach ($roles as $role) {
if (in_array($role, $entry['roles'])) {
$inList = TRUE;
break;
}
}
// return depending on 'invert' field
if ($entry['invert']) {
return !$inList;
}
else {
return $inList;
}
}
// returns true/false if state of current node matches conditions in $entry
function jammer_matchCreation($form, $form_state, $entry) {
// if 'always' don't mind
if ($entry['creation'] == JAMMER_ALWAYS) {
return TRUE;
}
// $form being created?
$created = FALSE;
$node = $form_state->node;
if (!isset($node->nid) || isset($node->is_new)) {
$created = TRUE;
}
}
// sort by priority (usort() callback)
function jammer_sortPriority($a, $b) {
if (!isset($a['priority'])) {
return 1;
}
if (!isset($b['priority'])) {
return -1;
}
if ($a['priority'] < $b['priority']) {
return -1;
}
if ($a['priority'] > $b['priority']) {
return 1;
}
return 0;
}
// main function: alter fields in form if needed
function jammer_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$messenger = \Drupal::messenger();
// get our data
$config = \Drupal::config('jammer.settings');
$data = $config
->get('stored_values');
$data = unserialize($data);
// sort entries by priority
usort($data, 'jammer_sortPriority');
// get current user roles
$user = \Drupal::currentUser();
$roles = $user
->getRoles();
// loop on entries to treat the ones that match $form_id
foreach ($data as $entry) {
// matching $form_id
if ('node_' . $entry['form'] . '_form' == $form_id) {
// check for roles matching
if (jammer_matchRoles($roles, $entry)) {
// ok. check for always/creation/exclude creation criteria
if (jammer_matchCreation($form, $form_state, $entry)) {
// ok. apply field(s) alteration
foreach ($entry['elements'] as $field) {
if (isset($form[$field])) {
if ($entry['remove']) {
// disable
$form[$field]['#disabled'] = 'disabled';
}
else {
// hide
$form[$field]['#access'] = FALSE;
}
}
}
}
}
}
}
}
// help data
function jammer_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.jammer':
$output = '';
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('G-Jammer allows to hide/desactivate fields in forms, with various conditions.') . '</p>';
$output .= '<h3>' . t('Usage') . '</h3>';
$output .= '<p>' . t('See configuration page at: <a href=":structure_types">configuration page</a>', [
':structure_types' => Url::fromRoute('jammer.jammer_config')
->toString(),
]) . '</p>';
$output .= '<p>' . t('Each action rule has the following elements:') . '</p>';
$output .= '<ul>';
$output .= '<li>' . t('Form id: drop-down to select on which form the rule will apply') . '</li>';
$output .= '<li>' . t('Elements id: list of fields name (comma-separated) in the form on which the rule will apply') . '</li>';
$output .= '<li>' . t('Remove/disable: if checked the target field(s) are no more visible on the form; else they are swtiched to read-only state (note: this may have no effect on some type of fields)') . '</li>';
$output .= '<li>' . t('Always/Exclude creation/Only creation/Exclude when empty: allow to apply the rule depending of the creation status of the node (if creating a new node or modifying an existing node). Note: "Exclude when empty" is not implemented.') . '</li>';
$output .= '<li>' . t('Only apply to role(s): list of role(s) which are concerned by the rule. The rule is applied if current user has (at least) one of these roles') . '</li>';
$output .= '<li>' . t('Reverse role(s): negate the previous behavior. If checked the rule is applied if current user do not have any of these roles') . '</li>';
$output .= '<li>' . t('Priority: from -20 (max priority) to 20. Rules are evaluated in this order') . '</li>';
$output .= '<li>' . t('Category/group: string used to group together existing rules in the list at the end of the page') . '</li>';
$output .= '<li>' . t('Comment: comment that can be associated to the rule') . '</li>';
$output .= '</ul>';
$output .= '<p>' . t('At bottom a table shows the existing rules. Each has a "Modify" and "Delete" link.') . '</p>';
$output .= '<p>' . t('Do not forget to validate your modifications (button is before and after table of existing rules).') . '</p>';
return $output;
}
}