Drupal 7, insert user_profile_form into a page - drupal-7

I tried to insert user_profile_form into a page. the code is rather simple:
global $user;
module_load_include('inc', 'user', 'user.pages');
print drupal_render(drupal_get_form('user_profile_form', $user, 'account'));
everything works fine except when uploading images, it shows:
"Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'user_profile_form' was given in drupal_retrieve_form()"
Any ideas, thx a lot

you need to use form_load_include() instead of module_load_include to load/build a form, and you have to pass arguments inside $form_state.
the code you are looking for looks like:
$form_id = 'user_profile_form';
$form_state = array();
//passing arguments to form.
$form_state['build_info']['args'] = array($account, 'account');
form_load_include($form_state, 'inc', 'user', 'user.pages');
$form = drupal_build_form($form_id, $form_state);

To prevent the notice or warning:
Trying to get property of non-object in user_account_form() (line 1019 of /var/www/modules/user/user.module)
Change the last two lines to:
<?php
$output_form = drupal_get_form($form_id);
print render($output_form);
?>

Related

Use results from one array in another array

I have a logged in user following other users in a blog. I would like my logged in users to see posts from users they follow.
I run a query that returns a list of user_ids (user_id1) a logged in user (user_id2) follows in an array. I then run the array through a foreach loop to get the list of users as user ids and place it in a new array. The returned user1_ids are echoed as a string. I would now like to use the returned user1_ids in another array that only displays posts based on the user1_ids.
The issue is that the values are returned as a string and my second array used in the posts only reads the first number of the first array as the values are returned as a string and not integers.
How can I use the results from my first array in my second array 'author__in'=> array()? Do I need to convert the string into integers or is there a better method?
// The database query that returns the array
<?php
$currentloggedinuser = get_current_user_id();
$followers_query = $wpdb->get_results("SELECT ID, user_id1 FROM wp_followers WHERE user_id2 = '$currentloggedinuser' ") ?>
// the returned array from the query through foreach placed in another array
<?php
$following_id = array();
foreach ($followers_query as $follower) {
$following_id[] = $follower->user_id1;
sort($following_id);
$following_ids = implode(", ",$following_id);
}
?>
// the resulting number values returned as a string
<?php echo $following_ids; ?>
<?php
$args = array(
'author__in'=> array($following_ids), // user1_ids I'd like to include from the array above
'post_type' => 'post'
);
This would probably work. I did not test it but it should be fine.
Here is some info on the $wpdb::get_col method
<?php
$currentloggedinuser = get_current_user_id();
// Get an array containing only the user_id1 values.
// It's a good habit to use the prepare method of $wpdb
// for security and ease of reading the code
// Also, you should use $wpdb->prefix instead of using the 'wp_' for the table name
// that prefix can be changed and your code would break.
$followers = $wpdb->get_col(
$wpdb->prepare(
"SELECT user_id1 FROM {$wpdb->prefix}followers WHERE user_id2 = %d",
$currentloggedinuser
)
);
sort($followers); // or you can just apply sorting in the query above
// Directly echo the imploded array.
// No need to store it in a variable to do so unless you want to use it somewhere else
echo implode(', ', $followers);
$args = array(
'author__in'=> $followers, //use the array we got from the query
'post_type' => 'post'
);

Prestashop returns last value of imploded array from DB

I started fiddling with Prestashop 1.7 modules and I ran into this weird behavior.
I have this code to save values from form post to database (working ok)
protected function postProcess()
{
$form_values = $this->getConfigFormValues();
foreach (array_keys($form_values) as $key) {
Configuration::updateValue($key, Tools::getValue($key));
if($key == 'MSLT_MEGAMENU_CATEGORIES'){
$categories = implode(",",Tools::getValue($key));
Configuration::updateValue('MSLT_MEGAMENU_CATEGORIES', $categories);
}else{
$this->errors[]=$this->l('Please select categories to display');
}
}
}
And I use this code to fetch those values from database (works ok)
protected function getConfigFormValues()
{
$categories = explode(',',Configuration::get('MSLT_MEGAMENU_CATEGORIES', true));
return array(
'MSLT_MEGAMENU_LIVE_MODE' => Configuration::get('MSLT_MEGAMENU_LIVE_MODE', true),
'MSLT_MEGAMENU_CATEGORIES' => $categories,
'MSLT_MEGAMENU_ACCOUNT_EMAIL' => Configuration::get('MSLT_MEGAMENU_ACCOUNT_EMAIL', 'contact#prestashop.com'),
'MSLT_MEGAMENU_ACCOUNT_PASSWORD' => Configuration::get('MSLT_MEGAMENU_ACCOUNT_PASSWORD', null),
);
}
and helper to populate form with values
$helper->tpl_vars = array(
'fields_value' => $this->getConfigFormValues(), /* Add values for your inputs */
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
);
return $helper->generateForm(array($this->getConfigForm()));
This is the var_dump() when trying to load values from database, for this case my DB value is (1,3,9)
array(1) { [0]=> string(1) "9" }
As you can see Configuration::get() only gets the last string value.
Interesting behavior is that when I update the data and stay in the same page, then everything is ok and data is fetched properly, but when I leave module configuration page and comeback, the issue happens. Maybe I am missing some little snippet of code? I am still a newbie. If needed I can provide more code.
I don't know what your specific goal is but in any case it is always convenient to use Prestashop's own functions.
To save and select the configuration variables
eg, save values in json format
Configuration::updateValue('MYVALUES', Tools::jsonEncode(Tools::getValue('MYVALUES')), true );
eg, get values
Tools::jsonDecode( Configuration::get( 'MYVALUES' );
Dirty workaround I found to get imploded values from DB correctly. Maybe someone will use it.
$sql = 'SELECT * FROM '._DB_PREFIX_.'configuration WHERE name = "MSLT_MEGAMENU_SELECTED_CAT"';
$value = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
$selected = explode(',',$value['value']);

Drupal 7 - Why wont my AJAX code work

I know there are lots of tutorials on loads of sites (I have read most of them in the last few hours) but I still can not get the following code to work. Can anyone see what I am doing wrong?
The second select box does not update when I change a value in the first one and I don't get the blue spinning ajax circle next to the first select box when I make a change.
$form['age_range'] = array(
'#type'=>'select',
'#title'=>'Age Range',
'#options'=>$age_ranges,
'#ajax'=>array(
'event'=>'change',
'callback'=>'childcare_observations_ajax_set_eyfs_category_list',
'wrapper'=>'eyfs_category_wrapper',
'method'=>'replace',
)
);
$form['eyfs_category_wrapper'] = array('#prefix'=>'<div class="eyfs_category_wrapper">','#suffix'=>'</div>');
$form['eyfs_category_wrapper']['eyfs_category'] = array(
'#type'=>'select',
'#title'=>'EYFS Category',
'#options'=>array(1=>'one',2=>'Two'),
);
return $form;
}
function childcare_observations_ajax_set_eyfs_category_list($form,&$form_state){
$eyfs_category_options = array();
$eyfs_category_options[0] = "We just changed the list values";
$eyfs_category_options[1] = "We also added a second option";
$form['eyfs_category_wrapper']['eyfs_category'] = array(
'#type'=>'select',
'#title'=>'EYFS Category',
'#options'=>$eyfs_category_options,
);
return $form['eyfs_category_wrapper'];
}
The wrapper should be the id of the element to replace. you have it as a class for eyfs_category_wrapper
plus I'm not sure if 'eyfs_category_wrapper' is a valid element, normally I use a field set, or just add a prefix and suffix to my target field.

How to insert "Checkboxes" values to SQL

well i'm confused on how to insert "Checkboxes" values to SQL
here's my code
$form['last'] = array(
'#type' => 'checkboxes',
'#title' => "Just title",
'#options' => array(
'opt1' => "Option 1",
'opt2' => "Option 2",
),
as you can see my form consist of two checkbox, so how to get the value and insert to sql language. anyone can give an example or hint
here the method that i'm used to get the value (i know its very wrong)
function fasil_form_submit($form,&$form_state){
global $user;
$entry = array(
'uid' => $user->uid,
'test1' => $form_state['values']['1first'],
$jenis = 'aa_test';
$return = insert_form($entry,$jenis);
}
ps : sorry for my bad english
I'm not 100% sure what you're trying to do but I think you're trying to insert a value in the database for each of the checkboxes ticked? If so this is the quickest way:
function fasil_form_submit($form,&$form_state){
// Filter out un-checked items
$checked = array_filter($form_state['values']['last']);
global $user;
foreach ($checked as $value) {
$entry = array(
'uid' => $user->uid,
'test1' => $value
);
$jenis = 'aa_test';
insert_form($entry, $jenis);
}
}
As already mentioned in another answer the simplest way to see what you need to get from the form is to output $form_state['values'] in your submission function to see what was passed from the form.
However, rather than use the unsightly print_r and potentially messing up the form submission by calling exit prematurely (in Drupal 7 drupal_exit() should always be used instead of exit anyway), I strongly recommend you download and install the Devel module and use it's dpm() function to print the variable to the screen.
Any variable passed to dpm() is outputted to the standard Drupal messages area, and becomes an easy to navigate on-screen hierarchy of that variable like this:
You can use it absolutely anywhere in code within Drupal, e.g.
function fasil_form_submit($form,&$form_state){
// Output the form submission array to the messages area:
dpm($form_state['values']);
}
The Devel module is very good, and absolutely essential for any serious Drupal development.
Hope that helps.
Here's a quick way I like to look at my form output
function fasil_form_submit($form,&$form_state){
header('content-type: Text/plain');
print_r($form_state['values']);
exit;
global $user;
$entry = array(
'uid' => $user->uid,
'test1' => $form_state['values']['1first'],
$jenis = 'aa_test';
$return = insert_form($entry,$jenis);
}
Now those 3 lines should make it clear how Drupal is submitting the data.
I believe you will need to loop through $form_state['values']['last'] and test that the key is not set to 0.
function fasil_form_submit($form,&$form_state){
global $user;
foreach ($form_state['values']['last'] as $key => $value) {
if (value != 0) {
// $form_state['values']['last'][$key] was checked
}
else {
// $form_state['values']['last'][$key] not checked
}
}
}

what should be input into the brackets of $this->paginate()

I am using cakephp 1.26.
I am doing some self-learning about Pagination in CakePHP.
I have tested the following code in my localhost, and it works fine.
I have altered a little change to the second line of code, and found that
nothing change to the result.
1st version:
$this->paginate=array('conditions'=>array('Testing.zero'=>'0'), 'limit' => 3);
$w = $this->paginate();
$this->set('postVariable', $w);
2nd version:
$this->paginate=array('conditions'=>array('Testing.zero'=>'0'), 'limit' => 3);
$w = $this->paginate('Testing');
$this->set('postVariable', $w);
3rd version:
$this->paginate=array('conditions'=>array('Testing.zero'=>'0'), 'limit' => 3);
$w = $this->paginate('helloworld');
$this->set('postVariable', $w);
4th version:
$this->paginate=array('conditions'=>array('Testing.zero'=>'0'), 'limit' => 3);
$w = $this->paginate($this->helloworld);
$this->set('postVariable', $w);
I have no idea what I should input into the brackets of $this->paginate()
The documentation says it all: http://api.cakephp.org/class/controller#method-Controllerpaginate
First parameter is the model name, second parameter is a scope, that is, an additional conditions array. Third parameter is currently useless.
The paginate function can be found in /cake/libs/controller/controller.php,line 934.It's a bit long but not that complex.And I think you can read it and find the reason yourself.Personally I prefer current model name as the parameter.In your code ,that would be
$w = $this->paginate("Testing");

Resources