Hello,

I was wondering if someone could help me learn about and decide if I should use unset <?php unset($form['field_nationality']); ?> or make #access FALSE to restrict the field to specific users. I would like only allow the field to be shown and used for role A on the node form and not for role B.

I hope someone can direct me.

Thanks!

Comments

yched’s picture

Status: Active » Fixed

As a general rule across drupal, use #access rather than brute-force unset. Other code in other modules might rely in the element being present in the array.

YK85’s picture

Status: Fixed » Active

In my custom module I used $form['field_example']['#access'] = FALSE; but the user still got the "Field Example is required" error message when trying to submit the form. Using unset($form['field_example']); made the field not show and the error message did not show.

I would like to use the recommended #access method but why is the field required error message still showing?

Thank you

YK85’s picture

Could anyone kindly help with this issue I am facing?

I used $form['field_example']['#access'] = FALSE; in template.php for roleB only, but I still get the "Field Example is required" error when user with roleB saves the node.

zeezhao’s picture

Also have a look at this page for more information: http://drupal.org/node/357328

karens’s picture

Status: Active » Fixed

If you are not allowing someone access to a required field, you will need to provide a value for that field. Without trying it I'm not sure if you want to set #value or #default_value in this case, but one or the other will work.

You should certainly not unset a required field ever, or your database will be corrupted by missing required values.

YK85’s picture

Title: unset vs #access to restrict field » set field as #required for specific role
Status: Fixed » Active

Hi,

markus_petrux mentions at http://drupal.org/node/357328#comment-1443156 that a different approach is needed to alter #required

If I make the field not required in the settings, but want to make it required for just one specific role, can someone help me with the code to achieve this?

roleA << all users: field will not be required as per settings, nothing saved in database
roleB << specific users: want to set field to required with code, value saved in database

I hope someone may have the time to help.

Thank you

zeezhao’s picture

Status: Active » Fixed

I have used exactly the same method to do this before. What you need is a function to apply to your condition, and call it in the after_build just like is done in the examples in the link above. Function will look like this:

function yourmodule_fix_required(&$elements) {
foreach (element_children($elements) as $key) {
if (isset($elements[$key]) && $elements[$key]) {
// Recurse through all children elements.
yourmodule_fix_required($elements[$key]);
}
}
$elements['#required'] = TRUE;
}

YK85’s picture

Hi, I appreciate the very quick reply!

I'm really new to programming and was wondering if I want to do the same for field_a, field_b, field_c...field_q do I need to type the code for each field or is there a better way to make it compact?

Thanks!

zeezhao’s picture

Without knowing the specifics of your code, unless there is a way to read your fields via a loop, you'll have to do each one.

Remember that you probably can't set the same field to false and also required... unless for different roles seeing the fields.

YK85’s picture

Hi again,

The below code is from the link in #4 + code in #7. It seems to work for field_myfield but I would somehow like to make it work for more than one field (make 5 fields required) for users with role_a

<?php
/**
* Custom after_build callback handler.
*/
function _mymodule_after_build($form, &$form_state) {
  _mymodule_fix_required($form['field_myfield']); // is there any way to add say 5 fields here instead of just field_myfield?
  return $form;
}

/**
* Recursively set the required attribute of a CCK field
* and all its dependent FAPI elements.
*/
function mymodule_fix_required(&$elements) {
  foreach (element_children($elements) as $key) {
    if (isset($elements[$key]) && $elements[$key]) {
    // Recurse through all children elements.
    mymodule_fix_required($elements[$key]);
    }
  }
  $elements['#required'] = 1;
}
?>

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

fnikola’s picture

can this code be placed in your template.php?