8

In /admin/people, I want to hide the display name (i.e. the Display name must not appear in the user list), but only for a specific role.

In my .module file, I have tried following hooks:

function hook_views_data_alter(array &$data) {
  kint($data['users']);die;
}

function hook_views_pre_render(\Drupal\views\ViewExecutable $view) {
  if($view->id() == 'myview'){
    print_r($view->result);die;
      // kint($value->_entity->get('title')->value);
  }
}

function hook_views_post_render(\Drupal\views\ViewExecutable $view) {
   if ($view->id() == 'viewid') {
    // Set the view title.
    $field_name = $view->getFields();
  }
}

function hook_field_views_data_views_data_alter(array &$data, \Drupal\field\FieldStorageConfigInterface $field) {
  $field_name = $field->getName();
  echo 'hjsjhwd';
  print_r($field_name);die;
  }

None of them helped me. I thought of getting fields of the view "user" and after that I will unset it for particular role. But in views listing, Name field is still appearing. How can I hide it for a particular role?

Is there any way to achieve this?

  • do you want to hide this field only in your view or you want it to be hide everywhere? – Alireza Tabatabaeian Aug 20 '17 at 7:58
  • Only at /admin/people listing page. There comes a column of display name at /admin/page, I want to hide that particular column actually. May be If I hide field, that column also gets hide for my particular role. – Sugandh Khanna Aug 20 '17 at 8:01
14
+50

The most efficient way would be using hook_views_pre_view(), which allows you to alter a View at the very beginning of the process i.e. before any query is built/run and before any rendering takes place. You can remove the 'name' field handler from the View based on the desired logic.

/**
 * Implements hook_views_pre_view().
 */
function MY_MODULE_views_pre_view($view, $display_id, array &$args) {
  if ($view->id() !== 'user_admin_people') {
    return;
  }

  $user_roles = \Drupal::currentUser()->getRoles();
  if (!in_array('my-special-role', $user_roles)) {
    $view->removeHandler($display_id, 'field', 'name');
  }
}

This solution assumes that you are only interested to serve this very specific use case - remove the field only from this specific View. Users might still be able to see the Display Names of users in other areas of the website.

  • Good job, voted up – Alireza Tabatabaeian Aug 23 '17 at 5:45
  • I think it should be $views->id() === ? – No Sssweat Aug 23 '17 at 6:46
  • 1
    @NoSsweat If !== then return, therefore only execute the logic if === – krystalcode Aug 23 '17 at 12:26
  • I might be wrong, but should there be an 's' in $views->id()? Should it be $view->id()? – Chris Happy Dec 12 '17 at 19:59
  • Also, if you want to see if the user has any of the roles, use if (empty(array_intersect($userRoles, $userRolesAllowed))). More info on array_intersect – Chris Happy Dec 12 '17 at 20:08
3

You can hide fields using hook_entity_field_access, you can hide field based on user account (role is one of its properties), operation and entity which contain the field.

You also can use Field Permissions module to hide a field from certain roles.

0

When you need to delete the table header < td > and also rows < td > you can use template_preprocess_views_view_table

/**
 * Implements template_preprocess_views_view_table().
 */
function TEMPLATE_preprocess_views_view_table(&$variables) {
  // @TODO: You should use $variables['view']->name and $variables['view']->current_display to apply this only one specific view.

  // Let's assume your field name is node status.
  // Remove header label.
  if (isset($variables['header']) && isset($variables['header']['status']) {
    unset($variables['header']['status']);
  }

  // Remove row columns.
  foreach($variables['rows'] as $key => $row) {
    if (isset($variables['rows']) && isset($variables['rows'][$key]) && isset($variables['rows'][$key]['status'])) {
      unset($variables['rows'][$key]['status']);
      unset($variables['result'][$key]->node_status);
    }
  }

  // You can always print_r($variables['rows']) to know what is exact field name that you need to delete.
  // print_r($variables['result']).
  // print_r($variables['header']).
}

Note: For any needs like this, try not to use more than two hooks to do your needs. Since Drupal always will do anything you want using one or two hooks no more.


Update: For Drupal 7 also using the same hook name on this URL template_preprocess_views_view_table

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.