Conditional statement inside array

Hi,
If someone would be willing to help me with my syntax.

  array (
  'widget_class' => 'SubPanelTopSelectButton',
  'mode' => 'MultiSelect',
  if 'parent_type' = 'Contacts' {
        'initial_filter_fields' => array('parent_name' => 'contacts_aos_products_1_name_advanced'),
  }
  ),

I have also tried

'initial_filter_fields' => ('parent_type'['Contacts'] ? array('parent_name' => 'contacts_aos_products_1_name_advanced') : null)

I know I can’t do that in an array but I am unable to figure the correct way. As you can see I am barely getting my head arround this.

Thanks

Hi,
the idea with ternary operator should work. Maybe its useful to do a smaller test and then apply the approach to your problem:

$a = "abc";
$b = "abc";
$testArray = array (
    'forum' => 'someValue',
    'ternaryTest' => ($a == $b) ? 'equal' : 'not equal'
);
print_r($testArray);

returns

Array ( [forum] => someValue [ternaryTest] => equal )

while the same test with $a = "def" returns

Array ( [forum] => someValue [ternaryTest] => not equal )

so, in your case I would expect something like:

$something = array (
    'widget_class' => 'SubPanelTopSelectButton',
    'mode' => 'MultiSelect',
    'initial_filter_fields' => ($parent_type == 'Contacts') ? array('parent_name'=>'contacts_aos_products_1_name_advanced'): null,
);

I’m a little bit guessing here (e.g.: where does the $parent_type come from).

<?php

 // created: 2020-02-27 14:31:21

$layout_defs["Cases"]["subpanel_setup"]['aos_products_cases_1'] = array (

  'order' => 100,

  'module' => 'AOS_Products',

  'subpanel_name' => 'default',

  'sort_order' => 'asc',

  'sort_by' => 'id',

  'title_key' => 'LBL_AOS_PRODUCTS_CASES_1_FROM_AOS_PRODUCTS_TITLE',

  'get_subpanel_data' => 'aos_products_cases_1',

  'top_buttons' => 

  array (

    0 => 

    array (

      'widget_class' => 'SubPanelTopButtonQuickCreate',

    ),

    1 => 

    array (

      'widget_class' => 'SubPanelTopSelectButton',

      'mode' => 'MultiSelect',

      'initial_filter_fields' => array('parent_name'=> 'contacts_aos_products_1_name_advanced'),

    ),

  ),

);

This is from custom/Extension/modules/Cases/Ext/Layoutdefs/aos_products_cases_1_Cases.php

I would say parent_type comes from the same place as parent_name but that is quite deep down the rabbit hole for my first time with php :grin:
If I had to guess that didn’t work because parent_type is not a function?
I appreciate the help.

The real issue here is not just getting the operator syntax right, it is when this code is executed, and what context there is for it (which variables you can reference). I very much doubt that you will be able to achieve what you want from here. When SuiteCRM is loading a view it’s not providing that code with much context, unless I am mistaken.

I think here, you’re defining field names, you’re not handling any record…

Maybe it’s better if you say what you’re trying to achieve, so people can advise you about a better approach.

'initial_filter_fields' => array('parent_name'=> 'contacts_aos_products_1_name_advanced'),

So fair this code works and it achieves that when products search pop-up is opened it populates contacts name from parent name. With this, you only get products assigned to that Contact without the need of typing it.

Now what I would like to do is for this to be only triggered if the parent_type is Contacts (because it can also be Accounts etc…)

I know parent_type will give me ‘Contacts’ so I was assuming conditional statement would work here.

So this is how it’s working now, I would like it to work only if Connected To: (parent_type) is Contacts.

I don’t think it will work there. I just tried setting a debugger breakpoint on the viewdefs assignments, and loading the account detail view. It doesn’t stop at the breakpoint. I don’t have time now to go and check exactly where this is loaded, but I bet you won’t have any bean available there for this kind of condition test.

Maybe try overriding the detail view and change the field’s contents there.