You are here

function mentions_crud_update in Open Social 8.8

Same name and namespace in other branches
  1. 8.9 modules/custom/mentions/mentions.module \mentions_crud_update()
  2. 8 modules/custom/mentions/mentions.module \mentions_crud_update()
  3. 8.2 modules/custom/mentions/mentions.module \mentions_crud_update()
  4. 8.3 modules/custom/mentions/mentions.module \mentions_crud_update()
  5. 8.4 modules/custom/mentions/mentions.module \mentions_crud_update()
  6. 8.5 modules/custom/mentions/mentions.module \mentions_crud_update()
  7. 8.6 modules/custom/mentions/mentions.module \mentions_crud_update()
  8. 8.7 modules/custom/mentions/mentions.module \mentions_crud_update()
  9. 10.3.x modules/custom/mentions/mentions.module \mentions_crud_update()
  10. 10.0.x modules/custom/mentions/mentions.module \mentions_crud_update()
  11. 10.1.x modules/custom/mentions/mentions.module \mentions_crud_update()
  12. 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());
  }
}