Initial_filter for pupup

Hello everyone, I will briefly explain my situation:
I have a custom module that has a field related to the Users module, what I would like to do is to set an initial filter that shows the list of a certain group of users, so far so good, in editviewdefs.php of the custom module I set the initial filter via:

...
'displayParams' => 
            array (
			  'initial_filter' => '&<field_relate>_name_advanced="+this.form.{$fields.<field_relate>.name}.value+"',
);
...

now when the popup window opens, the related field appears set with the initial_filter value, the problem is that if this field is changed of course the filter is applied to the whole Users form. If I remove the Users field from the filter, it works only when I open the popup, but if I click on the “search” button, without setting anything, it shows me the list of all users, losing the initial filter.
Any ideas on how to fix this please? Thanks

@fab
You can add to file: custom/modules/<module_name>/metadata/popupdefs.php the code like this:

$parametrs_advanced='`<field_relate>_name`="'.$_REQUEST['<field_relate>_name_advanced'].'"'
$popupMeta = array (
...
'whereStatement'=> "$parametrs_advanced",
...

Yes, I thought about something like that, for a search popup inside a subpanel I solved it this way:

in
custom/extension/modules/<module>/ext/Layoutdefs/<subpanel>.php

I inserted

field_to_name_array'=>array('name' =>$this->_focus->name),

and in popupdefs

$tmp= json_decode(html_entity_decode($_REQUEST['request_data']),true);
$where_param= 'name LIKE "'.$tmp['field_to_name_array']['name'].'"';

$popupMeta = array(
    ...
	'whereStatement'=> $where_param,
   ...
);

but in this case it doesn’t work,
1, because
$tmp['field_to_name_array']['name'] is empty, not even $this->_focus->name collects anything;
2, since it is a relate field, if in $where_param I insert the relate field, it doesn’t recognize it

@fab

Sorry, I don’t understand you.
Why do you work with ‘subpanel’? But you should work with ‘popup’.

Yes, now I’m working with popups, above I described how I solved a similar problem, but in that case I was working with subpanels and I thought I could solve it in the same way, but I couldn’t.

So, I have two problems:

  1. $_REQUEST[‘name_advanced’] works only the first time I open the popup, then, if I click on the search button without setting anything, it empties out
    in the file custom/modules//metadata/editviewdefs there is this:


    ‘displayParams’ =>
    array (
    ‘initial_filter’ => ‘&name_advanced="+this.form.{$fields.<field_relate>.name}.value+"’,

               'field_to_name_array' => 
               array (name_advanced =>'...');
               ),
    

whatever I put in name_advanced (inside field_to_name_array), is treated as a string, so if I enter:

 'field_to_name_array' => 
              array (name_advanced =>'hello');
              ),

$_REQUEST[‘name_advanced’] will show hello. I should get the value back. like the one contained in initial_filter, I tried entering

 field_to_name_array' => 
              array (name_advanced =>'this.form.{$fields.<field_relate>.name}.value');
              ),

but it doesn’t work

  1. the second problem is consequent to the second one, after retrieving the value, if I do

$where_param= ‘name_pro LIKE "’.$_REQUEST[‘name_advanced’].’"’;

it goes in error, because the field name_pro is not a users field, but it is a field of my custom module that is related to Users

@fab

  1. You can use two variables for filtering data transmission:
  • $_REQUEST['request_data']
  • $_SESSION
  1. You should write correct parameter WHERE of SQL request.

What I can’t do at the moment is to insert in

field_to_name_array (‘name_pro’=>…)

the value that I take through

$tmp= json_decode(html_entity_decode($_REQUEST['request_data']),true);
$GLOBALS['log']->fatal($tmp['field_to_name_array']['name_pro']);

I’m trying everything

@fab

  1. Code for custom/modules/<module_name_1>/metadata/editviewdefs.php (as you wrote in the first post):
...
'displayParams' => 
    array (
      'initial_filter' => '&<field_relate>_name_advanced="+this.form.{$fields.<field_relate>.name}.value+"',
);
...
  1. Code for custom/modules/<module_name_2>/metadata/popupdefs.php:
    if(!empty($_REQUEST['<field_relate>_name_advanced'])){
        $parametrs_advanced='`<field_relate>_name`="'.$_REQUEST['<field_relate>_name_advanced'].'"'; // example part of SQL WHERE
        $_REQUEST['request_data']=json_encode(htmlentities(array('<field_relate>_name_advanced'=>$_REQUEST['request_data'])),true);
    }else{
        $tmp=json_decode(html_entity_decode($_REQUEST['request_data']),true);
        $parametrs_advanced='`<field_relate>_name`="'.$tmp['<field_relate>_name_advanced'].'"';// example part of SQL WHERE
    }
    $popupMeta = array (
        ...
        'whereStatement'=> "$parametrs_advanced",
        ...
    );
  1. You should write correct parameter WHERE of SQL request.

I tried your code, in the first if

$_REQUEST['request_data']=json_encode(htmlentities(array('<field_relate>_name_advanced'=>$_REQUEST['request_data']),true);

results in NULL

in the second if

$tmp=json_decode(html_entities_decode($_REQUEST['request_data']),true);

results empty

@fab

Sorry. I made mistake. The right code:

$_REQUEST['request_data']=json_encode(htmlentities(array('<field_relate>_name_advanced'=>$_REQUEST['<field_relate>_name_advanced']),true);

Same result in the first if

$_REQUEST['request_data']

results in NULL

in the second if

$tmp

results empty

@fab

Do you have data in variable $_REQUEST['<field_relate>_name_advanced']?

Did you not forget change name ‘<field_relate>_name_advanced’ on correct name?

Yes, the name is correct, $_REQUEST['<field_relate>_name_advanced'] returns the correct value only the first time, when the popup appears, then when I click the search button (with no value set), $_REQUEST['<field_relate>_name_advanced'] is empty

I don’t know if correct, anyway I modified the code like this:

if(!empty($_REQUEST['<field_relate>_name_advanced'])){
        $parametrs_advanced='`<field_relate>_name`="'.$_REQUEST['<field_relate>_name_advanced'].'"'; // example part of SQL WHERE
        $_REQUEST['request_data']=$_REQUEST['<field_relate>_name_advanced'];
    }else{
        $parametrs_advanced='<field_relate>_name="'.$_REQUEST['request_data'].'";// example part of SQL WHERE
    }

and it works, now the problem is the query, it gives me an error because it rightly doesn’t recognize the <field_relate>_name field within the Users form, since it is related field of my custom form, how do I tell it to refer to that field which is not in the Users form?

@fab

At first I decided that you know that you can to use variable $_REQUEST[‘request_data’]. But it isn’t correct.

I tested decision with saving data in variable $_SESSION.

if(!empty($_SESSION['popupMetaFilter'][$module_name][$_REQUEST['action']])&&empty($_REQUEST['name_advanced'])){
    $_REQUEST['name_advanced']=$_SESSION['popupMetaFilter'][$module_name][$_REQUEST['action']]['name_advanced'];
}else{
    $_SESSION['popupMetaFilter'][$module_name][$_REQUEST['action']]['name_advanced']=$_REQUEST['name_advanced'];
}

Don’t modify $popupMeta['whereStatement']=>''

I do not believe, it works great, in a few lines solved both problems!
You are great @p.konetskiy, thank you very much!!!