Update app_list_strings from Logic Hook

Is there a way to update $app_list_strings from a Logic Hook without it requiring a Repair and Rebuild to work?

I have a situation where the dropdown choices change based on the activty being done. I want to enable a user (with sufficient privileges in the Security Roles) to use a Text Area to enter the Dropdown Labels and from there, the code will create the Dropdown Item Names. I have this part working using the following code (I fetch the contents of the Text Area into $sourceText)

$sourceTextArray = explode( PHP_EOL , $sourceText);
$stringStore = array();
foreach( $sourceTextArray as $key => $listEntry ) {
    $cleanString = trim( filter_var( $listEntry , FILTER_SANITIZE_STRING , FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_BACKTICK ) );
    $indexString = strtolower( preg_replace( "/[^A-Za-z0-9]/" , '' , $cleanString ) );
    $stringStore[$indexString] = $cleanString;
}

The $app_list_string entry is (temporarlity) in the dom list ā€œtest_dropdown_listā€ and I have tried, in a before_save Logic Hook to save the contents of the array into $app_list_strings[ā€˜charitable_activities_listā€™] using

global $app_list_strings;
$app_list_strings['test_dropdown_list'] = $stringStore;

and

global $app_list_strings;
$GLOBALS['app_list_strings']['test_dropdown_list'] = $stringStore;

and in both cases, if I immediately do

echo '<pre>';
var_dump($app_list_strings);
echo '</pre>';
exit;

it does sshow that the $app_list_strings global variable has been updated. But when I go to select from the Dropdown, it still uses the original dropdown list done before the Repair and Rebuild.

I have tried manually editing the file at
custom/application/Ext/Language/en_us.lang.ext.php
and that does create the result I want, but obviously that will only last until the next Repair and Rebuild,.

I also have to beleive there is a way to update the $app_list_strings variable in code instead of brute-forcing via file edits.

Any ideas?

Donā€™t you just love it?

Iā€™ve been working on this for two days and just after posting this, I found a solution.

Details are at

https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_12.0/Architecture/Languages/Managing_Lists/index.html

where you access the class DropDownHelper from {SuiteCRM_Directory}/modules/Studio/DropDowns/DropDownHelper.php
but below is the summary.

Create a custom module (AYU_CharitableProjects) whose only purpose is to set the Dropdown list values. This way, you know the only way the dropown list values change is when someone wants them to change. Also, by creating a sepoarate custom module, you can save the list for different events and return to that if/as needed.

The only field I added to the custom module was a TextArea field: fund_project_list_c

Use SuiteCRMā€™s Admin-DropDown Editor to create a Dropdown called funds_project_dom_list and give it one entry (for now)
GeneralUse => General Use

into {SuiteCRM_Directory}/custom/Extension/modules/AYU_CharitableProjects/Ext/LogicHooks/setFundsProjectList.php

<?php

$hook_version = 1;
$hook_array = Array();
// position, file, function
//
$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(
    80,
    'Update Dropdown list of Charity Projects',
    'custom/modules/AYU_CharitableProjects/listCharityProjects.php',
    'listCharityProjects',
    'userInputList',
);

into {SuiteCRM_Directory}/custom/modules/AYU_ChartiableProjects/listCharityProjects.phpā€™ put

<?php

if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class listCharityProjects {

    function userInputList( $bean, $events , $arguments ) {

        // Set the dropdown list for the Charitable Projects
        // so each Event can use a Project list specific to that Event
        // The dropdown list is just used as a filter for what is allowed as input
        // Once input is saved (into the MySQL database as text)
        // The dropdown list can be edited for future uses

        if ( !empty($bean->fund_project_list_c) && ( $bean->fund_project_list_c != NULL )) {

            require_once 'modules/Studio/DropDowns/DropDownHelper.php';
            $dropdownEditor = new DropDownHelper();

            // Initialize the arrays to be used to update the dropdown list
            $tempListArray = array();
            // Use as the Name, the name given to the list when it was created (Studio?)
            $tempListArray['dropdown_name'] = 'funds_project_dom_list';

            // Set the first choice of the list to "General Use"
            // See below for an explanation of the syntax
            $tempListArray['slot_'. 0] = 0;
            $tempListArray['key_'. 0] = 'GeneralUse';
            $tempListArray['value_'. 0] = 'General Use';
            $tempListArray['use_push'] = '';


            // We start the array list at 1 since we have the first (0) entry set above
            $itemCount = 1;
            $sourceTextArray = explode( PHP_EOL , $bean->fund_project_list_c );
            foreach( $sourceTextArray as $key => $listEntry ) {

                // Convert the source text into an array
                $cleanString = trim( filter_var( $listEntry , FILTER_SANITIZE_STRING , FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_BACKTICK ) );
                $indexString = preg_replace( "/[^A-Za-z0-9-]/" , '' , $cleanString );

                // Use the array contents to populate the inputs needed
                // by DropdownHelper
                $tempListArray['slot_'. $itemCount] = $itemCount;
                $tempListArray['key_'. $itemCount] = $indexString;
                $tempListArray['value_'. $itemCount] = $cleanString;
                // Set 'use_push' to true to update/add values while keeping old values
                // Set "use_push' to empty ('') to delete old values and replace with these
                $tempListArray['use_push'] = '';
                $itemCount++;
            }

            // Save the new dropdown list
            $dropdownEditor->saveDropDown($tempListArray);

        }
    }
}

Repair and Rebuild

Create a Dropdown field in whaever module you want and have it use, as its list: funds_project_dom_list

Create an (or open an existing) entry in CharitableProjects with some list items entered into the TextArea and Save the entry.

This updates the dom list with what you have in the TextArea and puts a filtered version of the TextArea entry as the ItemIndex.

2 Likes