For a while now I've been wrestling with the issue of dynamic forms that change depending on previous input. This is my first Symfony project, so I'm still getting the hang of how everything works together.
What I have is a form type for selecting periods of time. There are preset periods (such as 1min, 5min, 30min, 1hour etc) and a custom text box for entering a certain number of seconds. This form can be embedded in other forms, so (afaik) doesn't have a standard ID that can be accessed. I would like the custom time text box to be only visible if the "Custom Time" (empty value) is selected from the preset time list.
Alternately, the preset values could just write a value in seconds to the custom value (and would need to have the preset box changed to custom on modification of the custom text box). That would be acceptable too.
If the form builder wasn't being used, this would be trivial JavaScript but rather than hacking something together, I would much rather do it properly in reusable code so that I know for the future. To my way of thinking, if there was a way of specifying a custom form rendering template in the form type itself that applied only to that form, that would be fantastic.
Fingers crossed that there's a good solution to this!
Edited: Also, I'm using Twig for rendering all my views.
Results of a bit of research concluded that the best way to do this is to add field overrides for the widget:
/src/PWT/DataBundle/Resources/views/Form/fields.html.twig:
{% block periodChoose_widget %}
<script type="text/javascript" src="{{ asset('bundles/databundle/js/periodChoose.js') }}"></script>
{{ block('form_widget') }}
{% block %}
Then add the following to each of the app/bundle config files:
/app/config/config.yml:
imports:
data_bundle:
resource: #PWTDataBundle/Resources/config/config.yml
/src/PWT/DataBundle/Resources/config/config.yml:
# Twig Configuration
twig:
form:
resources:
- 'PWTDataBundle:Form:fields.html.twig'
I realized that I would need to add something in the global app config if I wanted to reuse the form across multiple bundles. This allows the bundle to be reasonably self contained, with only a small reference in the overall app config. The javascript is not included until the form is used, and the twig block has very little overhead compared to other solutions (afaik).
This way, the javascript that goes with the form is included whenever it is used, and to modify it only requires modification of the bundle, not anything in the overall app config/views. It also breaks the views up so that they don't have a long list of extends this, extends that (like would need to be applied if you had a bundle style that added formTheme tag to override on the base bundle template).
Some of those last 2 paragraphs is speculation, and there could easily be a far better way to do this but this is an adequate fit for me to move on. If anyone has a better solution, I would be happy to read about it.
Related
I'm trying to set up my website so changing one config value reflects in a site-wide change. The most obvious example is changing the site title.
I tried using config('constants.site_title') or something like that but it doesn't look like it can be accessed on the twig without initializing it in the php code section.
I tried using plugin's Settings as outlined in https://octobercms.com/docs/plugin/settings#backend-pages but that also was hard to access from the twig views.
Finally I found that using theme custom form data works (see https://octobercms.com/docs/themes/development#customization). Is this the right way to do it? I feel like this use case must have been encountered before and there must be a best practice for it.
Is this the right way to do it?
I think its a reasonable choice - supports most of field types including repeater, has its own "Configure customization options for the active theme" permission, and is easily accessible everywhere in Twig with {{ this.theme.<field_name> }}
I am about to build a modular page theme in grav (which uses twig as a template engine).
By default, a modular theme consists of at least two twig template files: one provides the page frame including the head and another one that provides the markup of a content module. Grav's standard theme provides examples of these templates:
modular page frame example
module example
As you can see, the module (example) does not have a head or javascripts block to add javascripts to the html head. (This wouldn't even be possible, as a typical modular page consists of multiple modules. So if two different modules would try to change the javascripts block the last one would overwrite the changes made by the first one which would lead to unintended results).
However, it happens that I need to add some javascript code to the head of my page from within a module. I thought about a solution and had the idea to add the javascript-code as a property to an object that exists "site wide", namely the pages object.
This leads to my question:
How can I add properties to an existing object using the twig syntax?
Apart from that I would also like to know if you can think of any reasons why this solution could possibly not work. But if you help me with the syntax, I can also just give it a try...
Manipulating the state of an object shouldn't be done in a Twig template. The view should be outputting data that has been supplied to it.
try to change the javascripts block the last one would overwrite the changes made by the first one which would lead to unintended results
See this answer here for an explanation of how to have multiple blocks append content to a block rather than overwrite it. You can simply have a 'extra_scripts' block or something like that, then append any required javascript for a particular module to that block as per the described link. Essentially you need to use {{ parent() }} to ensure content is included from the parent
I LOVE PHPStorm. However, I'm looking to take the 'PHP Class' file template, and essentially add a custom field in the creation for 'Extended Class'. I see no way to do this.
Is this possible?
Thanks in advance.
EDIT2: I asked a follow up but then figured it out by tinkering with it. My original question remains, however.
You cannot extend dialog window .. unless, maybe, via custom plugin (which has to be written in Java).
You can easily customize actual file template by adding new or removing existing elements -- Settings | Editor | File and Code Templates | Templates --> PHP Class. You can add any static text, use standard/built-in variables or perform some manipulations on values from built-in variables ... but any custom variables will be ignored (as dialog does not ask for their values).
Create and use separate file template -- this way IDE will ask for values for those additional variables. Unfortunately functionality of "New PHP Class" dialog window (like sync name space/file name editing etc) are not available here as it will be a completely different dialog.
P.S.
Some related tickets:
https://youtrack.jetbrains.com/issue/WI-8265 -- currently there is no plans to extend this dialog with extra "Extends" and "Implements" fields.
https://youtrack.jetbrains.com/issue/WI-5207 -- not sure how relevant it is now (because originally it was asking about adding namespace) .. but overall it's still related
https://youtrack.jetbrains.com/issue/WI-9790
I've been asked to create a CakePHP plugin that would allow users to dynamically generate forms. This doesn't sound too hard in and of itself (pull fields from DB, figure out what kind of input, show in HTML), but I don't understand how I can "save" the forms to a new page.
From my understanding, Wordpress circumvents this problem by storing all data in the database, and can use shortcodes to just "insert" the form into another page. Seeing that CakePHP handles relies on CTP files to define views (and not Database fields), I don't see any simple approach to doing this.
Is this goal of a WYSIWYG form editor possible in CakePHP (without having to resort to terrible practices like editing CTP/PHP files from within the app)? If so, can you point me towards some strategies I can use to develop this plugin?
I'm not sure what you do with the dynamic form, to me it sounds like you're describing two totally different things.
Save the structure of the generated form somehow in the DB, key/value, serialized via php or as json object
Return the data, set it to the view
Write a helper that turns the structure that describes the form again in
The editable version, restore the form
Parses the structure and returns the HTML of whatever the fields of the form should do
You'll have to write your own parser that looks for things like [gallery id=1] and replace that with the result of a function call. Assuming that this is what you want to do. So you'll have to map the "gallery" to a functional call. I think the best here would be to use requestAction()
The *.ctp files are what WP considers it's template files, and honestly, Wordpress is just a horrible awkward piece of software from a developer perspective.
I have an empty template with just a safecracker form consists of 2 custom field. When i try to load it up in the browser, it takes about 2-3 seconds to load. Looking in firebug i see the following GET
http://localhost/ee1/?ACT=7&ui=core,widget,mouse,position,draggable,resizable,button,dialog,datepicker&plugin=scrollable,scrollable.navigator,ee_filebrowser,ee_fileuploader,markitup,thickbox,toolbox.expose,overlay,tmpl&v=1308711156&use_live_url=y&include_jquery=y
I don't need to load most of the things showing in the url above. How can i control what is loaded with safecracker form. I will be loading jQuery/UI directly from google so i don't need safecracker to load it. I'm able to remove jQuery and datepicker from safecracker form by doing datepicker="no" include_jquery="no", but what about removing other things mentioned in the url such as scrollable, draggable, thickbox and other things.
The Short Answer:
Sadly, there's no way to disable many of the included SafeCracker JavaScripts.
The Long Answer:
SafeCracker is designed to be as general purpose as possible and to suit the needs of the largest audience.
With this tradeoff, SafeCracker bundles jQuery, jQuery UI Widgets, jQuery Tools and various other jQuery Plugins and functions to facilitate client-side form validation and presentation of custom fieldtypes.
As you've shown, you can disable jQuery from automatically being output by SafeCracker — say, if you want to use a newer version that what's available from EE's Control Panel — and can you disable the DatePicker, but as far as I know you can't prevent the rest of the JavaScripts from being output without hacking the core SafeCracker library.
Understandbly, I'm not a big fan of this "include everything and the kitchen sink approach" and hate seeing extraneous and bloated scripts being added to the page — especially ones that I would never use in the first place.
Nevertheless, it's clearly a design decision that SafeCracker was built to include all of the JavaScripts it outputs since there isn't an approved way to remove them. So we're essentially stuck dealing with them for the unforeseen future.
If you feel strongly about this, I would suggest submitting a Feature Request to EllisLab to allow more configurability to SafeCracker's sloppy way of outputting more JavaScripts that what's really necessary.
For the curious, take a peak around Line 887 in the following file to see just how many jQuery UI Widgets are bundled in the SafeCracker tag pair.
/system/expressionengine/modules/safecracker/libraries/safecracker_lib.php
Note: If you want to take matters into your own hands, you can comment out some of the items in the $js_defaults and $ui array, which will remove the JavaScripts from being output. However, in doing so you assume all responsibility and risk for SafeCracker's operation.
I'm assuming this has changed since rjb's answer, but SafeCracker provides two parameters I found helpful in drastically reducing page load time:
include_jquery="no" safecracker_head="no"
Relevant documentation