0

I am trying to hide the group_group (request membership) field for organic groups based on a radio button. Here is what I have done that doesn't work:

/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  $menu = menu_get_item();
  $path = drupal_get_path_alias();

  $node = node_load($menu['page_arguments'][0]->nid);

  // field_show_group_group is radio button
  if($item['field_show_group_group'][LANGUAGE_NONE]['value'] === 0) {
    dsm("Just show the subscribe button - NOTHING to do.");
  }
  else {

    dsm("hide button");
    $instance = field_info_instance('node', 'group_group', 'community');
    $instance['widget']['settings']['og_hide'] = FALSE;
    $instance['settings']['behaviors']['og_widget']['access_override'] = FALSE;
    unset($instance['widget']['settings']['og_hide']);
    unset($instance['settings']['behaviors']['og_widget']['access_override']);
    $form['group_group']['#access'] = FALSE;
    $form['group_group'][LANGUAGE_NONE][0]['value']['#access'] = FALSE;
    unset($form['group_group']);
    unset($form['group_group']);
    $menu['page_arguments'][0]->group_group['und'][0]['value'] = 0;
    unset($menu['page_arguments'][0]);
    $item['group_group'][LANGUAGE_NONE][0]['value']['#access'] = FALSE;
    unset($item['group_group'][LANGUAGE_NONE][0]['value']['#access']);
    $node->group_group[LANGUAGE_NONE][0]['value']['#access'] = FALSE;

}

These are all possibilities I couldn't think of, which of course for some reason don't work. I searched the whole internet for this including drupal.org, drupal.stackexchange, google and stackoverflow. Can you help?

  • If you want to hide the form's group_group than it should be $form['group_group']['#access'] = FALSE; but as far as I see, this wont work because the $item is undefined all the time. If this doesn't work I can think only one think, your code doesn't run. Also check if this field appears like 'group_group' in the $form and not like 'field_group_group' or similar strange way. – golddragon007 Dec 19 '18 at 10:43
  • There's a hide button on the field edit page, it uses the same $form['group_group']['#access'] = FALSE; to hide the field, maybe you need to check this patch drupal.org/project/og/issues/2278003 if you testing like an anonymous user. – golddragon007 Dec 19 '18 at 10:55
  • @golddragon007 The code is reaching the else and displaying the dsm('hide button'), so it's beeing run. I don't want to use the field settings as in the patch because I need to show or hide depending of the radio button provided on the form content type, not on the field settings. – staminna Dec 19 '18 at 11:04
  • 'field settings as in the patch' - you didn't check the patch, this fix the access issue, it's a separate thing, the field settings uses $form['group_group']['#access'] = FALSE; you need to use this, from the form alter. It's works in my system, if in yours doesn't, that means something overwrite this in your system. – golddragon007 Dec 19 '18 at 11:55
  • @golddragon007 am happy to accept your answer if you want to write one. Thanks – staminna Dec 19 '18 at 12:33
0

You should be able to hide the group_group field with the $form['group_group']['#access'] = FALSE; this is used by the Hide checkbox on the field settings page and this is how it works.

However, if you testing as an anonymous user maybe you have this issue: https://www.drupal.org/project/og/issues/2278003 there's a patch which fixes the access itself.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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