Advertisement:

Author Topic: Params::setParam- meta  (Read 1880 times)

WEBmods

  • Hero Member
  • *****
  • Posts: 936
  • github.com/webmods-croatia/love-osclass/ | patrick
Re: Params::setParam- meta
« Reply #15 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.

Sophia_OS

  • Sr. Member
  • ****
  • Posts: 416
Re: Params::setParam- meta
« Reply #16 on: April 20, 2019, 06:57:11 am »
Thank you!

WEBmods

  • Hero Member
  • *****
  • Posts: 936
  • github.com/webmods-croatia/love-osclass/ | patrick
Re: Params::setParam- meta
« Reply #17 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.
« Last Edit: April 22, 2019, 12:57:53 pm by patrickFromCroatia »

Sophia_OS

  • Sr. Member
  • ****
  • Posts: 416
Re: Params::setParam- meta
« Reply #18 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'

WEBmods

  • Hero Member
  • *****
  • Posts: 936
  • github.com/webmods-croatia/love-osclass/ | patrick
Re: Params::setParam- meta
« Reply #19 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.

Sophia_OS

  • Sr. Member
  • ****
  • Posts: 416
Re: Params::setParam- meta
« Reply #20 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!