2

I have configured the user data display to show the last access information, but only me as I have the admin role can see it, anonymous users can not, nor even authenticated users: So my question is how can I make that information to be seen publicly.

To configure the user visualization I went to "/admin/config/people/accounts/display" and used the "Layout Builder"

Some place I have seen, although not sure, that the field displaying this info is {{ access }}

Following image is from an admin session:

enter image description here

And Following image is from a non-admin user (authenticated only)

enter image description here

1 Answer 1

2

To make the Last access time field visible, you can use hook_entity_field_access().

use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
    
function mymodule_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) { 
  if ($field_definition->getName() == 'access' && $operation == 'view') {
    return AccessResult::Allowed();
  }

  return  AccessResult::neutral();
}
15
  • I'm probably doing something wrong, it is not working, this is my 1st time doing hooks... new module: modules/custom/user_public_info/user_public_info.info.yml → name: User public info description: blah... type: module core_version_requirement: "^9.2|^10" # Information added by me on 23-10-22 version: '1.x-0.1' project: 'corlexim_custom_module' datestamp: 1697978409 file: modules/custom/user_public_info/user_public_info.module → <?php and your code changing: function user_public_info_entity_field_access(... then save enable module.... Commented Oct 22, 2023 at 13:41
  • Then I went to the api page: of api.drupal.org/api/drupal/… and is a little different from what you sent me, so I tried adapting it but nothing... Commented Oct 22, 2023 at 13:42
  • To debug code whether control goes in hook, did you try to add logger & hit the page where last access field rendering, then checked in the logs, whether logger print or not. One more thing I would like to mention you need to clear the cache once, after adding hook code Commented Oct 22, 2023 at 13:47
  • Thanks, yes clearing cache is a thing I do in every step... I will investigate how to add logger to do so, hopefully I'll find somehing Commented Oct 22, 2023 at 14:08
  • 1
    Yes, I have just found the problem... my fault.. I had the field user configured for "current user" when should be only "user", "current user" is the user that is configuring, while "user" is the actual user to whom the data is related... Thanks a lot!! Commented Oct 23, 2023 at 14:36

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.