1
function custom_form_alter(&$form, &$form_state, $form_id) {

global $user;
$node = node_load(arg(1));
$num  = $node->field_node_reference['und'][0]['nid'];
$val  = node_load($num);
$event_noderef_uid = $val->uid;



if($form_id == 'regester_node_form'){

if($user->uid == $event_noderef_uid){
 $form['field_NAME']['#access'] = FALSE;        
 }

I want to get ($user->uid == $val->uid) to Hide "field_NAME"

0

You can clean up the code a bit by using hook_form_FORM_ID_alter:

function mymodule_form_regester_node_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  $node = node_load(arg(1));
  $num  = $node->field_node_reference['und'][0]['nid'];
  $val  = node_load($num);
  $event_noderef_uid = $val->uid;

  if($user->uid == $event_noderef_uid){
    $form['field_NAME']['#access'] = FALSE;        
  }
}

Other than that, if field_NAME exists in the form and the condition is true, access will be set to FALSE. If you are testing this as user 1, you may not be seeing the results you desire as that is the superuser. It would help if you posted the form you are trying to alter and what its structure is for field_NAME. Probably a really good case to write an automated test for as well.

  • Can I use EntityFieldQuery instead of $node = node_load(arg(1)); $num = $node->field_node_reference['und'][0]['nid']; $val = node_load($num); $event_noderef_uid = $val->uid; – a.alshamiri Feb 23 '17 at 7:41
  • Why to load a node since it is available in the $form variable? – mchar Feb 23 '17 at 7:57
  • @mchar it's "nodereference" inside node.. – a.alshamiri Feb 26 '17 at 8:15

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.