diff --git a/og_ui/js/og_ui.js b/og_ui/js/og_ui.js new file mode 100644 index 00000000..6f74e3a9 --- /dev/null +++ b/og_ui/js/og_ui.js @@ -0,0 +1,31 @@ +(function ($) { + +Backdrop.behaviors.ogUiContentTypes = { + // Provide the vertical tab summaries for OG. + attach: function (context) {console.log('loaded'); + var $context = $(context); + $context.find('fieldset#edit-og').backdropSetSummary(function() {console.log('found it') + var vals = []; + if ($context.find('input[name="og_group_type"]:checked').length) { + vals.push(Backdrop.t('Is a "Group" content type')); + } + + if ($context.find('input[name="og_group_content_type"]:checked').length) { + vals.push(Backdrop.t('Is a "Group content" content type')); + var target_type = $context.find('select[name="target_type"]').find('option:selected').text(); + console.log(target_type); + if (target_type) { + vals.push(Backdrop.t('Target type: ' + target_type)); + } + var target_bundles = $context.find('select[name="target_bundles[]"]').find('option:selected').toArray().map(item => item.text).join(); + if (target_bundles) { + vals.push(Backdrop.t('Target bundles: ' + target_bundles)); + } + } + + return vals.length ? vals.join(', ') : Backdrop.t('Not set'); + }); + } +}; + +})(jQuery); diff --git a/og_ui/og_ui.module b/og_ui/og_ui.module index 7c452288..17d6d4d7 100644 --- a/og_ui/og_ui.module +++ b/og_ui/og_ui.module @@ -911,18 +911,20 @@ function og_ui_form_node_type_form_alter(&$form, &$form_state) { '#collapsible' => TRUE, '#group' => 'additional_settings', '#description' => t('Specify how OG should treat content of this type. Content may behave as a group, as group content, or may not participate in OG at all.'), + '#attached' => array( + 'js' => array(backdrop_get_path('module', 'og_ui') . '/js/og_ui.js'), + ), ); // 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 +938,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 +956,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 +982,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 +998,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 +1012,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 +1021,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 +1087,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); }