View source
<?php
use Drupal\avatars\Entity\AvatarGenerator;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\avatars\AvatarPreviewInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\user\UserInterface;
const AK_FIELD_PICTURE_ACTIVE = 'user_picture';
const AK_FIELD_PICTURE_USER = 'avatars_user_picture';
const AK_FIELD_AVATAR_GENERATOR = 'avatars_avatar_generator';
function avatars_cron() {
$intervals = \Drupal::config('avatars.settings')
->get('refresh_interval');
$avatars_preview_storage = \Drupal::entityTypeManager()
->getStorage('avatars_preview');
$avatars_generator_storage = \Drupal::entityTypeManager()
->getStorage('avatar_generator');
$avatars_generators = $avatars_generator_storage
->getEnabledAvatarGenerators();
$timeService = \Drupal::time();
$now = $timeService
->getRequestTime();
foreach ($avatars_generators as $avatar_generator) {
$definition = $avatar_generator
->getPlugin()
->getPluginDefinition();
$query = $avatars_preview_storage
->getQuery()
->condition('avatar_generator', $avatar_generator
->id());
if ($definition['dynamic']) {
$offset = $intervals['dynamic'] > 0 ? $intervals['dynamic'] : 0;
$query
->condition('created', $now - $offset, '<');
}
else {
$offset = $intervals['static'] > 0 ? $intervals['static'] : 0;
if ($offset) {
$query
->condition('created', $now - $offset, '<');
$query
->condition('scope', AvatarPreviewInterface::SCOPE_TEMPORARY, '=');
}
else {
continue;
}
}
$ids = $query
->execute();
$avatars_preview_storage
->delete($avatars_preview_storage
->loadMultiple($ids));
}
}
function avatars_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
$avatar_manager = \Drupal::service('avatars.avatar_manager');
if ($field_definition
->getName() == AK_FIELD_PICTURE_ACTIVE && $operation == 'view' && $field_definition
->getTargetEntityTypeId() == 'user') {
if ($items) {
$user = $items
->getEntity();
$avatar_manager
->syncAvatar($user);
}
}
return AccessResult::neutral();
}
function avatars_entity_presave(EntityInterface $entity) {
if ($entity instanceof UserInterface && !$entity
->isNew()) {
$avatar_manager = \Drupal::service('avatars.avatar_manager');
$avatar_manager
->invalidateUserAvatar($entity);
$avatars_generator_storage = \Drupal::entityTypeManager()
->getStorage('avatar_generator');
foreach ($avatars_generator_storage
->getEnabledAvatarGenerators() as $avatar_generator) {
if ($avatar_generator
->getPlugin()
->getPluginId() == 'user') {
$avatar_manager
->notifyDynamicChange($avatar_generator, $entity);
}
}
}
}
function avatars_entity_update(EntityInterface $entity) {
if ($entity instanceof UserInterface && isset($entity->original)) {
$original = $entity->original;
if ($original
->getEmail() != $entity
->getEmail()) {
$avatar_manager = \Drupal::service('avatars.avatar_manager');
$avatars_generator_storage = \Drupal::entityTypeManager()
->getStorage('avatar_generator');
foreach ($avatars_generator_storage
->getEnabledAvatarGenerators() as $avatar_generator) {
$definition = $avatar_generator
->getPlugin()
->getPluginDefinition();
if (!empty($definition['dynamic'])) {
$avatar_manager
->notifyDynamicChange($avatar_generator, $entity);
}
}
}
}
}
function avatars_file_storage_load(array $entities) {
$avatar_manager = \Drupal::service('avatars.avatar_manager');
foreach ($entities as $file) {
if (FALSE !== $avatar_manager
->getAvatarPreviewByFile($file)) {
$file
->addCacheTags([
'avatar_preview',
]);
}
}
}
function avatars_theme() {
return [
'avatar_preview_radio' => [
'render element' => 'element',
],
];
}
function template_preprocess_avatar_preview_radio(array &$variables) {
if (isset($variables['element']['image'])) {
$variables['image'] = $variables['element']['image'];
}
$variables['radio'] = $variables['element']['radio'];
}
function avatars_callback_avatar_generators(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL, &$cacheable = TRUE) {
$avatars_generator_storage = \Drupal::entityTypeManager()
->getStorage('avatar_generator');
$options = [
'_none' => t('Site default'),
];
foreach ($avatars_generator_storage
->getEnabledAvatarGenerators() as $instance) {
if ($instance
->getPlugin()
->getPluginId() == 'user_preference') {
continue;
}
if ($entity instanceof UserInterface) {
$options[$instance
->id()] = $instance
->label();
}
}
return $options;
}