- 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.