Skip to content

Commit

Permalink
Issue #3425679: Fix PHP 8.2 issue with the Form Assembler which was m…
Browse files Browse the repository at this point in the history
…ixing global extra featuers configs on submit
  • Loading branch information
Natshah committed Mar 19, 2024
1 parent 127dcdd commit 5a4bde7
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 42 deletions.
2 changes: 1 addition & 1 deletion configbit/extra.components.varbase.bit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ config_bit:
dependencies:
varbase_ai:
title: "Varbase AI"
description: 'Collection of provided and configured recipes for AI tools, to empower Varbase editorial teams with more advanced Artificial Intelligence (AI) options. Built using <a href="https://www.drupal.org/project/ai_interpolator" target="_blank">AI Interpolator</a> and <a href="https://www.drupal.org/project/openai" target="_blank">OpenAI</a>.'
description: 'Provides a collection of recipes for AI tools. Enhances the work of the editorial team with capabilities with a range of advanced Artificial Intelligence (AI) options. Built using <a href="https://www.drupal.org/project/ai_interpolator" target="_blank">AI Interpolator</a> and <a href="https://www.drupal.org/project/openai" target="_blank">OpenAI</a>.'
selected: false
config_form: true
formbit: "src/FormBit/varbase_ai.formbit.php"
Expand Down
22 changes: 13 additions & 9 deletions src/Form/AssemblerForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ public function buildForm(array $form, FormStateInterface $form_state, array &$i
'#default_value' => $checkbox_selected,
];

if (isset($extra_feature_info['config_form']) &&
$extra_feature_info['config_form'] == TRUE) {
if (isset($extra_feature_info['config_form'])
&& $extra_feature_info['config_form'] == TRUE) {

$form['extra_features'][$extra_feature_key . '_config'] = [
'#type' => 'fieldset',
'#title' => $checkbox_title,
Expand Down Expand Up @@ -190,8 +191,9 @@ public function buildForm(array $form, FormStateInterface $form_state, array &$i
'#default_value' => $checkbox_selected,
];

if (isset($demo_content_info['config_form']) &&
$demo_content_info['config_form'] == TRUE) {
if (isset($demo_content_info['config_form'])
&& $demo_content_info['config_form'] == TRUE) {

$form['demo_content'][$demo_content_key . '_config'] = [
'#type' => 'fieldset',
'#title' => $checkbox_title,
Expand Down Expand Up @@ -255,8 +257,9 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
$extra_features_values[$extra_feature_key] = $form_state->getValue($extra_feature_key);
}

if (isset($extra_feature_info['config_form']) &&
$extra_feature_info['config_form'] == TRUE) {
if (isset($extra_feature_info['config_form'])
&& $extra_feature_info['config_form'] == TRUE) {

$formbit_file_name = \Drupal::service('extension.list.profile')->getPath('varbase') . '/' . $extra_feature_info['formbit'];
if (file_exists($formbit_file_name)) {

Expand All @@ -273,7 +276,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
}
}

$GLOBALS['install_state']['varbase']['extra_features_configs'] = $extra_features_editable_configs;
$GLOBALS['install_state']['varbase']['extra_features_configs'][$extra_feature_key] = $extra_features_editable_configs;
}
}
}
Expand All @@ -293,8 +296,9 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
$demo_content_values[$demo_content_key] = $form_state->getValue($demo_content_key);
}

if (isset($demo_content_info['config_form']) &&
$demo_content_info['config_form'] == TRUE) {
if (isset($demo_content_info['config_form'])
&& $demo_content_info['config_form'] == TRUE) {

$formbit_file_name = \Drupal::service('extension.list.profile')->getPath('varbase') . '/' . $demo_content_info['formbit'];
if (file_exists($formbit_file_name)) {

Expand Down
4 changes: 2 additions & 2 deletions src/Form/DevelopmentToolsAssemblerForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ public function buildForm(array $form, FormStateInterface $form_state, array &$i
];

// If config_form is true for this development tool.
if (isset($development_tool_info['config_form']) &&
$development_tool_info['config_form'] == TRUE) {
if (isset($development_tool_info['config_form'])
&& $development_tool_info['config_form'] == TRUE) {
$form['development_tools'][$development_tool_key . '_config'] = [
'#type' => 'fieldset',
'#title' => $checkbox_title,
Expand Down
21 changes: 13 additions & 8 deletions src/FormBit/varbase_ai.formbit.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
* Array of config names, and list of values.
*/
function varbase_ai_get_editable_config_names() {
$editable_configs = [
$varbase_ai_editable_configs = [
'openai.settings' => [
'api_key' => '',
'api_org' => '',
],
];

return $editable_configs;
return $varbase_ai_editable_configs;
}

/**
Expand All @@ -38,15 +38,15 @@ function varbase_ai_build_formbit(array &$formbit, FormStateInterface &$form_sta
$formbit['api_key'] = [
'#type' => 'textfield',
'#title' => ('API Key'),
'#default_value' => "sk-change-this",
'#default_value' => "",
'#description' => t('The API key is required to interface with OpenAI services. Get your API key by signing up on the <a href=":link" target="_blank">OpenAI website</a>.', [':link' => 'https://openai.com/api']),
'#element_validate' => ['validate_formbit_openai_api_key'],
];

$formbit['api_org'] = [
'#type' => 'textfield',
'#title' => t('Organization ID'),
'#default_value' => "org-change-this",
'#default_value' => "",
'#description' => t('The organization ID on your OpenAI account. This is required for some OpenAI services to work correctly.'),
'#element_validate' => ['validate_formbit_openai_api_org'],
];
Expand All @@ -63,10 +63,15 @@ function varbase_ai_build_formbit(array &$formbit, FormStateInterface &$form_sta
*/
function varbase_ai_submit_formbit(array $editable_config_values) {
// Save the changed values for openai.settings config.
$openaiConfig = \Drupal::configFactory()->getEditable('openai.settings');
$openaiConfig->set('api_key', $editable_config_values['openai.settings']['api_key']);
$openaiConfig->set('api_org', $editable_config_values['openai.settings']['api_org']);
$openaiConfig->save(TRUE);
if (isset($editable_config_values['openai.settings'])
&& isset($editable_config_values['openai.settings']['api_key'])
&& isset($editable_config_values['openai.settings']['api_org'])) {

$openaiConfig = \Drupal::configFactory()->getEditable('openai.settings');
$openaiConfig->set('api_key', $editable_config_values['openai.settings']['api_key']);
$openaiConfig->set('api_org', $editable_config_values['openai.settings']['api_org']);
$openaiConfig->save(TRUE);
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/FormBit/varbase_auth.formbit.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* Array of config names, and list of values.
*/
function varbase_auth_get_editable_config_names() {
$editable_configs = [
$varbase_auth_editable_configs = [
'varbase_auth' => [
'social_auth_type' => ['social_auth_google'],
],
];

return $editable_configs;
return $varbase_auth_editable_configs;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/FormBit/varbase_development.formbit.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* Array of config names, and list of values.
*/
function varbase_development_get_editable_config_names() {
$editable_configs = [
$varbase_development_editable_configs = [
'system.logging' => [
'error_level' => ERROR_REPORTING_HIDE,
],
Expand All @@ -25,7 +25,7 @@ function varbase_development_get_editable_config_names() {
],
];

return $editable_configs;
return $varbase_development_editable_configs;
}

/**
Expand Down
37 changes: 19 additions & 18 deletions varbase.profile
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,11 @@ function varbase_assemble_extra_components(array &$install_state) {

// Install selected extra features.
$selected_extra_features = [];
$selected_extra_features_configs = [];

if (isset($install_state['varbase']['extra_features_values'])) {
$selected_extra_features = $install_state['varbase']['extra_features_values'];
}

if (isset($install_state['varbase']['extra_features_configs'])) {
$selected_extra_features_configs = $install_state['varbase']['extra_features_configs'];
}

if (isset($selected_extra_features['varbase_heroslider'])
&& $selected_extra_features['varbase_heroslider'] == TRUE) {
$batch['operations'][] = [
Expand Down Expand Up @@ -156,10 +151,16 @@ function varbase_assemble_extra_components(array &$install_state) {
];
}

if (count($selected_extra_features_configs) &&
isset($extraFeatures[$extra_feature_key]['config_form']) &&
$extraFeatures[$extra_feature_key]['config_form'] == TRUE &&
isset($extraFeatures[$extra_feature_key]['formbit'])) {
$selected_extra_features_configs = [];
if (isset($install_state['varbase']['extra_features_configs'])
&& isset($install_state['varbase']['extra_features_configs'][$extra_feature_key])) {
$selected_extra_features_configs = $install_state['varbase']['extra_features_configs'][$extra_feature_key];
}

if (count($selected_extra_features_configs)
&& isset($extraFeatures[$extra_feature_key]['config_form'])
&& $extraFeatures[$extra_feature_key]['config_form'] == TRUE
&& isset($extraFeatures[$extra_feature_key]['formbit'])) {

$formbit_file_name = \Drupal::service('extension.list.profile')->getPath('varbase') . '/' . $extraFeatures[$extra_feature_key]['formbit'];

Expand Down Expand Up @@ -222,10 +223,10 @@ function varbase_assemble_extra_components(array &$install_state) {
];
}

if (count($selected_demo_content_configs) &&
isset($demoContent[$demo_content_key]['config_form']) &&
$demoContent[$demo_content_key]['config_form'] == TRUE &&
isset($demoContent[$demo_content_key]['formbit'])) {
if (count($selected_demo_content_configs)
&& isset($demoContent[$demo_content_key]['config_form'])
&& $demoContent[$demo_content_key]['config_form'] == TRUE
&& isset($demoContent[$demo_content_key]['formbit'])) {

$formbit_file_name = \Drupal::service('extension.list.profile')->getPath('varbase') . '/' . $demoContent[$demo_content_key]['formbit'];
if (file_exists($formbit_file_name)) {
Expand Down Expand Up @@ -331,10 +332,10 @@ function varbase_assemble_development_tools(array &$install_state) {
];
}

if (count($selected_development_configs) &&
isset($developmentTools[$development_tool_key]['config_form']) &&
$developmentTools[$development_tool_key]['config_form'] == TRUE &&
isset($developmentTools[$development_tool_key]['formbit'])) {
if (count($selected_development_configs)
&& isset($developmentTools[$development_tool_key]['config_form'])
&& $developmentTools[$development_tool_key]['config_form'] == TRUE
&& isset($developmentTools[$development_tool_key]['formbit'])) {

$formbit_file_name = \Drupal::service('extension.list.profile')->getPath('varbase') . '/' . $developmentTools[$development_tool_key]['formbit'];
if (file_exists($formbit_file_name)) {
Expand Down Expand Up @@ -380,7 +381,7 @@ function varbase_configure_multilingual(array &$install_state) {

// If the multilingual config checkbox were checked.
if (isset($install_state['varbase']['enable_multilingual'])
&& $install_state['varbase']['enable_multilingual'] == TRUE) {
&& $install_state['varbase']['enable_multilingual'] == TRUE) {

// Install the Varbase internationalization feature module.
$batch['operations'][] = [
Expand Down

0 comments on commit 5a4bde7

Please sign in to comment.