Enable WYSIWYG for a custom textarea form field in drupal - php

i have created a custom form in my custom drupal6 module and the form has a textarea.
I am outputting the form as html embedded inside the php code, like the following:
function custom_my_form()
{
$f = '<form name="add_user" action="" method="post">
<label> Name </label>
<p><input type="text" name="name" value="" /></p>
<label>About Yourself</label>
<p><textarea name="desc"></textarea></p>
<input type="submit" name="submit" value="Add">
</form>';
return $f;
}
I have installed and enabled the WYSIWYG module.
My default input format is "Filtered HTML" and i have chosen the FCK editor for this input format.
Now i want to enable the WYSIWYG for the textarea field in this form.
How can i go ahead with this ?

Well, how ever you create forms is up to you, it just depends if it has ever come back to bite you in the butt...
Look at the comment module. I had noticed that it was possible to choose input format for comments, and when Full/filtered HTML was selected, the WYSIWYG editor kicked in. Here is the related code:
$form['comment_filter']['comment'] = array(
'#type' => 'textarea',
'#title' => t('Comment'),
'#rows' => 15,
'#default_value' => $default,
'#required' => TRUE,
);
if (!isset($edit['format'])) {
$edit['format'] = FILTER_FORMAT_DEFAULT;
}
$form['comment_filter']['format'] = filter_form($edit['format']);
So, you define an array with two elements, one of which is the textarea itself, and the other one the format chooser generated by filter_form, and that's all.
This was from http://groups.drupal.org/node/104604

For D7, its even simple. We got new type called text_format.
$form['comment'] = array('#type' => 'text_format',
'#format' => 'full_html',
'#title' => t('Description'),
'#required' => TRUE);

Yes, you can do this. Just add the following javascript to your page.
<script src="//tinymce.cachefly.net/4.0/tinymce.min.js"></script>
<script>
tinymce.init({selector:'textarea'});
</script>
This script automatically converts the text areas in your html code to rich text editors
More info at this link.

First, you should not create forms like that. Instead, use Drupal's built-in Forms API. Take a look at the Form API Quickstart Guide for more information.
This page on Drupal's site will help you add WYSIWYG to your custom forms: http://drupal.org/node/358316

Related

WordPress, Passing posted_data from Contact Form 7 via wpcf7_before_send_mail hook to FeedBurner

I am developing a WordPress plugin that uses the Contact Form 7 wpcf7_before_send_mail action hook to grab the email that is entered in CF7 and, if the user select a checkbox, pass it the the email to MailChimp and FeedBurner.
The MailChimp portion is working because I am able to use an API to pass the subscription. However, from my research, it seems the only way to subscribe to FeedBurner is by using their form, which worked fine for a simpler version of this plugin I developed several months, however it seem that whenever I echo out anything (such as a hidden form) through the wpcf7_before_send_mail hook, it kills CF7. Here are the relavant portions of my code:
add_action( 'wpcf7_before_send_mail', 'before_send_mail' );
function before_send_mail($wpcf7) {
if (is_array($wpcf7->posted_data["subscribe"])){
$subscribe_news = in_array ('Newsletter',$wpcf7->posted_data["subscribe"]);
$subscribe_blog = in_array ('Blog',$wpcf7->posted_data["subscribe"]);
}
$subscribe_email = $wpcf7->posted_data["your-email"];
$subscribe_email = $wpcf7->posted_data["your-email"];
if($subscribe_news){ ktmcf7_submit_mailchimp($subscribe_email); }
if($subscribe_blog){ ktmcf7_submit_feedburner($subscribe_email); }
}
function ktmcf7_submit_feedburner($subscribe_email){
$options = get_option( 'ktm_singlesub_options' );
?>
<script>
alert('feedburner');
window.open('http://feedburner.google.com/fb/a/mailverify?uri=<?php echo $options['feedburner_id'] ?>', 'popup5', 'scrollbars=yes,width=550,height=520');
</script>
<form name="form2" action="http://feedburner.google.com/fb/a/mailverify" method="post" target="popup5" >
<input type="hidden" name="email" value="<?php echo $subscribe_email ?>" />
<input type="hidden" value="<?php echo $options['feedburner_id'] ?>" name="uri"/>
<input type="hidden" name="loc" value="en_US"/>
<!-- input type="submit" value="Subscribe2" style="visibility:hidden;height:5px;" / -->
</form>
<script>
document.form2.submit();
</script>
Again, I'ved tried several different ways to output to the browser from this hook (echo, var_dump, etc, and each time the arrouw just spun infinitely. How can i work around this? Is there another way to submit a subscription to Feedburner other than using a form. There web site says the API is no longer available, but is there a secret back door?
Thanks.
In theory, you don't want to output a form, you want to submit the form variables using an HTTP post to FeedBurners end point using the wp_ function for issuing HTTP POSTs.
The following code implements a WordPress Plugin that will issue an HTTP POST from Contact Form 7's wpcf7_before_send_mail event handler:
function wpcf7_do_something (&$WPCF7_ContactForm) {
$url = 'http://your-end-point';
$email = $WPCF7_ContactForm->posted_data['email'];
$post_data = array(
'email' => urlencode($email),
'feedburner_id' => urlencode($feedburner_id));
$result = wp_remote_post( $url, array( 'body' => $post_data ) );
}
add_action("wpcf7_before_send_mail", "wpcf7_do_something");
The above code works as advertised, but keep in mind you might need to deal with CSRF tokens as well. https://en.wikipedia.org/wiki/Cross-site_request_forgery

Getting CakePHP HtmlHelper to generate a "date" input

I've been making some basic CRUD pages for my cakePHP app using the HtmlHelper for the views. This is handy for building forms but for date inputs the helper by default generates 3 select boxes for the date which is quite cumbersome to use.
HTML5 introduces the input[type=date] and most browsers now incorporate some nice native interfaces to deal with it; e.g. Chrome produces a nice date-picker for date inputs.
I know it is possible to make the HtmlHelper just make the input a text box instead of the 3 dropdown by doing the following:
echo $this->Form->input('my_date', array('type' => 'text'));
But when I do
echo $this->Form->input('my_date', array('type' => 'date'));
it ignores the 2nd arguement and goes back to the 3 selects.
Is there a way to get the helper to make a date input?
It seem the HtmlHelper has not yet evolved to make use of the "date" input.
If you tell the helper to generate the date input as a text field, adding a jQuery one-liner can convert it to a date input.
So:
echo $this->Form->input('my_date', array('type' => 'text'));
to generate the field. Then:
$('#idOfMyDate').attr('type', 'date');
To change it to a date input.
If anyone has a better way I'd be keen to hear it.
The CakePHP FormHelper uses Widgets to render different input types. For "datetime" types, it uses the DateTimeWidget per default.
To get a regular input with the attribute type="date", you just have to tell CakePHP which widget to use.
In the View (usually App\AppView.php), you can configure the FormHelper:
<?php
namespace App\View;
use Cake\View\View;
class AppView extends View
{
public function initialize() {
$this->loadHelper('Form', [
'widgets' => [
'datetime' => ['Basic'],
],
]);
}
}
?>
The BasicWidget is the most basic widget which is used to render regular text inputs.
Then, in your view, you can just use 'type' => 'date' as expected:
echo $this->Form->input('my_date', array('type' => 'date'));
Or, since CakePHP already sets the type to "date" since the database column is a datetime field you can just leave it like this:
echo $this->Form->input('my_date');
The result is a regular text input with type="date".
For future readers: In the most recent version of CakePHP, you would use the method Form::control instead of Form::input. Everything else still applies.
Try this:
echo $this->Form->text('my_date',array('type' => 'date');
Like this it'll work as a charm
<div class="form-group">
<label for="Description"><?php echo __('Date'); ?></label>
<div class='input-group date' id='datetimepicker1'>
<?php echo $this->Form->input('scheduled_date', array('label'=> false, 'div' => false, 'class'=>'form-control', 'type' => 'text')); ?>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
Some explanation:
'div' => false: it's necessary to desable the div rendered by input() FormHelper function
'label' => false: it's necessary to desable the label rendered by input() FormHelper function
Take a look at cake PHP form helper doc for more details
I solved this problem with jquery
PHP
<?= $this->Form->control('birth_date', ['value' => $birth_date->i18nFormat('yyyy-MM-dd'), 'type' => 'text']); ?>
JS
$('#birth-date').attr('type', 'date');
It's obvious that Cake's FormHelper is messing up with <input type "date">. Therefore I solved this problem the following way (in CakePHP 2.x)
Copy FormHelper.php from lib\Cake\View\Helper\
Paste it to app\View\Helper. Now Cake will use your Form Helper instead of its own.
Open the new FormHelper.php, go to protected function _getInput($args) {, search for case 'date': and change it from:
case 'date':
$options += array('value' => $selected);
return $this->dateTime($fieldName, $dateFormat, null, $options);
to:
case 'date':
return $this->{$type}($fieldName, $options);
Cake will now stop transforming <input type="date"> into select boxes.
Keep in mind that with every future release of Cake 2.x you will have to transfer possible changes in Cake's Form Helper into your own Form Helper manually.

zend framework text fields with same name but with different belongings

I have a problem with zend form, where i need to built a form where field name are same but with different belongings. Here is the input fields that i wanted inside my form.
Currently i am getting with straight html but because of this i am missing validation.
<input type="text" name="travel_guide_tab[4][title]">
<input type="text" name="travel_guide_tab[4][description]">
<input type="text" name="travel_guide_tab[6][title]">
<input type="text" name="travel_guide_tab[6][description]">
In Zend Form the element names must be unique (in some way) or else they will overwrite. However you can continue using your html form and simply filter and validate in the controller using Zend_Filter_Input. The filter and validation classes are the same ones used by Zend_Form you just pass the data in a different way.
Simple example, partial:
public function someAction() {
//set filters and validators for Zend_Filter_Input
$filters = array(
'nameOfInput' => array('HtmlEntities', 'StripTags')
);
$validators = array(
'nameOfInput' => array('NotEmpty', 'Int')
);
//assign Input
$input = new Zend_Filter_Input($filters, $validators);//can also pass the data in constructor (optional)
$input->setData($this->getRequest()->getParams());
//check input is valid and is specifically posted as 'Delete Selected'
if ($input->isValid()) {
//do some stuff
}
Good Luck.

cakephp input type number

I have a mobile website form that I want to add type attributes to the inputs so that there correct keyboard format will pop up.
However in cakephp setting the type as number a textarea is created instead of the input and the type is not set.
Setting type as text does work.
How do I overide this and have cakephp just keep it as a text input with type=number?
<?php echo $form->input('phone',array('type' => 'number')); ?>
Result:
<textarea id="UserCardExpires" rows="6" cols="30" name="data[User][card_expires]"class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset"></textarea>
This is ok:
<?php echo $form->input('postcode' ,array('type' => 'text')); ?>
Result
<input type="text" id="UserPostcode" name="data[User][postcode]" class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset">
On older versions of Cake, the Form helper won't automagically interpret $options['type'] as the HTML5 input-element type attribute. You have to force it by using "type" as an option on an explicit text element.
Use the following:
$form->text( 'phone', array( 'type' => 'number' ) );
I think phone numbers might be:
echo $form->text( 'phone', array( 'type' => 'tel' ) );
EDIT:
Sorry I'm an idiot, thats HTML5.

How to Zend_Dojo_Form_Element_FilteringSelect onchange submit

Well the title pretty much says it all. I had:
$strata = new Zend_Form_Element_Select('strata');
$strata->setLabel('Select a strata: ')->setMultiOptions($this->stratalist)->setAttrib('onChange', 'this.form.submit()');
Then I need to use some fancy dojo form elements in other forms. So I decided to make them all look the same and did this:
$strata = new Zend_Dojo_Form_Element_FilteringSelect('strata');
$strata->setLabel('Select a strata: ')->setMultiOptions($this->stratalist)->setAttrib('onChange', 'this.form.submit()');
It shows up and looks fine, but the form is not submitted when I change the FilteringSelect. If I look at the HTML that is rendered, sure enough:
<select name="strata" id="strata" onChange="this.form.submit()">
I suspect that Dojo elements cannot or do not work like this. So how do I make this form submit when I change the FilteringSelect?
Here it is:
When defining the form, give it an id:
$this->setName('StrataSelect');
or
$this->setAttrib('id', 'StrataSelect');
Then the onChange event uses getElementById:
$strata = new Zend_Dojo_Form_Element_FilteringSelect('strata');
$strata->setLabel('Select a strata: ')->setMultiOptions($this->stratalist)->setAttrib('onChange', "document.dojo.byId('StrataSelect').submit();");
or
$strata->setLabel('Select a strata: ')->setMultiOptions($this->stratalist)->setAttrib('onChange', "document.getElementById('StrataSelect').submit();");
Why this now works and none of the "old school" submit() calls probably has something to do with dojo handling the onchange event. So submit or this.form are not objects, methods, etc etc etc.
I don't want to put any javascript this form depends on into the view. I want this form to be "portable". So therefore I don't want to use dojo.connect
There are probably better ways to do this. So I'll leave this unanswered for now.
Do you have parseOnLoad enabled? If you're building the form in php you can do this:
$form = new Zend_Form_Dojo();
$form->addElement(
'FilteringSelect',
'myId',
array(
'label' => 'Prerequisite:',
'autocomplete' => true,
'jsId' => 'myJsId',
),
array(), //attributes
array( //your select values
'id1' => 'name1',
'id2' => 'name2',
'id3' => 'name3',
)
);
you might need to set a few attributes on your $form.
try this:
$form->setAttribs( array('jsId'=>'MyFormName') );
Then in your onClick:
MyFormName.submit()
If your form passes validation (presuming you have some), it should submit.

Categories