From 87e38dbeda70038d6c48f8683d004206eb4ef641 Mon Sep 17 00:00:00 2001 From: argiepiano Date: Fri, 20 May 2022 21:47:46 -0600 Subject: [PATCH] Issue #103. Improve the OG vertical tab --- og_ui/og_ui.module | 47 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/og_ui/og_ui.module b/og_ui/og_ui.module index 7c452288..979ffe65 100644 --- a/og_ui/og_ui.module +++ b/og_ui/og_ui.module @@ -914,15 +914,14 @@ function og_ui_form_node_type_form_alter(&$form, &$form_state) { ); // Group settings. - $url = array('!url' => l(t('Manage fields'), 'admin/structure/types/manage/' . str_replace('_', '-', $node_type) . '/fields')); + $url = '/admin/structure/types/manage/' . str_replace('_', '-', $node_type) . '/fields'; $is_group = og_is_group_type('node', $node_type); $description = t('Set the content type to be a group, that content will be associated with, and will have group members.'); if ($is_group) { - $description .= '
' . t('To unset the group definition you should delete the "Group type" field via !url.', $url); + $description .= '
' . t('To unset the group definition you should delete the "Group type" field via Manage fields.', array('!url' => $url)); } - $form['og']['og_group_type'] = array( '#type' => 'checkbox', '#title' => t('Group'), @@ -936,7 +935,9 @@ function og_ui_form_node_type_form_alter(&$form, &$form_state) { $description = t('Set the content type to be a group content, that can be associated with groups.'); if ($is_group_content) { - $description .= '
' . t('To unset the group content definition or change the settings you should delete the "Groups audience" field via !url.', $url); + $description .= '
' . t('To unset the group content definition or change the settings you should delete the "Groups audience" field via Manage fields.', array('!url' => $url)); + } else { + $description .= '
' . t('Checking this box will add a default "Group audience" field to this content type.'); } $group_content_options = og_get_all_group_entity(); if (!$group_content_options) { @@ -952,14 +953,23 @@ function og_ui_form_node_type_form_alter(&$form, &$form_state) { ); if ($group_content_options) { - // Don't show the settings, as there might be multiple OG audience fields - // in the same bundle. + $target_type_default_value = key($group_content_options); + $description = t('The entity type that can be referenced thru this field.'); + + // Check if this the audience field has already been set up. If so, set the + // form default value and disable this select box. + $audience_field_info = field_info_field(OG_AUDIENCE_FIELD); + if ($audience_field_info) { + $target_type_default_value = $audience_field_info['settings']['target_type']; + $description .= '
' . t('The target type has already been set for the default "Group audience" field. To change it, edit the "Groups audience" field via Manage fields, or add a new "Group audience" field in OG field settings.', array('!url' => $url, '!url_settings' => '/admin/config/group/fields')); + } + $form['og']['target_type'] = array( '#type' => 'select', '#title' => t('Target type'), '#options' => $group_content_options, - '#default_value' => key($group_content_options), - '#description' => t('The entity type that can be referenced thru this field.'), + '#default_value' => $target_type_default_value, + '#description' => $description, '#ajax' => array( 'callback' => 'og_node_type_form_settings', 'wrapper' => 'og-settings-wrapper', @@ -969,6 +979,11 @@ function og_ui_form_node_type_form_alter(&$form, &$form_state) { ':input[name="og_group_content_type"]' => array('checked' => TRUE), ), ), + + // If the default group audience field has already been set up there is + // no point in allowing the user access to this select box. The value + // can't be changed from here anyway. + '#disabled' => !empty($audience_field_info), ); $target_type = !empty($form_state['values']['target_type']) ? $form_state['values']['target_type'] : key($group_content_options); @@ -980,6 +995,13 @@ function og_ui_form_node_type_form_alter(&$form, &$form_state) { } } + $default_value = array(); + if ($audience_field_info) { + if (!empty($audience_field_info['settings']['handler_settings']['target_bundles'])) { + $default_value = $audience_field_info['settings']['handler_settings']['target_bundles']; + } + }; + // Get the bundles that are acting as group. $form['og']['target_bundles'] = array( '#prefix' => '
', @@ -987,7 +1009,7 @@ function og_ui_form_node_type_form_alter(&$form, &$form_state) { '#type' => 'select', '#title' => t('Target bundles'), '#options' => $bundles, - '#default_value' => array(), + '#default_value' => $default_value, '#size' => 6, '#multiple' => TRUE, '#description' => t('The bundles of the entity type that can be referenced. Optional, leave empty for all bundles.'), @@ -996,6 +1018,11 @@ function og_ui_form_node_type_form_alter(&$form, &$form_state) { ':input[name="og_group_content_type"]' => array('checked' => TRUE), ), ), + + // If the default group audience field has already been set up there is + // no point in allowing the user access to this select box. The value + // can't be changed from here anyway. + '#disabled' => !empty($audience_field_info), ); } array_unshift($form['#submit'], 'og_ui_form_node_type_form_submit'); @@ -1057,7 +1084,7 @@ function og_ui_node_type_save($bundle_name) { if ($config->get('og_group_content_type_' . $bundle_name) && !og_is_group_content_type('node', $bundle_name)) { $og_field = og_fields_info(OG_AUDIENCE_FIELD); - $og_field['field']['target_type'] = $config->get('target_type_' . $bundle_name); + $og_field['field']['settings']['target_type'] = $config->get('target_type_' . $bundle_name); $og_field['field']['settings']['handler_settings']['target_bundles'] = $config->get('target_bundles_' . $bundle_name); og_create_field(OG_AUDIENCE_FIELD, 'node', $bundle_name, $og_field); }