Currently I am loading form element definitions from an ini file that looks like this as an example:
[tickets] ; tablename
ticket_number.name = "ticket_number"
ticket_number.type = "text"
ticket_number.label = "Ticket ID: "
ticket_number.options.validators.alnum.validator = "alnum"
ticket_number.options.validators.strlen.validator = "StringLength"
ticket_number.options.validators.strlen.options.min = "6"
ticket_number.options.validators.strlen.options.max = "20"
assigned_date.name = "assigned_date"
assigned_date.type = "text"
assigned_date.label = "Entered Date: "
Now lets say I have a form element (select / dropdown box), and I want to load the options from a database (lookup table), is there a way to automatically configure that within the form elements ini config, or does that have to be done within the form class upon creation of the element?
Well, there are ways to do this, the question is whether you actually want to do this.
Possible ways to do it:
Create your own smart select element, which has the ability to build a multiOptions list based on some option values which you provide in your ini file.
or, before creating the form instance, inject the multiOptions values into the Zend_Config file.
or, after you've created the form instance, set the multiOptions using $form->getElement('name')->setMultiOptions(array(..))
Related
I am modifying the template of a plugin, and I want to retrieve a field from the content element.
Using f:debug I see the only data available is from the plugin itself, and none from the content element.
Is there any way I can perhaps insert the field I need in the plugin settings?
eg. something like:
plugin.tx_plugin.settings {
contentUid = TEXT
contentUid.field = uid
}
The best way I can think of to do this is with a custom ViewHelper. Something like:
namespace MyVendor\MyExtension\ViewHelpers;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
class ContentUidViewHelper extends AbstractViewHelper
{
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
return $configurationManager->getContentObject()->data['uid'];
}
}
In your Fluid template:
<mynamespace:contentUid />
This will get the uid of the content element, but you can get any field this way. Just change the key of the data array to the field you need.
In the corresponding method (like the listAction or showAction) of the controller you can get the data of the content element in the following way:
$contentObject = $this->configurationManager->getContentObject();
$this->view->assign('contentObjectData', $contentObject->data);
As far as I know, you can't get to that data using typoscript, but I've never needed it anyway since I've been using the above code in the controller.
settings do not have stdWrap-type per default, but only string. So you can not use cObjects as values.
For one (or a few) settings, you could do the stdWrap-processing in your method/class yourself:
$settingsAsTypoScriptArray = $this->objectManager->get(TypoScriptService::class)->convertPlainArrayToTypoScriptArray($this->settings);
$contentObj = $this->configurationManager->getContentObject();
if ($contentObj === null) {
$contentObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
}
// now override the values in the settings array with the processed value
$contentUid = (int)$contentObj->stdWrap($settingsAsTypoScriptArray['contentUid'], $settingsAsTypoScriptArray['contentUid.']);
If you wanna have many settings to be stdWraped, have a look into EXT:news. Georg implemented an interesting concept via useStdWrap configuration.
I want to build a multipage from.
The first page asks for first name and last name.
I want to greet the user with his first name in the second page.
The best way to do this is to use Live Merge Tags with Populate Anything:
https://gravitywiz.com/documentation/gravity-forms-populate-anything/#live-merge-tags
If you collected the user's first name in a Name field on page 1, you could great him in the field label for a field on page 2 like so:
Hello, #{Name (First):1.3}
(In this example, the field ID for the Name field is 1. The 3 refers to the first name input of a Name field and will always be 3).
If avoiding another plugin (as useful as that one is), you can use either the pre_submission_filter or pre_submission hooks to do this.
If their name was field 1 and lets say the field you'd like to show is field 2...
// THESE FOUR FILTERS WORK TOGETHER TO PRE-POPULATE ALL SORTS OF STUFF, AND YOU CAN ADD TO THIS AS NECESSARY. MINE IS ABOUT 1500 LINES LONG AND IS USED BY SEVERAL FORMS.
add_filter('gform_pre_render', 'populate_forms');
add_filter('gform_pre_validation', 'populate_forms');
add_filter('gform_pre_submission_filter', 'populate_forms', 10);
add_filter('gform_admin_pre_render', 'populate_forms');
function populate_forms($form) {
$form_id = $form['id'];
$current_form = 2; // pretending the form id you are working on is 2.
$future_form = 10; // imaginary form you'll create later for another purpose.
switch($form_id) {
case $current_form:
$first_name = !empty(rgpost('input_1_3')) ? rgpost('input_1_3') : null; // gets the value they entered into the first-name box of field 1.
foreach ($form['fields'] as &$field) {
if ($field->id === '2') { // Make as many of these as necessary.
if ($first_name) { // make sure there's actually a value provided from field 1.
$field->placeholder = $first_name; // not necessary, just habit since sometimes you'd need to have a placeholder to reliably populate some fields.
$field->defaultValue = $first_name; // this is the piece that will actually fill in the value like you'd expect to see in your question.
}
}
}
break;
//case $future_form: do more stuff.
//break;
}
return $form;
}
That should be a decent start for your functionality plugin where you can populate the current and future forms without much hassle. This can also be done with the gform_field_value hook; I've always found the language a bit clumsy with that one, personally.
The plugin mentioned earlier is definitely neat, but I found myself wanting to rely on that stuff less and less.
I have several forms for a single content type in Drupal 7. The purpose of this is to initiate different workflows when the user submits the form, depending on the type of information included, and defined by the /url for each. These forms are on different pages and the fields shown on each are defined in a custom module. For example:
.../form1 initiates workflow 1 and displays fields a, b, e, f, g
.../form2 initiates workflow 2 and displays fields a, b, c, e, h
.../form3 initiates workflow 3 and displays fields a, b, f, x, y
In this module it looks something like this:
function my_custom_module_custom_form() {
// Build Form
$form = getForm('content_type');
switch (strtolower($form['#action'])):
case('/form1'):
$form['field_some_field']['#access'] = FALSE;
switch (strtolower($form['#action'])):
case('/form2'):
$form['field_other_field']['#access'] = FALSE;
I would like to have a page template for each form, so I can specify what goes into each, rather than showing/hiding every field for each within the module, which is cumbersome, given the number of fields.
Can I create a page template for each form and link the submit button to trigger a particular action in the module?
Note: adding dependencies or using separate content types is not applicable to our use cases. If there are errors in the code above, it is only that i've given a quick example here, the actual module works.
Thanks for the help!
I have created the theme suggestions for a particular node form:
Change the function name to THEMENAME_preprocess_node
Change initial value of template_filename to 'node'
Account for dashes in aliases by adding this line below the second if statement: $alias = str_replace('-', '_', $alias);
So here's what it looks like now:
function THEMENAME_preprocess_node(&$variables, $hook) {
// Node template suggestions based off URL alias
if (module_exists('path')) {
$alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
if ($alias != $_GET['q']) {
$alias = str_replace('-', '_', $alias);
$template_filename = 'node';
foreach (explode('/', $alias) as $path_part) {
$template_filename = $template_filename . '__' . $path_part;
$variables['theme_hook_suggestions'][] = $template_filename;
}
}
}
}
Please let me know if you need any more details.
I have a w2ui form that contains a w2ui Drop List of choices. The choices will be different depending on what the user selected to bring up the form. My question is: can the contents of a Drop List be changed after it has been rendered?
With standard HTML controls, I would do something like this:
$("#mySelect option[value='xyz']").remove();
or
$("#mySelect").append('<option value="abc">abc</option>');
Can these kinds of operations be done with a w2ui Drop List? Any example code?
In w2ui 1.5 you can use $jQueryElement.w2field() to access the w2fild object - and then manipulate it.
Example:
var field = $("#my_input").w2field();
field.options.items = ["my", "new", "items"];
// optionally: pre-select first item
field.setIndex(0);
// if you do NOT use "setIndex" you need to call "refresh" yourself!
// field.refresh();
Note: setIndex() internally calls refresh() - so as stated above, you do not need to call refresh yourself in that case.
If you want to completely clear/empty your field, you can call field.reset().
Edit: after clarification that it's about a form field:
// Note: ``this`` refers to the w2form
// ``field[8]`` refers to a field of type "select"
this.fields[8].options.items = ["my", "new", "items"];
this.record = {
field_select: 'new'
};
this.refresh();
I don't know too much about Joomla, but I'm trying to work with a Menu on a Joomla site. In the Database I can see a column called params in the menu table, and it has some data I need. The params column has this data:
categories=446
feedLink=1
fusion_item_subtext=
fusion_columns=1
fusion_customimage=
splitmenu_item_subtext=
page_title=
show_page_title=1
pageclass_sfx=
menu_image=-1
secure=0
I know I can do a mysql query, get that column and parse the value using string manipulation/regex, but that doesn't sound like the right way.
I have seen some code in Joomla that looks like:
$cid = $params->get('secure');
Does Joomla have a special way to query and return objects so that these params are accessible with this type of syntax?
Right way is to use JMenu::getParams method
$app =& JFactory::getApplication();
$menu =& $app->getMenu();
$params = $menu->getParams($menuItemId);
$params->get('paramName');
Yes, Joomla does have special way of getting the parameters in an easily accessible object based on JObject.
you can get the entire site menu with this
$menu = JFactory::getApplication()->getMenu();
$item = $menu->getActive(); // will get active menu item. can use getItem() instead to get specific item
$item->get('parmName');
This is not exact code, more like pseudocode. This will get you on the right track...
Helpfull Stuff:
Joomla Framework API
JMenu Documentation
first you get a JApplication instance like this
$app = & JFactory::getApplication();
or for joomla 1.5 use:
global $mainframe //to get JApplication object
get JMenu instance like this:
$menu = $app->getMenu();
you can get active menu params or any other menu params like this
$active = $menu->getActive(); //get active menu
$menuInstance = $menu->getActive($Itemid); // to get Itemid use JRequest::getInt('Itemid', 0);
here you have an StdClass object with params field inside, now u use JParameter class like this
$menuParams = new JParameter($menuInstance->params);
here you have it, to get any parameter you want:
$someParam = $menuParams->get('some_param', 'default');