forked from colorfield/civicrm_user
-
Notifications
You must be signed in to change notification settings - Fork 0
/
civicrm_user.module
80 lines (73 loc) · 3.27 KB
/
civicrm_user.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* @file
* Contains civicrm_user.module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\civicrm_user\CiviCrmUserQueueItem;
/**
* Implements hook_help().
*/
function civicrm_user_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the civicrm_user module.
case 'help.page.civicrm_user':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Operations on Drupal User entities based on a CiviCRM data source. Useful when the process of creating and editing users is not delegated to Drupal.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_cron().
*
* Executes Contact to User operations.
*/
function civicrm_user_cron() {
$config = \Drupal::configFactory()->get('civicrm_user.settings');
if ($config->get('enable_cron') === 1) {
// We usually don't want to act every time cron runs (which could be every
// minute) so keep a time for the next run in the site state.
$interval = $config->get('cron_interval');
$nextExecution = \Drupal::state()->get('civicrm_user_cron.next_execution');
$nextExecution = !empty($nextExecution) ? $nextExecution : 0;
\Drupal::messenger()->addStatus(t('Next CiviCRM contact to user processing at @date.', ['@date' => date('d/m/Y H:i:s', $nextExecution)]));
if ((int) $_SERVER['REQUEST_TIME'] >= (int) $nextExecution) {
// @todo check if manual/cron queue is not running (create helper)
// @todo set back queue creator and processor once unit tests have been implemented.
/* @var \Drupal\civicrm_user\CiviCrmUserQueueCreatorInterface $creator */
$creator = \Drupal::service('civicrm_user.creator');
$creator->addItems(CiviCrmUserQueueItem::QUEUE_TYPE_CRON);
/* @var \Drupal\civicrm_user\CiviCrmUserQueueProcessorInterface $processor */
$processor = \Drupal::service('civicrm_user.processor');
$processor->processItems(CiviCrmUserQueueItem::QUEUE_TYPE_CRON);
\Drupal::logger('civicrm_user')->notice(t('CiviCRM user cron ran.'));
\Drupal::state()->set('civicrm_user_cron.next_execution', (int) $_SERVER['REQUEST_TIME'] + (int) $interval);
\Drupal::messenger()->addStatus(t('Setting next CiviCRM contact to user processing to @date.', ['@date' => date('d/m/Y H:i:s', (int) $_SERVER['REQUEST_TIME'] + (int) $interval)]));
}
}
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function civicrm_user_user_insert(EntityInterface $entity) {
// Send mail when a user is inserted.
// @todo check if user is not already notified + provide configuration to actually send.
// It makes sense to include this in a user insert hook because of the
// possibility to configure civicrm_user to prevent user operations within Drupal.
_user_mail_notify('register_no_approval_required', $entity);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function civicrm_user_form_user_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$config = \Drupal::configFactory()->get('civicrm_user.settings');
$isReadOnly = $config->get('user_readonly');
if ($isReadOnly) {
$form['account']['name']['#disabled'] = TRUE;
$form['account']['mail']['#disabled'] = TRUE;
}
}