Ability to see referenced blocked users?

Created on 10 October 2024, 2 months ago

Problem/Motivation

This module is good, but simple workaround is to create entity reference filter in views and remove "User Status" filter so you can go to content type > manage fields > find your entity reference field to user and change Reference TYPE from Default to Views and select your (Entity Reference Source) view.
The Job is done! No need to install this module.

However the problem is that once user is able to select "blocked" user, and save the content he is unable to see him/his profile so it is blank and looks weird. So I believed this module will do the trick and I will be able to reference blocked users but also able to see them in Display mode.

Is it possible to implement this feature or somehow enable this option?

💬 Support request
Status

Active

Version

1.0

Component

Documentation

Created by

🇸🇰Slovakia coaston

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @coaston
  • 🇺🇸United States flashwebcenter Austin TX

    Hello,

    This approach won’t work for the Authored By field, which is the main reason this module was created. For the issue you have is likely due to Drupal’s default behavior, where blocked users can’t have their profiles or referenced fields rendered for anonymous users. To allow anonymous users to see fields of blocked users in a reference field, her is a solution:

    2- Add the following to your theme/module, I used article as a content type.

    use Drupal\file\Entity\File;
    use Drupal\Core\Url;
    
    function HOOK_preprocess_node(&$variables) {
    
      if ($variables['node']->bundle() == 'article') {
        // Load the referenced user.
        if ($user_reference = $variables['node']->get('field_user')->entity) {
          // Check if the user is blocked.
          if ($user_reference->isBlocked()) {
            // Manually load user data fields (e.g., name, email).
            $variables['blocked_user_name'] = $user_reference->getDisplayName();
            $variables['blocked_user_email'] = $user_reference->getEmail();
    
            // Load other fields.
            $variables['blocked_first_name'] = $user_reference->get('field_first_name')->value;
            $variables['blocked_last_name'] = $user_reference->get('field_last_name')->value;
    
            // Load the user picture URL if it exists.
            if ($user_picture = $user_reference->get('user_picture')->entity) {
              $file_uri = $user_picture->getFileUri();
              $variables['blocked_user_picture'] = \Drupal::service('file_url_generator')->generateAbsoluteString($file_uri);
            }
    
            $variables['blocked_user_summary'] = $user_reference->get('field_user_summary')->value;
          }
        }
      }
    }

    2- Create a custom twig template for the content type (node--your-content-type.html.twig) and add the following fields:

    {% if blocked_user_name %}
      <p>User Name: {{ blocked_user_name }}</p>
    {% endif %}
    
    {% if blocked_user_email %}
      <p>Email: {{ blocked_user_email }}</p>
    {% endif %}
    
    {% if blocked_first_name %}
      <p>First Name: {{ blocked_first_name }}</p>
    {% endif %}
    
    {% if blocked_last_name %}
      <p>Last Name: {{ blocked_last_name }}</p>
    {% endif %}
    
    {% if blocked_user_picture %}
      <p><img src="{{ blocked_user_picture }}" alt="{{ blocked_user_name }}'s picture"></p>
    {% endif %}
    
    {% if blocked_user_summary %}
      <p>Summary: {{ blocked_user_summary }}</p>
    {% endif %}

    This is an example of fields on the demo site; feel free to customize it to match your field names

    Best wishes,
    Alaa

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024