Drupal 7 db_insert on WYSIWYG textarea field - drupal

this is my first post here at stackoverflow and i'll try make sharp and short ;-)
I am running into problems when inserting a textarea form field using CKeditor into the database.
Here is my form element:
$form['add']['description'] = array(
'#wysiwyg' => true,
'#name' => 'description',
'#title' => t('description'),
'#type' => 'text_format',
'#base_type' => 'textarea',
);
When submitting the form i do get a SQL error like this because Drupal appends the placeholder elements 'value' and 'format'
db_insert failed. Message = SQLSTATE[21S01]:
Insert value list does not match column list:
1136 Column count doesn't match value count at row 1,
query= INSERT INTO {tablename} (description)
VALUES (:db_insert_placeholder_13_value, :db_insert_placeholder_13_format)
Unfortunately i exactly have to set up the textarea this way to make the CKeditor work. Can you advice me how to get rid of the
:db_insert_placeholder_13_value,
:db_insert_placeholder_13_format
to a single variable again?
I really appriciate your help
PROBLEM SOLVED --- silly me, should have used my glasses more :-D
Just use the correct array index inside the submit hook for saving the data to the database and it will work:
function MYMODULE_form_add_submit($form, &$form_state) {
...
'description' => $form_state['values']['description']['value'],
...
}

If you're just interested in the text itself (not the format), add a validation handler for the form and use something like this
function MYMODULE_form_name_validate($form, &$form_state) {
$form_state['values']['description'] = $form_state['values']['description']['value'];
}

Related

show month number instead of month name on symfony2 formbuilder

It might be ridiculous quesion, but I can't find the way with googling.
I am making form like this.
$form = $this->createFormBuilder($searchTime)
->add('date','date',array(
"property_path" => false,
'years' => $years,
'data' => new \Datetime()
but it shows month select box as name such as jun,july,aug
however I want to show number here 6,7,8 ....
How can I make it ?
Maybe it's worth a try to specify a format, as the reference says:
$builder->add('date', 'date', array(
'widget' => 'choice',
'format' => 'yyyy-MM-dd',
// ...
));
The reference says, that if your widget is choice and you specify the format then the selects will be rendered in the order of the format. Maybe the select format is also will be adjusted to the format pattern.
(The month without leading zeros would be: yyyy-M-dd)

Getting a value from select - Drupal

I have a question. How to get a value from a $form['item'] so that i can use the value to filter data in form['item_2'].
Example:
For option where table looks like :
item_id item_name
1 pencile
2 book
So when i chose 2 book it will filter out data in the other form where i have table looking like :
newitem_id item_id newitem_name
1 1 hard
2 1 soft
3 2 novel
The example code :
function my_module_form($form,&$form_submit){
$form=array();
$select=db_query("SELECT * FROM {table_1}");
$options_one=array();
foreach($select as $data){
$options_one[$data ->item_id] = $data -> item_name;
}
$form['item'] = array(
'#title'=>t('Items'),
'#type' => 'select',
'#options' => $options_one,
'#required' => TRUE,
);
$temp=1;
$select = db_query("SELECT * FROM {table_2} WHERE newitem_id=:item_id",array(':item_id'=>$temp));
$options_two=array();
foreach($select as $data){
$options_two[$data ->newitem_id] = $data -> newitem_name;
}
$form['item_2'] = array(
'#title'=>t('Items'),
'#type' => 'select',
'#options' => $options_two,
'#required' => TRUE,
);
Now in the code for $temp i use number one and it gives me hard and soft values in dropdown. But how can i add the selected value from the $form['item'] to the $temp.
You could either use simple jQuery in your case or you could use drupal FORM API #ajax property.An array of elements whose values can control the behavior of the element with respect to the Drupal AJAX framework.AJAX (Asynchronous Javascript and XML) is a term used for dynamic communication between the browser and the server, without page reloads.You could find more details on this property here.If you find it difficult you could try the examples for developers which drupal provides shows exactly how the values in a dropdown is dependent on another drop down.
Drupal Developer examples
Using Simple Jquery(if your values just static values.If you need to take values from db,should be using jQuery+ajax)
Jsfiddle

Drupal - Search API - Views - Solr Index: How to implement a Date Filter?

Looking to incorporate a date filter within a Solr Index View. Presently the only option is for the user to manually enter a text date to filter by date. It would be nice to expose a date filter and search by M/d/Y using the 'Between' function which is available in the 'Content' View. Creating a 'Solr Index' View seems to remove that functionality.
I noticed that the Between operator is missing from handler_filter.inc within the Search API module:
public function operator_options() {
return array(
'<' => t('Is smaller than'),
'<=' => t('Is smaller than or equal to'),
'=' => t('Is equal to'),
'<>' => t('Is not equal to'),
'>=' => t('Is greater than or equal to'),
'>' => t('Is greater than'),
);
}
What other code would we need to modify in order to get a more user-friendly date filter available for the user to query?
You can use something like that:
$or = $query->createFilter('AND');
$or->condition('created', $start_date, '>=');
$or->condition('created', $end_date, '<=');
$query->filter($or);
in hook_search_api_query_alter.

how to populate text area with database value before form load in drupal 7?

I have used hook_form_FORM_ID_alter( ) function to alter a menu_edit_menu form to add few fields. I ve stored the values from those fields in a table when submit action is performed. what I need now is when the particular form entry is loaded again for editing. The fields should be populated with the data in the database before form loads. Can anyone tell me how this could be done.
I have tried using hook_validate ( ) function but the function is called when the submit action is performed which is of no use in this regard. I have to perform the database query before the form gets rendered to populate the text fields with data from tables. Kindly provide me some insight into how this could be accomplished.
I have a problem with the sql select query as well/
$query = db_select('menu_custom');
$query
->fields(array('menu_name','role1','role2','role3'))
->condition('check',1,'=')
->conditon('title',$form_state['values']['title'])
->execute();
foreach($query as $record)
{
$mname = $result ->menu_name;
$rl1 = $result -> role1;
$rl2 = $result -> role2;
$rl3 = $result -> role3;
dpm($mname." ".$rl1);
}
I am getting error that my field specification is wrong but I cant figure out the problem there.
This is a bit too long for a comment so I'll put it here:
The only error you've got in your query is that the first argument passed to the fields() function needs to be the name/alias of the table from which the fields are coming. So your query should probably look something like this:
$query = db_select('menu_custom')
->fields('menu_custom', array('menu_name','role1','role2','role3'))
->condition('check',1,'=')
->conditon('title',$form_state['values']['title'])
->execute();
You get the data from your database in your form function, and put them as default_value
$name = db_query(YOUR_SQL);
$form['first_name'] = array(
'#title' => t('First Name'),
'#type' => 'textfield',
'#default_value' => $name,
);
is this what you meant?

How do I persist form data across an "access denied" page in Drupal?

We're building a small sub-site that, on the front page, has a one input box form that users can submit. From there, they're taken to a page with a few more associated form fields (add more details, tag it, etc.) with the first main form field already filled in. This works splendidly, thus far.
The problem comes for users that are not logged in. When they submit that first form, they're taken to a (LoginToboggan based) login page that allows them to login. After they login, they redirect to the second form page, but the first main form field isn't filled in -- in other words, the form data didn't persist.
How can we store that data and have it persist across the access denied page?
You could save the data in a cookie that would be passed to the page after the login page.
Assuming that you are using the Form API to create the form, and that you have a field called "fieldname" in the function to generate the form:
function my_form() {
$form['fieldname'] = array (
'#type' => 'textfield',
'#title' => 'Some fieldname'
);
// Second field if you need to save another
$form['fieldname2'] = array (
'#type' => 'textfield',
'#title' => 'Some other fieldname'
);
}
Set the cookie(s) in the submit handler for your form:
function my_form_submit($form, &$form_state) {
// Set the cookie with their data before they are redirected to login
// Use the array syntax if you have one than one related cookie to save,
// otherwise just use setcookie("fieldname",...
setcookie("saved_data[fieldname]", $form_state['values']['fieldname']);
// Your other code
}
After they login and are redirected to page two of your form, you can read the value of the cookie(s) and if they exist, insert them into the default values of the fields:
function my_form() {
if (isset($_COOKIE['saved_data[fieldname]'])) {
$default_fieldname = $_COOKIE['saved_data[fieldname]'];
}
else {
$default_fieldname = '';
}
// Same check for the second field
if (isset($_COOKIE['saved_data[fieldname2]']))...
$form['fieldname'] = array (
'#type' => 'textfield',
'#title' => 'Some fieldname',
'#default_value' => $default_fieldname
);
// Second field if used
$form['fieldname2'] = array (
'#type' => 'textfield',
'#title' => 'Some other fieldname',
'#default_value' => $default_fieldname2
);
}
The cookies(s) will be presented on each page load after they are set, so it doesn't matter if there are several failed login attempts or other page views in between. You should probably delete the cookies after they're used or set a short expiration time so that they are cleaned up automatically.
I don't know if there is a good way to transfer form data from two entirely different forms in Drupal, especially in the context of access denied. The best choices I can find would be to:
Use the url to send the data for the field.
You could also save the data in the db, but it might be a bit tricky to extract the data again.

Resources