SivlerStripe field to render Javascript tags/ code - silverstripe

I am developing a SilverStripe project. In my project, I am trying to create a custom content block where admin can enter the Javascript code to be rendered on the front-end.
I have two field
private static $db = [
'Script' => 'Text',
'Content' => 'HTMLText',
];
(Note the Script field).
In the template, I render the variables like this.
$Script
$Content
Then in the Script textarea field, I entered the following content
<script>alert("I am the Script")</script>
When I go to the front-end page after publishing, I see this instead.
How can I create a field where I can enter Javascript code in the SilverStripe?

I’m pretty sure that you can use the raw modifier
$Script.RAW

Related

add a textarea field in OrangeHRM

I need to add a text area field in Job screen.
I tried with a custom field but I can only add a simple textbox. How can I add a textarea?
I have never worked with symfony so I need a very simple and detailed solution, please.
In orangeHRM or Symfony1.4, if you want to add new field in any form you have to define new field in PHP array which will be rendered in Screen. In your case to add field in viewJobDetails page.
Add field, validator in PHP array as follows.
1. Go to symfony/plugins/orangehrmPimPlugin/lib/form/EmployeeJobDetailsForm.php
2. add following code in $this->setWidgets(array(add given code here));
'new_field' => new sfWidgetFormTextarea()
add following code in $this->setValidators(array(add given code here));
'new_field' => new sfValidatorString(array('required' => false))
Hope this will help.

How to send data to base template without controller?

I need to get the data from entity in the base template (twig), but not rendering this data from controller.
Specifically, I want to realize the menu. Menu labels stored in the database (Page entity). I have many controllers and I don't want to repeat the code of the entity handling in each controller.
I could extend the controller's classes, but I want to avoid stuff like this:
return $this->render('... .html.twig',
array(
...
'menu' => $labels,
...
)
);
in each of the controllers.
This is the perfect use-case for an embedded controller. You can call a new controller from your template for rendering a part of your response, in this case the menu. It can have any logic a controller can, meaning you can query your database, build your menu structure and render a twig file for outputting it as html.

FOSUserBundle: Change Password within actual project

I have successfully installed FOSUserBundle in my project and everything works as expected. However, I am struggling with how to implement it in my actual project.
I want to create the following setup:
A page displaying some user settings in one form (like newsletter subscription), the possibility to change the password in a second form and maybe also a third form to change the username.
The settings form as well as some more information is coming from an existing action in my controller and is working well.
I did try a few things but things are not really working out yet:
I copied some functionality from FOSUserBundle\Controller\ChangePasswordController\changePasswordAction() to my own action. This way I could get the change password form, create the view and pass it to my template.
I added the form to my template with {{ form_widget(form) }}. The form is being displayed and it's even working. I can change the password. However, the labels are being lost, simply reading Current, First, and Second. Also there is no error messaging showing up when the two new passwords don't match or are being left empty.
Over all I have the feeling I am probably doing this in a wrong way. Could you please help me how I should handle this task and point out where I am likely doing something stupid?
Here is the code of my action, reduced to what's important here:
# src/Acme/MyBundle/Controller/BackendController.php
public function accountAction(){
//pretty much a copy of FOSUserBundle\Controller\ChangePasswordController\changePasswordAction()
$user = $this->get('security.context')->getToken()->getUser();
$form = $this->container->get('fos_user.change_password.form');
$formHandler = $this->container->get('fos_user.change_password.form.handler');
$process = $formHandler->process($user);
if ($process) {
//password has been changed, response will be generated
}
//more stuff going on here
$moreStuff = ...
//render view
return $this->render('AcmeMyBundle:Backend:account.html.twig', array(
'form' => $form->createView(),
'moreStuff' => $moreStuff
));
}
IMO rendering more than one form in one action is not a good idea.
Always try to separate things and let an action handle only one feature.
In your twig template I suggest to use the render method :
{% render 'AcmeBundle:SomeAction' with{'param:param} %}
It will generate a GET request on the action provided with some params if needed.
Create one action that will render the twig template with subrequests :
// AcmeUserBundle:editAction
{% render 'AcmeUserBundle:changePasswordAction' %}
{% render 'AcmeUserBundle:settingsAction' %}
{% render 'AcmeUserBundle:profileAction' %}
And then you'll need to create one action per form.
For password and username modification you can also override FOSUserBundle views if your needs are only visual. If you need to add/remove a field on the form you will need to create a new service.
I sugget reading FOSUserBundle documentation about overriding :
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md#next-steps

Changing facebook tab icon using php sdk

I have created a facebook application for addding page tab in any facebook page.
Everything is working fine but i am not able to change the icon the tab i am adding.
I am using following code of php sdk
if($facebook->api($tabid, 'POST', array(
'custom_name' => $tabtitle,
'image_url'=>$newname,
'is_non_connection_landing_tab'=>1,
'access_token'=>$mpagetoken
))
Please tell me where i am wrong in this ?
There is no such field as image_url in page tabs.
Actually tab icon is the application icon which is icon_url field of application object, and while some of fields may be edited by issuing POST request to https://graph.facebook.com/APP_ID?PROPERTY_1=PROPERTY_VALUE this field isn't marked as editable:
Not all app properties can be edited via the API, please refer to the properties in the table above for an indication of whether a property can be edited or not.

Silverstripe How to use Calendar on DateFields with custom form template?

I have created a custom form class and template for my form by following the instructions here.
However I am having trouble with adding DateFields with calendars. Usually I just do something like this:
$dateField = new DateField ('DateRequired', 'Date Required');
$dateField->setConfig ('showcalendar', true);
I have tried the above code in my custom form however the page doesn't include any of the jquery ui scripts or css files for the calendar field.
So my question is how can I get the my custom form to include all the scripts and render the fields with the jquery ui calendars?
actually the change of the template should not be necessary.
$dateField = new DateField('...');
$dateField->setConfig('showcalendar', true);
$dateField->setConfig('showdropdown', true);
$dateField->setConfig('dateformat', 'dd.MM.YYYY');
Ok I have worked it out. The code within the template that calls the fields needs to be changed from:
$dataFieldByName(DateRequired)
to:
$dataFieldByName(DateRequired).FieldHolder
Now all of the javascript is included within the page.

Resources