Osclass forums

Support forums => General help => 3.6.x => Topic started by: serjuc11111 on November 08, 2016, 04:39:10 pm

Title: Params::setParam- meta
Post by: serjuc11111 on November 08, 2016, 04:39:10 pm
I have a meta input like :
<input id="meta_new-custom-field" name="meta[42]" value="" type="text">
how to set params for this input:?
Params::setParam("meta[42]","0123456789");//not working
$_POST[meta[42]] = '0123456789';//not workig too
meta field: pk_i_id - 42 s_name -> Phone* ,
thx!
Title: Re: Params::setParam- meta
Post by: serjuc11111 on November 08, 2016, 07:51:01 pm
No help for this one?What im doing wrong....??
Title: [solved]Re: Params::setParam- meta
Post by: serjuc11111 on November 08, 2016, 08:39:41 pm
If you will need to solve this ,use
where $br=fk_i_field_id
 Params::setParam('meta',array($br=>'0123456789'));
Title: Re: [solved]Re: Params::setParam- meta
Post by: Sophia_OS on March 28, 2019, 10:57:55 pm
If you will need to solve this ,use
where $br=fk_i_field_id
 Params::setParam('meta',array($br=>'0123456789'));

how did you solve this? please share with us!
i want to get and set the value of a custom-field in item-post.php

$get_value = Params::getParam('meta[42]'); //Getting the value = Not working!
Params::setParam('meta[42]', '999'); //Setting the value = Not working!
Title: Re: [solved]Re: Params::setParam- meta
Post by: WEBmods on March 28, 2019, 11:09:46 pm
If you will need to solve this ,use
where $br=fk_i_field_id
 Params::setParam('meta',array($br=>'0123456789'));

how did you solve this? please share with us!
i want to get and set the value of a custom-field in item-post.php

$get_value = Params::getParam('meta[42]'); //Getting the value = Not working!
Params::setParam('meta[42]', '999'); //Setting the value = Not working!

meta is an ARRAY. Which means getParam('meta[field_id]') won't work, you need to get meta variable and access it's key - getParam('meta')['field_id']. Probably same for setParam, never used that function so...

Regards.
Title: Re: Params::setParam- meta
Post by: Sophia_OS on March 29, 2019, 06:49:36 pm
If you will need to solve this ,use
where $br=fk_i_field_id
 Params::setParam('meta',array($br=>'0123456789'));

how did you solve this? please share with us!
i want to get and set the value of a custom-field in item-post.php

$get_value = Params::getParam('meta[42]'); //Getting the value = Not working!
Params::setParam('meta[42]', '999'); //Setting the value = Not working!

meta is an ARRAY. Which means getParam('meta[field_id]') won't work, you need to get meta variable and access it's key - getParam('meta')['field_id']. Probably same for setParam, never used that function so...

Regards.

what is the format for setting?

Params::setParam('meta[42]', '$cf_value'); //Not working!
Params::setParam(('meta')[42], '$cf_value'); //Parse error: syntax error, unexpected '[' in ...
Title: Re: Params::setParam- meta
Post by: WEBmods on March 29, 2019, 07:14:22 pm
Try this:

Code: [Select]
$old_meta = Params::getParam('meta');
$new_meta = $old_meta;
$new_meta[42] = 'Value for field 42';
Params::setParam('meta', $new_meta);

Regards.
Title: Re: Params::setParam- meta
Post by: Sophia_OS on March 30, 2019, 09:48:56 pm
Try this:

Code: [Select]
$old_meta = Params::getParam('meta');
$new_meta = $old_meta;
$new_meta[42] = 'Value for field 42';
Params::setParam('meta', $new_meta);

Regards.

i put this code in header, but when i load item-post.php, custom-field-42 didn't get "Value for field 42" value on page-load! and it was empty!
Title: Re: Params::setParam- meta
Post by: WEBmods on March 30, 2019, 10:08:33 pm
Where exactly do you want to get and modify this meta field?

Regards.
Title: Re: Params::setParam- meta
Post by: Sophia_OS on March 31, 2019, 11:34:18 pm
Where exactly do you want to get and modify this meta field?

Regards.

in item-post.php i want to get and replace! (posted_item hook)
not with sql, i just want to get the value of CF and replace it!
Title: Re: Params::setParam- meta
Post by: WEBmods on April 01, 2019, 09:00:06 am
Try doing that in pre_item_post. Param is already saved when posted_item runs.

Regards.
Title: Re: Params::setParam- meta
Post by: Sophia_OS on April 01, 2019, 09:01:52 pm
Try doing that in pre_item_post. Param is already saved when posted_item runs.
Regards.

Not working!
I even put that code directly in header.php and didn't work!
Title: Re: Params::setParam- meta
Post by: Sophia_OS on April 19, 2019, 09:47:53 am
@patrickFromCroatia
Did you find a way to do this?
Title: Re: Params::setParam- meta
Post by: WEBmods on April 19, 2019, 01:03:01 pm
So you want to change the value of the meta param before it's saved in the database? Or can you update it in the database later?

Regards.
Title: Re: Params::setParam- meta
Post by: Sophia_OS on April 19, 2019, 10:27:51 pm
So you want to change the value of the meta param before it's saved in the database? Or can you update it in the database later?

Regards.

i know how to update the database to change it with posted_item hook! i don't wanna use database to do this!
i want to replace the value of the meta param before it's saved in the database!
Title: Re: Params::setParam- meta
Post by: WEBmods on April 19, 2019, 11:07:57 pm
So you can't use posted_item as it runs when all data is already saved in the database. I will check if we can modify meta value saved in session with pre_item_post.

Regards.
Title: Re: Params::setParam- meta
Post by: Sophia_OS on April 20, 2019, 06:57:11 am
Thank you!
Title: Re: Params::setParam- meta
Post by: WEBmods on April 22, 2019, 12:56:15 pm
According to my quick research, this should work...

Code: [Select]
<?php
function wm_item_pre_post_session_edit() {
    
$key 10// Your meta field ID.
    
$meta Params::getParam('meta'); // Get all meta fields.
    
if(@$meta[$key] == null) return; // Stop the function if your specific meta field doesn't exist. @ is added to stop warnings if $meta is not an array.
    
$old_value $meta[$key]; // Get value from your meta field.
    
$new_value 'New value'// Put new value for your meta field.

    
Session::newInstance()->_setForm('meta_'.$key$new_value); // Save new value.
    
Session::newInstance()->_keepForm('meta_'.$key);
}
osc_add_hook('pre_item_post''wm_item_pre_post_session_edit');

Regards.
Title: Re: Params::setParam- meta
Post by: Sophia_OS on April 23, 2019, 10:43:10 am
According to my quick research, this should work...

Code: [Select]
<?php
function wm_item_pre_post_session_edit() {
    
$key 10// Your meta field ID.
    
$meta Params::getParam('meta'); // Get all meta fields.
    
if(@$meta[$key] == null) return; // Stop the function if your specific meta field doesn't exist. @ is added to stop warnings if $meta is not an array.
    
$old_value $meta[$key]; // Get value from your meta field.
    
$new_value 'New value'// Put new value for your meta field.

    
Session::newInstance()->_setForm('meta_'.$key$new_value); // Save new value.
    
Session::newInstance()->_keepForm('meta_'.$key);
}
osc_add_hook('pre_item_post''wm_item_pre_post_session_edit');

Regards.

thank you very much for the code but i exactly paste this code in my functions.php and changed the 10 to my custom-filed id (oc_t_meta_fields -> pk_i_id), but after publishing the item, its value did NOT change to 'New value'
Title: Re: Params::setParam- meta
Post by: WEBmods on April 23, 2019, 03:58:08 pm
I did more research and I think there is no other way but to update the database or to change core files. I used both Session and Param classes to try to modify meta, but I noticed that meta fields are already saved in a variable when pre_item_post runs.

ItemActions.php:149

Code: [Select]
            $meta = Params::getParam("meta"); // META IS SET HERE.
            foreach($_meta as $_m) {
                $meta[$_m['pk_i_id']] = (isset($meta[$_m['pk_i_id']]))?$meta[$_m['pk_i_id']]:'';
            }
            if($meta!='' && count($meta)>0) {
                $mField = Field::newInstance();
                foreach($meta as $k => $v) {
                    if($v=='') {
                        $field = $mField->findByPrimaryKey($k);
                        if($field['b_required']==1) {
                            $flash_error .= sprintf(_m("%s field is required."), $field['s_name']) . PHP_EOL;
                        }
                    }
                }
            }
            // hook pre add or edit
            // DEPRECATED: pre_item_post will be removed in 3.4
            osc_run_hook('pre_item_post'); // CHANGING PARAM OR SESSION HERE DOES NOT AFFECT ANYTHING.

Regards.
Title: Re: Params::setParam- meta
Post by: Sophia_OS on April 24, 2019, 01:22:33 am
I did more research and I think there is no other way but to update the database or to change core files. I used both Session and Param classes to try to modify meta, but I noticed that meta fields are already saved in a variable when pre_item_post runs.

ItemActions.php:149

Code: [Select]
            $meta = Params::getParam("meta"); // META IS SET HERE.
            foreach($_meta as $_m) {
                $meta[$_m['pk_i_id']] = (isset($meta[$_m['pk_i_id']]))?$meta[$_m['pk_i_id']]:'';
            }
            if($meta!='' && count($meta)>0) {
                $mField = Field::newInstance();
                foreach($meta as $k => $v) {
                    if($v=='') {
                        $field = $mField->findByPrimaryKey($k);
                        if($field['b_required']==1) {
                            $flash_error .= sprintf(_m("%s field is required."), $field['s_name']) . PHP_EOL;
                        }
                    }
                }
            }
            // hook pre add or edit
            // DEPRECATED: pre_item_post will be removed in 3.4
            osc_run_hook('pre_item_post'); // CHANGING PARAM OR SESSION HERE DOES NOT AFFECT ANYTHING.

Regards.

@patrickFromCroatia Thank you so much!