I have an order page that displays a set of line items.
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.
Hide using Field Permission as described in https://stackoverflow.com/a/9187147/260665
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.
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.
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.
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?