1

I am trying to create a View in Drupal 8 which displays the list of Username,Email,Role,Created Date and Status in a tabular format.

I am able to create the view and display all the fileds for administrator user but my problem is I cant display all the fileds for the anonymous user . Only Username,Role and Created fields are being displayed.

I cant figure it out why the other fields are not being displayed for anonymous user.

I have tried to check the permissions related to the users and can only find that View user information is relevent but enabling it for anonymous user does nothing other than giving hyperlink to Username.

Can anyone please help me to solve the issue.

My view configurion can be found here.

// views.view.users_list.yml

    uuid: 
    langcode: en
    status: true
    dependencies:
      config:
        - user.role.administrator
        - user.role.anonymous
      module:
        - user
    _core:
      default_config_hash: 
    id: users_list
    label: 'Users List'
    module: views
    description: 'Provides Users List View'
    tag: ''
    base_table: users_field_data
    base_field: uid
    core: 8.x
    display:
      default:
        display_plugin: default
        id: default
        display_title: Master
        position: 0
        display_options:
          access:
            type: perm
            options:
              perm: 'access user profiles'
          cache:
            type: tag
            options: {  }
          query:
            type: views_query
            options:
              disable_sql_rewrite: false
              distinct: false
              replica: false
              query_comment: ''
              query_tags: {  }
          exposed_form:
            type: basic
            options:
              submit_button: Apply
              reset_button: false
              reset_button_label: Reset
              exposed_sorts_label: 'Sort by'
              expose_sort_order: true
              sort_asc_label: Asc
              sort_desc_label: Desc
          pager:
            type: mini
            options:
              items_per_page: 10
              offset: 0
              id: 0
              total_pages: null
              expose:
                items_per_page: false
                items_per_page_label: 'Items per page'
                items_per_page_options: '5, 10, 25, 50'
                items_per_page_options_all: false
                items_per_page_options_all_label: '- All -'
                offset: false
                offset_label: Offset
              tags:
                previous: ‹‹
                next: ››
          style:
            type: table
            options:
              grouping: {  }
              row_class: ''
              default_row_class: true
              override: true
              sticky: false
              caption: ''
              summary: ''
              description: ''
              columns:
                name: name
              info:
                name:
                  sortable: false
                  default_sort_order: asc
                  align: ''
                  separator: ''
                  empty_column: false
                  responsive: ''
              default: '-1'
              empty_table: false
          row:
            type: fields
          fields:
            name:
              id: name
              table: users_field_data
              field: name
              entity_type: user
              entity_field: name
              label: ''
              alter:
                alter_text: false
                make_link: false
                absolute: false
                trim: false
                word_boundary: false
                ellipsis: false
                strip_tags: false
                html: false
              hide_empty: false
              empty_zero: false
              plugin_id: field
              relationship: none
              group_type: group
              admin_label: ''
              exclude: false
              element_type: ''
              element_class: ''
              element_label_type: ''
              element_label_class: ''
              element_label_colon: true
              element_wrapper_type: ''
              element_wrapper_class: ''
              element_default_classes: true
              empty: ''
              hide_alter_empty: true
              click_sort_column: value
              type: user_name
              settings: {  }
              group_column: value
              group_columns: {  }
              group_rows: true
              delta_limit: 0
              delta_offset: 0
              delta_reversed: false
              delta_first_last: false
              multi_type: separator
              separator: ', '
              field_api_classes: false
            mail:
              id: mail
              table: users_field_data
              field: mail
              entity_type: user
              entity_field: mail
              plugin_id: field
          filters:
            status:
              value: '1'
              table: users_field_data
              field: status
              plugin_id: boolean
              entity_type: user
              entity_field: status
              id: status
              expose:
                operator: ''
              group: 1
          sorts: {  }
          title: 'Users List'
          header: {  }
          footer: {  }
          empty: {  }
          relationships: {  }
          arguments: {  }
          display_extenders: {  }
        cache_metadata:
          max-age: -1
          contexts:
            - 'languages:language_content'
            - 'languages:language_interface'
            - url.query_args
            - user.permissions
          tags: {  }
      users_list_view:
        display_plugin: page
        id: users_list_view
        display_title: Page
        position: 1
        display_options:
          display_extenders: {  }
          path: users/list
          fields:
            name_1:
              id: name_1
              table: users_field_data
              field: name
              relationship: none
              group_type: group
              admin_label: ''
              label: Name
              exclude: false
              alter:
                alter_text: false
                text: ''
                make_link: false
                path: ''
                absolute: false
                external: false
                replace_spaces: false
                path_case: none
                trim_whitespace: false
                alt: ''
                rel: ''
                link_class: ''
                prefix: ''
                suffix: ''
                target: ''
                nl2br: false
                max_length: 0
                word_boundary: true
                ellipsis: true
                more_link: false
                more_link_text: ''
                more_link_path: ''
                strip_tags: false
                trim: false
                preserve_tags: ''
                html: false
              element_type: ''
              element_class: ''
              element_label_type: ''
              element_label_class: ''
              element_label_colon: true
              element_wrapper_type: ''
              element_wrapper_class: ''
              element_default_classes: true
              empty: ''
              hide_empty: false
              empty_zero: false
              hide_alter_empty: true
              click_sort_column: value
              type: user_name
              settings:
                link_to_entity: true
              group_column: value
              group_columns: {  }
              group_rows: true
              delta_limit: 0
              delta_offset: 0
              delta_reversed: false
              delta_first_last: false
              multi_type: separator
              separator: ', '
              field_api_classes: false
              entity_type: user
              entity_field: name
              plugin_id: field
            mail:
              id: mail
              table: users_field_data
              field: mail
              relationship: none
              group_type: group
              admin_label: ''
              label: Email
              exclude: false
              alter:
                alter_text: false
                text: ''
                make_link: false
                path: ''
                absolute: false
                external: false
                replace_spaces: false
                path_case: none
                trim_whitespace: false
                alt: ''
                rel: ''
                link_class: ''
                prefix: ''
                suffix: ''
                target: ''
                nl2br: false
                max_length: 0
                word_boundary: true
                ellipsis: true
                more_link: false
                more_link_text: ''
                more_link_path: ''
                strip_tags: false
                trim: false
                preserve_tags: ''
                html: false
              element_type: ''
              element_class: ''
              element_label_type: ''
              element_label_class: ''
              element_label_colon: true
              element_wrapper_type: ''
              element_wrapper_class: ''
              element_default_classes: true
              empty: ''
              hide_empty: false
              empty_zero: false
              hide_alter_empty: true
              click_sort_column: value
              type: basic_string
              settings: {  }
              group_column: value
              group_columns: {  }
              group_rows: true
              delta_limit: 0
              delta_offset: 0
              delta_reversed: false
              delta_first_last: false
              multi_type: separator
              separator: ', '
              field_api_classes: false
              entity_type: user
              entity_field: mail
              plugin_id: field
            roles_target_id:
              id: roles_target_id
              table: user__roles
              field: roles_target_id
              relationship: none
              group_type: group
              admin_label: ''
              label: Roles
              exclude: false
              alter:
                alter_text: false
                text: ''
                make_link: false
                path: ''
                absolute: false
                external: false
                replace_spaces: false
                path_case: none
                trim_whitespace: false
                alt: ''
                rel: ''
                link_class: ''
                prefix: ''
                suffix: ''
                target: ''
                nl2br: false
                max_length: 0
                word_boundary: true
                ellipsis: true
                more_link: false
                more_link_text: ''
                more_link_path: ''
                strip_tags: false
                trim: false
                preserve_tags: ''
                html: false
              element_type: ''
              element_class: ''
              element_label_type: ''
              element_label_class: ''
              element_label_colon: true
              element_wrapper_type: ''
              element_wrapper_class: ''
              element_default_classes: true
              empty: ''
              hide_empty: false
              empty_zero: false
              hide_alter_empty: true
              type: separator
              separator: ', '
              entity_type: user
              entity_field: roles
              plugin_id: user_roles
            created:
              id: created
              table: users_field_data
              field: created
              relationship: none
              group_type: group
              admin_label: ''
              label: Created
              exclude: false
              alter:
                alter_text: false
                text: ''
                make_link: false
                path: ''
                absolute: false
                external: false
                replace_spaces: false
                path_case: none
                trim_whitespace: false
                alt: ''
                rel: ''
                link_class: ''
                prefix: ''
                suffix: ''
                target: ''
                nl2br: false
                max_length: 0
                word_boundary: true
                ellipsis: true
                more_link: false
                more_link_text: ''
                more_link_path: ''
                strip_tags: false
                trim: false
                preserve_tags: ''
                html: false
              element_type: ''
              element_class: ''
              element_label_type: ''
              element_label_class: ''
              element_label_colon: true
              element_wrapper_type: ''
              element_wrapper_class: ''
              element_default_classes: true
              empty: ''
              hide_empty: false
              empty_zero: false
              hide_alter_empty: true
              click_sort_column: value
              type: timestamp
              settings:
                date_format: medium
                custom_date_format: ''
                timezone: ''
              group_column: value
              group_columns: {  }
              group_rows: true
              delta_limit: 0
              delta_offset: 0
              delta_reversed: false
              delta_first_last: false
              multi_type: separator
              separator: ', '
              field_api_classes: false
              entity_type: user
              entity_field: created
              plugin_id: field
            status:
              id: status
              table: users_field_data
              field: status
              relationship: none
              group_type: group
              admin_label: ''
              label: 'User status'
              exclude: false
              alter:
                alter_text: false
                text: ''
                make_link: false
                path: ''
                absolute: false
                external: false
                replace_spaces: false
                path_case: none
                trim_whitespace: false
                alt: ''
                rel: ''
                link_class: ''
                prefix: ''
                suffix: ''
                target: ''
                nl2br: false
                max_length: 0
                word_boundary: true
                ellipsis: true
                more_link: false
                more_link_text: ''
                more_link_path: ''
                strip_tags: false
                trim: false
                preserve_tags: ''
                html: false
              element_type: ''
              element_class: ''
              element_label_type: ''
              element_label_class: ''
              element_label_colon: true
              element_wrapper_type: ''
              element_wrapper_class: ''
              element_default_classes: true
              empty: ''
              hide_empty: false
              empty_zero: false
              hide_alter_empty: true
              click_sort_column: value
              type: boolean
              settings:
                format: default
                format_custom_true: ''
                format_custom_false: ''
              group_column: value
              group_columns: {  }
              group_rows: true
              delta_limit: 0
              delta_offset: 0
              delta_reversed: false
              delta_first_last: false
              multi_type: separator
              separator: ', '
              field_api_classes: false
              entity_type: user
              entity_field: status
              plugin_id: field
          defaults:
            fields: false
            style: false
            row: false
            access: false
          style:
            type: table
            options:
              grouping: {  }
              row_class: ''
              default_row_class: true
              override: true
              sticky: false
              caption: ''
              summary: ''
              description: ''
              columns:
                name_1: name_1
                mail: mail
                roles_target_id: roles_target_id
                created: created
                status: status
              info:
                name_1:
                  sortable: false
                  default_sort_order: asc
                  align: ''
                  separator: ''
                  empty_column: false
                  responsive: ''
                mail:
                  sortable: false
                  default_sort_order: asc
                  align: ''
                  separator: ''
                  empty_column: false
                  responsive: ''
                roles_target_id:
                  sortable: false
                  default_sort_order: asc
                  align: ''
                  separator: ''
                  empty_column: false
                  responsive: ''
                created:
                  sortable: false
                  default_sort_order: asc
                  align: ''
                  separator: ''
                  empty_column: false
                  responsive: ''
                status:
                  sortable: false
                  default_sort_order: asc
                  align: ''
                  separator: ''
                  empty_column: false
                  responsive: ''
              default: '-1'
              empty_table: false
          row:
            type: fields
            options: {  }
          access:
            type: role
            options:
              role:
                anonymous: anonymous
                administrator: administrator
          display_description: ''
        cache_metadata:
          max-age: -1
          contexts:
            - 'languages:language_content'
            - 'languages:language_interface'
            - url.query_args
            - user.roles
          tags: {  }

https://www.hastebin.com/yorotapuge.sql

I searched and found out there was a similar question for D7 but the solution is not much helpful to me.

Show user email from the user entity to anonymous users

View Images for administrator and anonymous user can be view in below :

Admin View[Admin View]

Anon View[Anon View]

2

This is by design, email addresses can be seen by admins only, but you can override it with hook_entity_field_access(): Add new permission to view user email field.

Maybe this module can help (not yet ported to D8): Show Email Address

Use Contact form instead of revealing email address

Email addresses should never be revealed publically for various security reasons and spam scripts.

Usually, a Contact form is provided if a user should be reachable through email. That way the user email address isn't revealed but they can still be contacted through email, and the form can be spam-protected too.

There is a Contact module permission where you can allow Anonymous users to use the Contact form on all user profiles.

You can also include a link to the contact form in Views (Contact link) instead of showing the email address.

  • Thanks for the suggestions will check them and let you know. The view can not be accessed directly from the URL inorder to access it token should be added to URL , hence I think it won't be security issue. I use it for other functionality. I need the list of users for that functionality. Hence used view can't find a better way to get it done. – miststudent2011 Nov 15 '18 at 16:26
  • I had proceeded with a different approach, added the solution below , what your view regarding that ? – miststudent2011 Nov 20 '18 at 11:18
  • You know best what your project requires. There is more than one way of solving things, and you seem to have found the one that suits you best, so all good! Just make sure it really is hidden without an access token ;) – prkos Nov 20 '18 at 14:18
  • Ya,Thanks tested it throughly so the view cant be accessed without access token. – miststudent2011 Nov 20 '18 at 14:23
0

This is most likely a permissions issue. Rather than giving anonymous users the permissions to view all user account info you may want to use the "Disable SQL Rewriting" option within your View. This will bypass to permissions checking for the outputting of that View.

Go to "Query Settings" in the "Advanced" section of your View settings and check the box for "Disable SQL Rewriting"

0

I used the following approach to solve the problem.Instead of patching the core.

I created a new using hook_views_data_alter hook.I created a new custom View field using the above hook and wrote the logic to retrieve data.

My Code is as follows:

my_module.views.inc file which is placed in the root directory of the module folder.

<?php
/**
 * Implements hook_views_data_alter().
 */
function my_module_views_data_alter(array &$data) {

  //Add new fields to Users views
  $data['users']['email_id_field'] = array(
    'field' => array(
      'id' => 'email_id_field',
      'title' => t('Email Id'),
      'help' => t('Custom field to display Email Id.'),
    ),
  );

}

EmailIdField.php file which is placed in my_module\Plugin\views\field\ directory.

namespace Drupal\my_module\Plugin\views\field;

use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;

/**
 * Default implementation of the base field plugin,
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("email_id_field")
 */

class EmailIdField extends FieldPluginBase {   

  /**
   * @{inheritdoc}
   */
  public function query() {

  } 

  /**
   * @{inheritdoc}
   */
  public function render(ResultRow $values) {

    $EmailId = $values->_entity->get('mail')->getValue();
    $result = $EmailId[0]['value'];

    return $result;
  }
}

After creating new field I have added that field to Views field and thus it is made to display in the view.

Good examples can be found here

https://www.drupal8.ovh/index.php/en/tutoriels/293/create-a-view-plugin-without-using-database-field

and here

https://www.webomelette.com/creating-custom-views-field-drupal-8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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