Reusing the CQ5 Form into the mywebsite components is not showing up the End of the Form section - aem

I am trying to reuse the Form into my project components.
I have copy pasted the entire form folder from "/libs/foundation/components/form" to my project "/apps/mywebsite/components/form".
But when i am trying to use the form from mywebsite in the parsys the from shows only Start of the from.
Where as when i tried to use the form from the foundation in the same page parsys it shows both Start and End of the form.
Observation:
From the content, when i am using the foundation form the in the page content i can see the start and end nodes. where as when i am using the mywebsite form start node alone is created.

The form end is added/deleted by the fixStructure() method of the FormParagraphPostProcessor class. This post processor listens for creation and deletion of form start and form end paragraph and creates/removes the other paragraphs accordingly.
if ( ResourceUtil.isA(res, FormsConstants.RT_FORM_BEGIN)
|| ResourceUtil.isA(res, FormsConstants.RT_FORM_END)) {
if ( FormsHelper.checkFormStructure(res) != null ) {
logger.debug("Fixed forms structure at {}", contentResource.getPath());
}
}else {
fixStructure(res);
}
This class depends on the FormConstants.java where the form start(RT_FORM_BEGIN) and the form end(RT_FORM_END) are defined as "foundation/components/form/start" and "foundation/components/form/end" respectively. Due to this the post processor doesn't process the form start / end that is present within your project.
To make your custom form component working you may consider one of the following possible options:
Add the sling:resourceSuperType property for your project form start as "foundation/components/form/start". This would create a form end, but it would be of type foundation/components/form/end and not your project form end.
In case you do not want the default form end but your custom form end, then you may need to create a custom post processor which listens to your form start and end and fix the structure accordingly. This requires modifying few other java classes like FormsHelper.java etc, as they are also dependent on the FormConstants.java. Also make sure that the imports in the start.jsp are changed accordingly.
Finally, if you do not want to create custom classes, you can copy the default form start to "/apps/foundation/components/form/start" and make your modifications on top of it. But you need to be careful while using this approach as this is a global change and would affect the other projects that are using the default foundation form start.

Related

Setting Date as a prop in Adobe DTM

I'm trying to use Adobe DTM to pass the date to a prop variable but haven't had much success. The final output should be a prop report in Adobe that'll provide me traffic data for specific dates (5/11/16, 6/15/15 etc). The ultimate goal for setting the dates as a prop is to be able to classify a range of dates based on various business needs.
Could anyone point me in the right direction for getting this done? I am assuming I'll have to add a line of code in the s.code file that'll define s.prop5 = ...
Thanks
Based on your comments, it sounds like you are just looking to pop something with the current date stamp with "MM-DD-YYYY" format.
As Gigazelle mentioned, you can create a Data Element to return the value, and then reference it for setting your prop. However, throwing a data layer into the mix may be overkill for you, depending on your needs/limitations.
Data layers are meant for exposing data that DTM can't feasibly/reliably automate on its own via built-in features or hosting an autonomous js snippet.
The only reason you might want to consider having your site push it to a data layer, is if you want to generate the date via server-side coding to ensure the date is generated within the same timezone setting for all visitors. If you generate it client-side, it will be generated according to the visitor's browser/system settings. Since visitors are from all over the world in different timezones, the data may not be as consistent (even if you add additional code to change the timezone offset, it still may not be 100% based on browser version/security settings, or visitors who alter their browser/system date/timezone settings).
So, if you want to ensure the best accuracy, then I suggest you output the value via server-side code, and put into a data layer. How you do that depends on your server and what language you have at your disposal for your web pages being served up. Here is a very basic example using PHP:
<script>
var dataLayer = {
currentDate : '<?php echo date('m-d-Y'); ?>'
}
</script>
This will have the server generate the date stamp and output a js object called dataLayer with a property currentDate you can reference. You can create a Data Element as Type "JS Object" and for Path, put dataLayer.currentDate, and then reference your Data Element elsewhere (see below).
If that's too much trouble for you or if you want to keep it pure client-side/DTM and are okay with the potentially lower consistency...
Within DTM, go to Rules > Data Elements, and click Create New Data Element.
Name it "currentDate" (no quotes).
For Type, choose "Custom Script", and click Open Editor, and add the following:
var t=new Date(),d,m,y;
d=t.getDate();
d=d<10?'0'+d:d;
m=t.getMonth()+1;
m=m<10?'0'+m:m;
y=t.getFullYear();
return m+'-'+d+'-'+y;
Click Save and Close and Save Data Element.
Now you can reference the data element to pop prop5. How you do it depends on how you've setup Adobe Analytics within DTM. For example, if you set it up as a tool and only want it to pop on initial page view, you can open your AA tool config, go to the Global Variables dropdown, and set prop5 there. You reference it as %currentDate%
You can do the same %currentDate% syntax in a Page Load Rule or other rule or any other place that uses DTM's built-in fields.
Alternatively, if you need to reference it within javascript code (e.g. if you are setting prop5 within s.doPlugins or some other Custom Script box, you can reference the data element like this:
s.prop5 = _satellite.getVar('currentDate');
Set a JS variable on your site (such as within a data layer) that outputs the date in the format you wish to collect it in. Something like var d = Date();
In DTM, go to Rules > Data Elements and create a data element that maps to the JS variable you created on your site
If you want prop5 to be defined on every page, click the gear icon and map prop5 to your data element name by using %DataElementName% (whatever you named your data element in step 2, wrapped in percent signs). If you don't want it defined on every page, go to Rules and create a page load rule, event based rule or direct call rule depending on when you want the variable to trigger. Under the Adobe Analytics section of the rule, map prop5 to %DataElementName% .
This will allow you to collect dates as values, which can then be used in classifications.

redux-form Wizard form with linked fields

I am building a multi-step application form with React. Having first built it with pure internal state I am now in the process of refactoring to Redux using redux-form.
Having used the example here as a basis: http://redux-form.com/5.2.5/#/examples/wizard?_k=oftw7a we have come a good way.
However the problem appears when i have two forms which are supposed to have the same value. During one of the pages i have a name field, that is supposed to be duplicated on the name field of the next page. The opposite should happen if you go back from the last page. Any tips to how this could be achieved?
Using the wizard, you are basically working with the exact same form that's split into multiple pieces. Ultimately it's the same form, because redux-form tracks them by name. It is how the library identifies the pieces of the same form - using the name.
form: 'wizard',
Here you can see that the exact same instance of the form will be shared throughout the pieces. fields work in a similar manner. Each field is defined as part of a form.
As long as you use the same field constants inside the fields object that you pass into the reduxForm function and as long as the value for form is the same, so that they use the same underlying form object, it should work for you just fine.
On one page you should pass in
export default reduxForm({
form: 'wizard',
fields : {
'fieldIWantOnBothPartsOfTheForm',
'someOtherFieldThatShouldOnlyBeHere',
},
...
And then on the other page:
export default reduxForm({
form: 'wizard',
fields : {
'fieldIWantOnBothPartsOfTheForm',
'thirdFieldHere',
},
...
Also, make sure you keep destroyOnUnmount equal to false if you want to navigate back-and-forth.
Hope that helps.

AEM DefaultValue written to JCR

I noticed that when I set my defaultValue for a dropdown, altho it is correctly selected in the drop down when I first add my component to the page it does not write the defaultValue to the corresponding JCR until I edit the component and save it. Even if I just open the corresponding dialog and click OK now my component works as expected because the values have been added to the JCR.
I am sure there is an important piece that I am missing here, does anyone knows how defaultValues that are required in order for the component to render properly can be added to the JCR when they are first added to the page?
Like Shwan say's that's the way it works. The default values or empty texts are only for the dialog. They aren't persisted until the dialog is authored. The properties have to be set by a different method. CQ already ships with this feature and you can do it without any custom code.
Under your component , create a node called cq:template[nt:unstructured] . If all the data is stored on the component node itself , add the default values as properties to cq:template node with name same as the ones in your dialog. In case the data is stored in a child node add a similar node under cq:template node.
Source : http://blogs.adobe.com/experiencedelivers/experience-management/defaults-in-your-component/
I believe that is simply the way it works. The default value specified in a dialog does not get used until the dialog is loaded/saved, so until that happens the node on the JCR repository that is being authored won't have the default value.
We got around this on a project by adding back-end code that was tied to the component (a tag) so that when the component was loaded, if the property did not exist, it would be written with the default the first time. Ex:
if (wcmMode == WCMMode.EDIT )
{
if(!currentNode.hasProperty("SomePropertyThatWillAlwaysExistIfTheDialogHasBeenSaved")) {
currentNode.setProperty("PropertyThatShouldHaveDefault", GlobalConstants.TRUE);
currentNode.getSession().save();
}
}
Like Sharath Madappa say's that's the way it works fine if component name and jsp name same. If you dont have componentname.jsp under component or page, cq:template won't work.(Reference:http://labs.6dglobal.com/blog/2014-07-08/using-the-cq-template/)
If you hava componentname.html under your component, changed the node [cq:template] type to [cq:Template] instead of [nt:unstructured]. In this case, defaultValues can be added to the JCR when they are first added to the page.

Zend Framework Dynamically added fields of a form and populate

I have been attempting to create a form where a user can simply press a button and the form will add a new field for the user to use. I have 2 of these dynamically added field types.
Firstly a field where a user can upload files, by pressing the add button another field is pasted underneath the current field and is ready for use.
I have followed an old guide on how to get this done with a bit of ajax and jQuery.
This guide to be exact: http://www.jeremykendall.net/2009/01/19/dynamically-adding-elements-to-zend-form/
As you can see it's from 2009 and a bit outdated yet it still works under the current Zend Framework version 1.11.11
The problem however arises now that i want an edit / update version of the form. I need to populate it's fields but first of all i need to create enough fields for the data to be stored in. So when there's 3 files that have been uploaded it should create 2 additional fields and place the 3 file names in these fields ready to be edited and updated. Simply using $form->populate($stuff) is not going to work
I just have no idea how to accomplish this and the tutorial on dynamically added fields only goes as far as the addAction and not how to create the editAction under these conditions.
Is there any tutorial out there on how to create and manage forms such as these? I'm sure i am not the only one who's had the idea to builds these kind of forms?
I can add my code if there's a request for it but it's the same as the example from the guide, just a different set of elements in the form.
Adding a small example of it's use.
A user adds an item with 3 files, these files are uploaded along with a filename so in the database it appears like this : File_Id : '1' , File_Name : 'SomeFile' , File_location : 'somewhere/on/my/pc/SomeFile.txt'.
Now the user realizes he forgot a file or wants to delete a file from that list, he goes to the edit page and here i want the form to display the previously added filenames. So if there's 3 files it shows 3 and when there's 2 it shows 2 etc. How do i build a form to dynamically add fields based on the number of uploaded files and then populate them?
Any advice on how to handle this is well appreciated :)
You can make use of the semi-magic setXxx() methods of the form.
Inside the form:
public function setFiles($files) {
foreach ($files as $file) {
$this->addElement(/* add a file element */);
//do other stuff, like decorators to show the file name, etc.
}
}
In your controller:
$files = $model->getFiles();
$form = new Form_EditFiles(array('files' => $files));
By passing an array with key files you will make the form try to call the method named setFiles(), which you have conveniently provided above.
This should push you in the right direction, or so I hope at least.
If I understand you correctly you want to populate file upload fields, which is not possible because of security reasons.
Edit:
You can add Elements inside of the Controller via $form->addElement() (basicly just like the $this->addElement() statements in the Tutorial)

Drupal, overriding add/edit form forms for custom content type

I have created a new content type called protocol. The problem is that when you define a content type that means you also say how in the form the content is to be added and edited, like which form elements there will be.
A protocol is a content type that stores a title, an abstract and instructions. I want to add the title/instructions/abstract through one textarea where you tag the parts of the text like this:
[title]This is a title[/title] [abstract]This is an abstract. [/abstract][instructions]And these are my instructions.[/instructions]
That text is then processed and the content between each tag can be picked out and stored in a variable which should then be stored for the content type just like it had been added through a seperate field/textarea in a add/edit content form.
Is this possible to do? What kind of things should I read up on? Where in the drupal code are the function/functions that describes what happens when you push "Save" for a new content type for the standard add content form?(I just want to read it, not change anything)
Not sure this exactly matches what you're trying to do, but in a basic sense it should get you towards your goal. I wrote a module called endorse for Drupal 6 that provides a custom form feeding the submitted values into a new node:
http://drupal.org/project/endorse
Here's the form definition:
http://drupalcode.org/project/endorse.git/blob/refs/heads/master:/endorse.module#l136
Some basic validation follows and then the actual node save occurs at the top of the submit function, here up to line 231:
http://drupalcode.org/project/endorse.git/blob/refs/heads/master:/endorse.module#l206
The rest in that function is irrelevant except for the thank you and redirect at the very end of the submit function. If you're doing this in D7, it'll change a bit (see api.drupal.org for function definitions and whatnot), but it should look more or les the same.
Steps to solve your problem.
Create a module. Implement hook_menu with your custom add page.
Create a custom form using FORM API that it's gonna be displayed in your new page.
In your hook_form_submit get your values from the variable form state.
Parse the text and create and save a new node (snippet here).
$newNode = (object) NULL;
$newNode->type = 'protocol';
$newNode->title = $parsed_title;
$newNode->uid = 1;
$newNode->created = strtotime("now");
$newNode->changed = strtotime("now");
$newNode->status = 1;
$newNode->comment = 0;
$newNode->promote = 0;
$newNode->moderate = 0;
$newNode->sticky = 0;
// add CCK field data
$newNode->field_{YOUR_CUSTOM_FIELD_1}[0]['value'] = $parsed_data1;
$newNode->field_{YOUR_CUSTOM_FIELD_2}[0]['value'] = $parsed_data2;
// save node
node_save($newNode);
Those are the basic steps. If you have any more questions please ask.
TIP: Install the Devel module and use the function dpm() when you need to know the contents of some variable. You are probably gonna need it when you are implementing hook_form_validate or hook_form_submit for knowing the contents in the variable $form_state.
So just do:
dpm($form_state); //this will give you the variables inside the array with a krumo view.