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!!!

@p.konetskiy how to place this line code:

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

I want to test on my end, don`t know if it is applicable to my concern https://community.suitecrm.com/t/relate-field-auto-populate-related-result-on-other-field/86748.

@p.konetskiy please nevermind I was able to figure it out. But I have another question how I can set my popupdef field as a readonly field.?

    'searchInputs' => array (
  1 => 'name',
  4 => 'custom_province_c',
),
  'searchdefs' => array (
  'name' =>
  array (
    'name' => 'name',
    'width' => '10%',
  ),
  'custom_province_c' =>
  array (
    'type'=>'readonly',
    'studio' => 'visible',
    'label' => 'LBL_PROVINCE',
    'width' => '10%',
    'name' => 'custom_province_c',
    'value' =>''.$_REQUEST['province_name'].'',
  ),

@fab , hello bro, were you able to fix it? I was able to implement it but my problem is when you search the name on a popup view there will be no result even the names are existing.

<?php
if(!empty($_REQUEST['city'])){
    $parameters_advanced='`dvt3_city_id_c`= "'.$_REQUEST['city'].'"'; // example part of SQL WHERE
}else{
    $parameters_advanced='`dvt3_city_id_c`=""';// example part of SQL WHERE
}

But it will work if I set the parameters to $parameters_advanced = " ". Check sample code below:

<?php
if(!empty($_REQUEST['city'])){
    $parameters_advanced='`dvt3_city_id_c`= "'.$_REQUEST['city'].'"'; // example part of SQL WHERE
}else{
    $parameters_advanced='';// example part of SQL WHERE
}

The only disadvantage on this code is even there is no value on the province which is my first field, all the data on city popupdefs will display. The pre-filtered customization will be pointless if it will display all data of the city.