4

I have an order page that displays a set of line items.

screenshot

However, my client wants me to remove the "Responsible Vendor" from display for authenticated users, but it should be displayed for administrators and store manager users.

I have tried the following, but I could not get the desired results.

  1. Hide using Field Permission as described in https://stackoverflow.com/a/9187147/260665

  2. Implementing hook_field_widget_form_alter() following https://stackoverflow.com/q/8009716/260665

I am using the following code.

function bricks_field_widget_form_alter(&$element, &$form_state, $context) {
  if ($context['field']['field_name'] == 'field_responsible_vendor') {
    // Users without the "administer nodes" permission should not see the
    // Responsible Vendor section
    if (!user_access('administer nodes')) {
      unset($element['#options']['field_responsible_vendor']);
    }
  }
}

This hook is never invoked for the field_responsible_vendor field which represents the Responsible vendor entity reference field.

Now, I tried to dig deep and see why this hook was never invoked for the field in which I am interested. I placed breakpoints in /includes/module.inc with some test code in it.

    // Raj:
    $gotIt = NULL;
    if (is_array($context1)) {
      if ($context1['field'] == 'field_responsible_vendor') {
        $gotIt = $context1['field']['field_name'];
      }
      if (is_array($context1['field'])) {
        if ($context1['field']['field_name'] == 'field_responsible_vendor') {
          $gotIt = $context1['field']['field_name'];
        }
      }
    }

I tried it with $context2 too. The nearest I could get was the following.

enter image description here

I could never get to the field_responsible_vendor field to unset it based on the user roles.

Is there anything I am missing? How can I achieve what I am looking for?

As per @Darvanen's answer, I was able to remove the Responsible vendor value using this code.

function bricks_views_pre_render(&$view) {
  foreach ($view->result as $lineItem) {
    if (!user_access('administer nodes')) {
      unset($lineItem->field_field_responsible_vendor);
    }
  }
}

I have the following 2 problems.

  1. This hook is invoked for all views, I would rather like to restrict either the invocation to this page / view alone, or provide a condition to check if it is the page where this code should be executed in order to make sure that performance is not impacted.
  2. The column header is appearing though none of the line items will contain the value for it. I suppose I need to use another hook in order to address this?

    screenshot

1
+50

I would hide the column using a hook_preprocess_HOOK().

The item list is rendered by a view which is using table display and views-view-table.tpl.php so we just need to alter $variables which are passed to that template file; like this:

/**
 * Implemenets hook_preprocess_views_view_table()
 *
 * Removes a column out of order items table for non-admin users
 */
function MYTHEME_preprocess_views_view_table(&$variables) {
  if ($variables['view']->name == 'commerce_line_item_table') {
    if (!user_access('administer nodes')) {

      // name of the field that you want to hide
      $field_name = 'field_responsible_vendor';

      // Unset it from the table header
      unset($variables['header'][$field_name]);

      // Unset the value from each row
      foreach ($variables['rows'] as &$row) {
        unset($row[$field_name]);
      }
    }
  }
}

(This function should be placed in the template.php file in the theme folder and the word MYTHEME should be replaced by the theme name).

  • Perfectly addresses my issue. Thank you. Had to clear the cache in order to get it working! I still wonder why Field Permission didn't work for me. – Raj Pawan Gumdal May 19 '15 at 8:29
1

The hook method is a good one but you'll need a different hook.

The order page uses a view of line items, not a node display, so you should use a views hook.

I would try either hook_views_pre_render or hook_views_pre_view.

Use dpm($view) (needs devel module) to inspect the view and then modify it however you wish.

To figure out that it was using a view I went to the page, inspected the element in my browser and followed the chain up until I found either a form or a view:

enter image description here


Additional information:

  1. You will definitely need to put an if statement around your code. Dig through the dpm results for a view id or some other identifier and do something like:

    If ([address of view id] != [view id you want]){
      return;
    }
    
  2. To make the column disappear you'll need to unset the field itself, not just the value. Look in $view somewhere other than ->result. Sorry, without the dpm output in front of me I can't be more specific than that.

  • Hi Darvanen, please see my question with edited part. If this last bit of the problem is solved I can accept your answer and award the bounty. Thanks! – Raj Pawan Gumdal May 16 '15 at 6:00
  • This is only to hide a column out of display. When it's only about display, it's not recommended to alter 'result' or anything else prior rendering. – Alexar May 16 '15 at 15:18

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.