cc-1.0.x-dev/modules/cc_account/cc_account.module
modules/cc_account/cc_account.module
<?php
declare(strict_types=1);
/**
* @file
* Provides cryptocurrency account entity types.
*/
use Drupal\Core\Render\Element;
use Drupal\user\UserInterface;
/**
* Implements hook_theme().
*/
function cc_account_theme() {
return [
'cc_account' => [
'render element' => 'elements',
],
'cc_account_type' => ['render element' => 'elements'],
'field__pay_link' => [
'base hook' => 'field',
'template' => 'barcode--qrcode--qr-target',
],
'field__qr_target' => [
'base hook' => 'field',
'template' => 'barcode--qrcode--qr-target',
],
'qr_target' => [
'base hook' => 'field',
'template' => 'barcode--qrcode--qr-target',
],
'barcode__qrcode__qr_target' => [
'base hook' => 'field',
'template' => 'barcode--qrcode--qr-target',
],
];
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function cc_account_theme_suggestions_cc_account(array $variables) {
$suggestions = [];
$account = $variables['elements']['#cc_account'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'cc_account__' . $sanitized_view_mode;
$suggestions[] = 'cc_account__' . $account->bundle();
$suggestions[] = 'cc_account__' . $account->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'cc_account__' . $account->id();
return $suggestions;
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function cc_account_theme_suggestions_cc_account_type(array $variables): array {
$suggestions = [];
$account_type = $variables['elements']['#cc_account_type'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'cc_account_type__' . $sanitized_view_mode;
$suggestions[] = 'cc_account_type__' . $account_type->id();
$suggestions[] = 'cc_account_type__' . $account_type->id() . '__' . $sanitized_view_mode;
return $suggestions;
}
/**
* Implements hook_theme_suggestions_field_alter().
*/
function cc_account_theme_suggestions_field_alter(array &$suggestions, array $variables) {
if (($variables['element']['#formatter'] == 'barcode') && ($variables['element']['#field_name'] == 'qr_target')) {
$suggestions[] = 'barcode__qrcode__qr_target';
// Log all suggestions for debugging
\Drupal::logger('cc')->notice('Field suggestions: @suggestions', [
'@suggestions' => print_r($suggestions, TRUE)
]);
}
}
/**
* Prepares variables for cryptocurrency account templates.
*
* Default template: cc-account.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the cryptocurrency account information and any
* fields attached to it.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_cc_account(array &$variables): void {
$variables['view_mode'] = $variables['elements']['#view_mode'];
$variables['cc_account'] = $variables['elements']['#cc_account'];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
// Add the account entity to the cache dependencies.
// This will trigger a cache rebuild whenever the account entity is updated.
$variables['#cache']['tags'] = $variables['cc_account']->getCacheTags();
}
/**
* Prepares variables for cryptocurrency account type templates.
*
* Default template: cc-account-type.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the account type information and
* any fields attached to it.
*/
function template_preprocess_cc_account_type(array &$variables): void {
$variables['view_mode'] = $variables['elements']['#view_mode'];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
/**
* Implements hook_user_cancel().
*/
function cc_account_user_cancel($edit, UserInterface $user, $method): void {
switch ($method) {
case 'user_cancel_block_unpublish':
// Unpublish accounts:
$account_storage = \Drupal::entityTypeManager()->getStorage('cc_account');
$account_ids = $account_storage->getQuery()
->condition('uid', $user->id())
->condition('status', 1)
->accessCheck(FALSE)
->execute();
foreach ($account_storage->loadMultiple($account_ids) as $account) {
$account->set('status', FALSE)->save();
}
break;
case 'user_cancel_reassign':
// Anonymize accounts:
$account_storage = \Drupal::entityTypeManager()->getStorage('cc_account');
$account_ids = $account_storage->getQuery()
->condition('uid', $user->id())
->accessCheck(FALSE)
->execute();
foreach ($account_storage->loadMultiple($account_ids) as $account) {
$account->set('uid', 0)->save();
}
break;
}
}
