drupal|May 14, 2019|1 min read

Drupal 8 - How to show view user data to owner user only

TL;DR

Add a contextual filter for the current user ID in your Drupal 8 view to restrict displayed data to the logged-in user's own content.

Drupal 8 - How to show view user data to owner user only

Introduction to Problem

I have some data related to users. For example: which are the files users have downloaded. This information is private to user. I wanted to show users their history of activities. Views are fantastic module to create a view block for this kind of purpose. I configured it to show data I wanted, and show it on user profile page.

But, the issue now is, everybody can see everybody’s data. The objective was to show user data to current logged-in user only. ANd, the data must belong to him only.

Its not like User-A should be able to see User-B data.

Solution

  • In your view, click on Advanced, then Add a Relationship

Drupal Views

  • Search for Author. Add it.

Drupal Views

  • After selecting, on next screen. Click on Require this relationship

Drupal Views

  • Add a Filter. Search for Current user.

Note: This field will be visible only when you have added Relationship, as mentioned above

Drupal Views

  • Select relationship of author, which you just created in above steps.
    • In Operator, select Is equal to
    • On right hand side, under option Is the logged in user, select Yes

Drupal Views

Save the view. Thats it. Note: This post does not include which fields to show, on which page to show.

Enjoy…

Related Posts

Drupal 8 - How to Theme Form and its Fields with reordering fields

Drupal 8 - How to Theme Form and its Fields with reordering fields

Introduction In this post, we will see how to theme form and its fields…

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Introduction I needed a report page, where I wanted to have some information…

Drupal: How to detect country and redirect to country specific website by using Cloudflare

Drupal: How to detect country and redirect to country specific website by using Cloudflare

Introduction Assume you have a drupal website and using cloudflare. You are…

Drupal - How to rename column of a content type

Drupal - How to rename column of a content type

Introduction You already have a content type with one or more fields in it…

Drupal 8 - How to hide help link About text formats and text format guidelines

Drupal 8 - How to hide help link About text formats and text format guidelines

Problem In drupal textarea field, it was always a pain to see the two links…

Drupal 8 Smart Image Style - Handle aspect ratio for small and long images

Drupal 8 Smart Image Style - Handle aspect ratio for small and long images

Problem Statement I’ve been using image styles, and heavily used “Scale and crop…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…