0

I need to hide the text format tips for body field, depending on user role, in a module. After some research, I found this kind of code:

function mymodule_form_node_form_alter(&$form, &$form_state) {
  if (isset($form['body'][LANGUAGE_NONE][0]['#after_build'])) {
    $form['body'][LANGUAGE_NONE][0]['#after_build'][] = 'mymodule_node_after_build';
  } else {
     $form['body'][LANGUAGE_NONE][0]['#after_build'] = array('mymodule_node_after_build');
  }
}
function mymodule_node_after_build(&$form, &$form_state) {
  /*if (!user_access('administer filters')) {
    $form['LANGUAGE_NONE']['0']['format']['#attributes']['class'][] = 'invisible';
  }*/
}

*I know the mymodule_node_after_build function is all commented, so it's doing nothing, but it's here only for illustration. Just adding #after_build to the form or field makes it all invisible - and this is the issue here.*

The above code results in field body not displayed at all, instead of just hiding text format info. More precisely, the html contains this code in place of the body field (just an empty div):

<div class="field-type-text-with-summary field-name-body field-widget-text-textarea-with-summary form-wrapper" id="edit-body"></div>

I tried setting this also for:

$form['body'][LANGUAGE_NONE]['#after_build']
$form['body']['#after_build']
$form['#after_build']

(the last one displays white space instead of the whole form)

Unless there is another much simpler solution to this problem, I'd like to use the above approach or something similar in a module, but it's not working!! So what's wrong with it?

1 Answer 1

0

Here is the simple code you can write your role base logic in hidetips_test_node_form_after_build() fuction.


function hidetips_form_node_form_alter(&$form, &$form_state) {
  $form['#after_build'][] = 'hidetips_test_node_form_after_build';
}

function hidetips_test_node_form_after_build($form) {
  // Here you can write your logic for role based hide and display.
  $form['body']['und']['0']['format']['#access'] = FALSE;
    return $form;
}
1
  • Oh well. Looks like I forgot to add return $form at the end. Your solution reminded me about it!
    – camcam
    Commented Jul 12, 2012 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.