function mentions_crud_update in Open Social 10.3.x
Same name and namespace in other branches
- 8.9 modules/custom/mentions/mentions.module \mentions_crud_update()
- 8 modules/custom/mentions/mentions.module \mentions_crud_update()
- 8.2 modules/custom/mentions/mentions.module \mentions_crud_update()
- 8.3 modules/custom/mentions/mentions.module \mentions_crud_update()
- 8.4 modules/custom/mentions/mentions.module \mentions_crud_update()
- 8.5 modules/custom/mentions/mentions.module \mentions_crud_update()
- 8.6 modules/custom/mentions/mentions.module \mentions_crud_update()
- 8.7 modules/custom/mentions/mentions.module \mentions_crud_update()
- 8.8 modules/custom/mentions/mentions.module \mentions_crud_update()
- 10.0.x modules/custom/mentions/mentions.module \mentions_crud_update()
- 10.1.x modules/custom/mentions/mentions.module \mentions_crud_update()
- 10.2.x modules/custom/mentions/mentions.module \mentions_crud_update()
Insert mentions into DB.
1 call to mentions_crud_update()
- mentions_insert_or_update in modules/
custom/ mentions/ mentions.module - Handling mentions crud operations.
File
- modules/
custom/ mentions/ mentions.module, line 120 - Code for the mentions module.
Code
function mentions_crud_update($type, $mentions, $id, $author) {
$mentions_storage = \Drupal::entityTypeManager()
->getStorage('mentions');
// Build old array of mentions.
$old_users = [];
$old_mids = [];
$mention_ids = \Drupal::entityQuery('mentions')
->condition('entity_type', $type)
->condition('entity_id', $id)
->execute();
foreach ($mention_ids as $mention) {
$entity = $mentions_storage
->load($mention);
// Make sure the uid value is available.
if (isset($entity
->get('uid')
->getValue()[0]['value'])) {
$old_user = $entity
->get('uid')
->getValue()[0]['value'];
$old_users[] = $old_user;
$old_mids[$old_user] = $mention;
}
}
// Build array of new mentions.
$new_users = [];
foreach ($mentions as $mention) {
if ($mention['target']['entity_type'] == 'profile') {
$storage = \Drupal::entityTypeManager()
->getStorage($mention['target']['entity_type']);
$profile = $storage
->load($mention['target']['entity_id']);
$new_users[] = $profile
->getOwnerId();
}
else {
$new_users[] = $mention['target']['entity_id'];
}
}
$event_dispatcher = \Drupal::service('event_dispatcher');
// Add new mentions.
foreach (array_diff($new_users, $old_users) as $uid) {
$mention_attributes = [
'entity_type' => $type,
'entity_id' => $id,
'uid' => $uid,
'auid' => $author,
];
$mention = $mentions_storage
->create($mention_attributes);
$mention
->save();
$event_dispatcher
->dispatch('mentions.insert', new Event());
}
// Update existing mentions.
foreach (array_intersect($new_users, $old_users) as $uid) {
$entity = $mentions_storage
->load($old_mids[$uid]);
$entity
->set('created', \Drupal::time()
->getRequestTime());
$entity
->save();
$event_dispatcher
->dispatch('mentions.update', new Event());
}
// Delete old mentions.
foreach (array_diff($old_users, $new_users) as $uid) {
$entity = $mentions_storage
->load($old_mids[$uid]);
$entity
->delete();
$event_dispatcher
->dispatch('mentions.delete', new Event());
}
}