Allowing a user to bookmark a node i have created

 public function bookmark($nid,$uid) {
    $user_data = \Drupal::service('user.data');
    $bookmarked_nodes = $user_data->get('my_module', $uid, 'bookmark');
    $bookmarked_nodes[$nid] = $nid;
    $user_data->set('my_module', $uid, 'bookmark', $bookmarked_nodes);
    \Drupal::messenger()->addStatus(t('node bookmarked.'));

i would like then to create a view listing only nodes that has been bookmarked by the current user.

Is it possible ?

1 Answer 1


Better use the Flag module for a more robust bookmarking solution. Allows you to record a "flag" made by user (in this case, a bookmark) and has a ready-to-use Views integration to filter flags and relate the both user and the flagged entity.

  • The module is not stable yet
    – Matoeil
    Mar 7, 2023 at 18:19
  • @Matoeil Unless you have a company policy against unstable modules or modules not covered by Drupal security, it's stable enough for use. I would use Flag than reinvent the same thing myself (and have something even more unstable). Besides, if you find any flaws, you can always patch the module yourself with cweagans/composer-patches.
    – Joseph
    Mar 7, 2023 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.