📣 We're Hiring!

How to modify editviewdefs.php from logic_hooks

As per the subject, I would like to edit the editviewdefs.php file from file via logic_hooks. The purpose is to create new fields for a certain module, not knowing first what fields I need to create, I can’t do it via Studio and so I necessarily have to do it via code.
This is the editviewdefs of a subpanel, so once the editviewdefs is midified I have to edit the subpanel layout (metadata/subpanels/…) as well.
Can anyone tell me how to do this? Thanks

@fab

Do you want to modify field list for the form or also create new fields?

I would like to create new fields

I tried to insert manually a field in metadata/subpanles/.php
test’ =>
array (
‘vname’ => ‘TEST’,
‘width’ => ‘45%’,
‘default’ => true,
),
and in the subpanel command the TEST field, then you can add fields via code, but I need to insert this code inside a loop, so as to add N fields.
How do I edit metadata/subpanles/.php from logic_hooks?

@fab

Logichook doesn’t works for forms. It works for fields data only. You can write anything code but it will not correctly.

But if through code I can insert a field, somehow I can make this procedure automatic… if not through logic_hooks, at least through view.detail.php or something else.

I found this on the net (it should be what I’m looking for), but I can’t figure out where to insert the code:
https://sugarclub.sugarcrm.com/dev-club/f/questions-answers/882/dynamically-add-fields-to-sub-panel-on-editiviewdefs-php

@fab

This code add copy of fields of module Accounts in module GDLF_Gandalf_Table. The fields type is non-db. I think that the $dictionary is using as global variable.

From what I see, it creates an array of fields ($filteredKeys=['id', 'deleted'];), if these exist in Accounts ok, otherwise it adds them…
anyway i need something like this but in $subpanel_layout['list_fields']
I can’t believe it’s not possible to do something like this

@fab
Look at reply of autor post himself.

“Store each of the available fields from Account into array to custom fields in Gandalf Table”
maybe i’m misinterpreting it, but the title
“Dynamically add fields to sub-panel on editiviewdefs.php”
seemed appropriate to my case…
Anyway, the problem remains, there must be a way to add a new field by code

I finally managed to edit the editviewdefs.php file in this way:
through logic_hooks I created a file in which there is

require_once('modules/ModuleBuilder/parsers/ParserFactory.php');
$parser = ParserFactory::getParser('editview', 'Module_b');

$field= array(
   $bean->name
);

array_push($parser->_viewdefs['panels']['default'], $field);

$parser->handleSave(false);

now there is one last step left, what appears inside editview is this:

2=>array(
),

the field is added at the desired position, but it is empty. Doing a print_r before and after $parser->handleSave(false),
the file is written correctly, but something is lost when saving.
I tried changing the contents of $field like this:

$field= array(
   array(
   $bean->name
),
);

in this case, the editview file looks like this:

2=>array(
    0=>array(
       0=><field_name>,
),
),

if I add another one immediately afterwards, the result will be:

2=>array(
),
3=>array(
    0=>array(
       0=><field_name>,
),
),

I can’t figure out what’s wrong. Please wait for suggestions

for completeness, this is my editviewdefs.php:

<?php
$module_name = 'Module_b';
$viewdefs [$module_name] = 
array (
  'EditView' => 
  array (
    'templateMeta' => 
    array (
      'maxColumns' => '2',
      'widths' => 
      array (
        0 => 
        array (
          'label' => '10',
          'field' => '30',
        ),
        1 => 
        array (
          'label' => '10',
          'field' => '30',
        ),
      ),
      'useTabs' => false,
      'tabDefs' => 
      array (
        'DEFAULT' => 
        array (
          'newTab' => false,
          'panelDefault' => 'expanded',
        ),
      ),
    ),
    'panels' => 
    array (
      'default' => 
      array (
        0 => 
        array (
          0 => 'name',
          1 => '',
        ),
        1 => 
        array (
          0 => 'description',
          1 => '',
        ),
      ),
    ),
  ),
);
;
?>

OK, I’ve solved it, the problem was the array $field, you have to pass

$field= array(
			array(
            name' => $bean->name."_c",
            'label' => $bean->name,
          ),
		);

for those who are interested, above I have described the whole procedure to edit the editviewdefs

@fab

I glad that it works for you.
I am sorry but I don’t think that it’s a good solution. I can’t recommend to use it and I write my opinion only.

May I ask why this is a solution that you do not recommend?

@fab

This is my opinion only:
As programmer:

  1. The file editviewdefs.php is changing dynamically using ModuleBuilder. This is an additional load on the server.
  2. You should switch off using SuiteCRM cache (switch on Developer mode) or insert addition control to the module.

As analyst:

  1. I don’t sure that you use optimal object model for you business. When I have met such situations I did objects revision. I am adding one or several object to logic usually. The end user don’t see that they was added in most cases. I can’t give you concrete solution because I don’t know a lot details. (I wrote about it already.)
  2. It’s maybe don’t comfortable for users because they will see a lot of different forms in one place. If it was just a few fixed forms it would be normal but there will be as many forms as SuiteCRM will create in your situation.