user_badges.module in User Badges 8
Same filename and directory in other branches
Contains user_badges.module..
File
user_badges.moduleView source
<?php
/**
* @file
* Contains user_badges.module..
*/
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function user_badges_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the user_badges module.
case 'help.page.user_badges':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('User Badges module.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function user_badges_theme() {
$theme = [];
return $theme;
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function user_badges_user_presave(Drupal\Core\Entity\EntityInterface $entity) {
/** @var \Drupal\user\Entity\User $entity */
$account_role_ids = $entity
->getRoles();
$field_item_list = $entity
->get('field_user_badges');
foreach ($field_item_list
->filterEmptyItems() as $index => $item) {
/** @var \Drupal\user_badges\Entity\Badge $badge */
$badge = $item
->get('entity')
->getValue();
$badge_role_ids = $badge
->getBadgeRoleIds();
// Remove the badge if it has roles but none among the current ones.
if ($badge_role_ids && !array_intersect($badge_role_ids, $account_role_ids)) {
$field_item_list
->removeItem($index);
}
else {
$target_ids[] = $badge
->id();
}
}
// Add the rest.
$query = \Drupal::entityQuery('badge')
->condition('role_id', $account_role_ids, 'IN');
if (!empty($target_ids)) {
$query
->condition('id', $target_ids, 'NOT IN');
}
array_map([
$field_item_list,
'appendItem',
], $query
->execute());
}
Functions
Name | Description |
---|---|
user_badges_help | Implements hook_help(). |
user_badges_theme | Implements hook_theme(). |
user_badges_user_presave | Implements hook_ENTITY_TYPE_presave(). |