Drupal - dynamic options for text_list field - forms

I have a custom node type for which I want to have a field that uses a special combobox based on list_text. When one chooses the type list_text it is normally possible to enter a static list of selectable texts, however, I want this list to be dynamic, i.e. based on the results of a db_query. What is the best way to do this using Drupal 7?
A simple example for clarification: A node of this custom type X contains a field that points to another node, so whenever a node of type X is created I want a combobox that contains all other nodes.
(Best solution would be to only display the combobox during node creation, and no longer during edit. But I could also live with it if the combobox was shown during the edit as well.)
I have tried to customize options_select by defining my own data type and implementing hook_options_list accordingly. The combobox was displayed during creation with the correct values, however, I could not save it.. I have no idea what went wrong there, but on the first submit it would change to a different theme, and when I tried again I got an internal server error. Am I on the right track at all with defining a completely new data type for the field? there surely must be a simpler way?

You're right in that you don't need a new datatype. Here's a good tutorial on how to do this. It's not specifically for D7 but I didn't see much that wasn't still applicable. There may be a better way to do it in D7 specifically but I would love to know it too if so :)

The tutorial linked by allegroconmolto sent me on the right way. Thanks for that.
Here's the simpler way of doing it: tutorial
Basically, it is, as I assumed, a common problem and hence a simple solution for it was included in the webform module by now. It provides a hook_webform_select_options_info which can be used to register a callback method. The callback method is then called each time a corresponding option select of a webform is shown, so that you can easily fill it with the results of a dbquery or anything else. Works like a charm and takes next to no time to implement.

Related

AnyLogic - make many edit boxes visible depending on variable

Here is a simplified example of my problem:
I want to make the edit box 1 or 2 visible, depending on whether a checkbox of box 1 or box 2 is checked. The variables "variableCheckbox1" and "variableCheckbox2" are connected to the respective ceckboxes.
I have written a function as shown in the picture. The function is called from the EditBox 1 ("Enabled"). This is a possible solution for ONE box each.
Does anyone have an idea if there is a more elegant way to do this for a very high number of checkboxes / edit boxes?
A possible solution would be to write a function with a switch case for each variable in my model, called from each box. A very inelegant solution would be to make a single function for each box, but I don't consider that a good way.
If you want this for a large number, turn the entire setup into its own agent type: Checkbox and editbox. Instantiate as needed, you can add a parameter to specify what this is about exactly...
This would be the most generic and powerful approach...
if this works for you, you can use replications for checkboxes and editboxes and associate them together through the index... and then just add the code in the visible field of the edit box... Of course you will have to determine their positions programatically

Possibility of a multilanguage 'source' name with Twincat Eventlogger

Roald has written an excellent guide for the Twincat Eventlogger.
https://roald87.github.io/twincat/2020/11/03/twincat-eventlogger-plc-part.html
https://roald87.github.io/twincat/2021/01/20/twincat-eventlogger-hmi-part.html
For us this is exactly what we want, there is however 1 thing I haven't figured out. How to get the sourcename of the alarm in multiple languages in the HMI. params::sourceName gives the path in the software (example: MAIN.fbConveyor1.Cylinder1) This path can be customized when initializing the alarm (as Roald has shown). This doesn't work in my case, since I would like to define a generic alarm (example: "Cilinder not retracted within maximum time") that is instantiated multiple times.
I was thinking of using the source as a way to show the operator where the alarm occurs. We use this way (path) already for saving machine settings among other things. The machines we build are installed all over the world, so multilanguage is a must.
Beckhoff does support multilanguage alarm names (when defined), but the source is not defined, but dynamically generated.
Anyone have an idea how this problem can be solved?
If I understand your question correctly, then being able to parameterize the event text with information of the source of the problem should help you out.
If you define the event text as Cylinder {0} has not retracted in time. then you can add the arguments of that text during runtime.
IF bRaiseAlarm THEN
bRaiseAlarm := FALSE;
fbAlarm.ipArguments.Clear().AddString('Alice');
fbAlarm.Raise(0);
END_IF
However, since this also stated in the articles you mentioned, I am unsure if this would solve your problem.
'Alice' in this example, can be hard to localize. The following options come to my mind.
The string can be based on an ENUM. Enums can have textlist support, so if you add your translations there, that should allow multilingual output. However... this does require a lot of setup, placing translations inside your code, and making sure the PLC application is aware of the language that the parameter should use.
Use tags to mark the source device, as tags can be language invariant. It is not the most user-friendly method, but it could work for you. It would become something like: "Cylinder 'AA.1123' did not retract in time.". 'AA.1123' as a tag would have to be stored inside your PLC code as a string. You will have to trust that your operator can relate the tag back to the actual source.
Hopefully, this helped, or else please help me understand the problem better.

Implement the Lead conversion using custom button

I will need to create a Custom Button "convert lead" that will perform the same functionality as Standard Button "Convert" when the button is clicked.
What is the best approach to do it..?
That's a very broad question. What exactly you need, what have you tried so far? Do you really need just a button that opens the conversion page or something more?
If you want to somehow recreate it with Apex... Core of the coded solution would be the Database.convertLead method. You don't pass to it whole leads (like to Database.insert for example) but instead just their IDs + bunch of control flags to make it do exactly what you need. Read up about LeadConvert object. And similarly you can get Account/Contact/Opportunity ID from the result object if it succeeded.
I don't think there's programmatic way to access field names & mappings defined by administrator for lead conversion. Maybe you'd need to store this info somehow (in helper object? custom metadata?). So then you'd query the field names from metadata, then query the lead fields and finally display table of fields & mappings to the user.

NSComboBox with alternative ways of writing

I'm trying to implement a autocomplete function into a small app. I want to give the user the possibility to write down a city (from a long long list of cities) into a NSComboBox. That works fine, as long as the user is using the exact writing of the city inside my array of city names. But if they use, for whatever reason, a different spelling, it fails and the city is not found.
So if the user is looking for "Köln" for example, it's not a problem, but if he is looking for "Cologne" he wouldn't be able to find it.
For this I have, for each city an additional array of alternative spellings.
Now I would love NSComboBox (or any other type of TextField) to look not only in "city.name" but also within the array "city.alternativeNames". The shown value should as well represent what the user is writing.
I don't want to create an entry in the list of the pop-up part of NSComboBox for each alternative name, as that would make the list even longer than it is and would confuse people (cologne alone has 85 different spellings).
Thanks for your suggestions, I'm completely new to NSComboBox.
Interesting problem. I think probably you need to choose a different way to structure your data to make it easier.
Consider the lookup method (matching a string). Structuring your data for this case should account for the preferred spelling of each city (preferred by you, for the scrolled list).
How about a flat array of cities (to allow a simple search based on user spelling without also having to check each possible array of alternates and manage sorting them properly) but each has an optional (can be nil) "preferred spelling" pointer to the "correct" one. When displaying the options in the combo box, show the array filtered by those with no optional preferred spellings plus the currently-typed partial/full completion of the alternate spelling?
So a City has a name property and an optional preferred property. In your case, if Cologne is preferred, the Köln instance would have Cologne set as preferred. Köln would only appear in the list if the user typed it (even partially) and it would automatically be in the correct alpha-sorted position (assuming your cities are kept sorted).
Does this make sense or do I need to rephrase? Haven't had enough coffee this morning. :-)

Why does the Function Module name of a Smartform change (sometimes)?

Not really critical question, but i'm curious
I am working on a form and sometimes the generated function name is /1BCDWB/SF00000473, and sometimes /1BCDWB/SF00000472. This goes back and forth.
Does anyone know what's the idea behind this? Cuz i'm quite sure it's not a bug (or i might be wrong on that).
It is not a bug. You always have to use SSF_FUNCTION_MODULE_NAME to determine the actual function module name and call it dynamically using CALL FUNCTION l_function_module.
Smartform FMs are tracked by internal numbering and thats saved in the table STXFADMI. You would always notice the different number in Development System if you have deleted any existing Form. Similarly, you would also notice the different number in your Quality system based on the sequence the forms are imported in QAS and the forms as well (as test forms are not migrated to QAS.
Similar behavior is also true for Adobe Form generated FMs.
You need to understand that every smartform has a different interface and hence the automatically generated function module needs to have different import parameters.
Due to this reason the 'SSF*' FMs generate a FM specific for your smartform. The name of the 'generated' FM changes when you migrate from one system to another. And that's the reason why you should use a variable while calling the 'generated' fm and not hardcode it.
The same goes with Adobe form as someone has rightly said in this thread.