【问题标题】:How to properly display a calculated value of a non-db field in the ListView?如何在 ListView 中正确显示非 db 字段的计算值?
【发布时间】:2019-03-08 21:56:32
【问题描述】:

在 Suitecrm/SugarCRM 6.5 中

我需要修改 ListView。根据特定条件显示字段的值。

在帐户模块中,name 字段根据某些条件可能为空,我要求在发生这种情况时显示自定义字段的值。为此,首先我在编辑/细节视图中像往常一样尝试使用“customCode”+ smarty,但在几篇文章中提到这在 ListView 中是不可能的。让步是使用逻辑钩子 + 一个非 db 字段来存储计算的字段。按照这个 SO answer 我写了以下内容:

custom/Extension/modules/Accounts/Ext/Vardefs/custom_field_name_c.php 的自定义字段

<?php

$dictionary['Account']['fields']['name_c']['name'] = 'account_name_c';
$dictionary['Account']['fields']['name_c']['vname'] = 'LBL_ACCOUNT_NAME_C';
$dictionary['Account']['fields']['name_c']['type'] = 'varchar';
$dictionary['Account']['fields']['name_c']['len'] = '255';
$dictionary['Account']['fields']['name_c']['source'] = 'non-db';
$dictionary['Account']['fields']['name_c']['dbType'] = 'non-db';
$dictionary['Account']['fields']['name_c']['studio'] = 'visible';

标签suitecrm/custom/Extension/modules/Accounts/Ext/Language/es_ES.account_name_c.php

<?php

$mod_strings['LBL_ACCOUNT_NAME_C'] = 'Nombre de cuenta';

custom/modules/Accounts/logic_hooks.php 处的逻辑挂钩条目

$hook_array['process_record'] = Array();
$hook_array['process_record'][] = Array(
    2,
    'Get proper name',
    'custom/modules/Accounts/hooks/ListViewLogicHook.php',
    'ListViewLogicHook',
    'getProperName'
);

custom/modules/Accounts/hooks/ListViewLogicHook.php 处的逻辑钩子

<?php

class ListViewLogicHook
{
    public function getProperName(&$bean, $event, $arguments)
    {
        if (empty($bean->fetched_row['name'])) {
            $bean->account_name_c = $bean->fetched_row['person_name_c'];
        } else {
            $bean->account_name_c = $bean->fetched_row['name'];
        }

        // Here I also tried, but same result
        // $bean->account_name_c = empty($bean->name) ? $bean->person_name_c : $bean->name;
    }
}

listviewdefs.php custom/modules/Accounts/metadata/listviewdefs.php

我添加了字段

'NAME_C' =>
  array (
      'width' => '20%',
      'label' => 'LBL_ACCOUNT_NAME_C',
      'default' => true,
  ),

在这些修改和修复之后,我希望看到该字段充满正确的值。但它似乎是空的。我已经验证逻辑钩子被正确调用,但nameperson_name_c 字段始终为空。如果相关,我已经在 Studio 中验证了 Account 中的 name 字段是 typename 我不知道在获取其值时这意味着什么。

感谢你的cmets

【问题讨论】:

    标签: php sugarcrm suitecrm


    【解决方案1】:

    问题出在逻辑钩子中,首先是$bean 无法访问自定义字段,所以我必须使用$bean-&gt;custom_fields-&gt;retrieve(); 调用它们而且名称字段始终为空,我不得不使用DBManagerto get only the name field.

    最终逻辑钩子的逻辑如下:

    <?php
    
    class ListViewLogicHook
    {
        public function getProperName($bean, $event, $arguments)
        {
            // Get access to custom fields from $bean
            $bean->custom_fields->retrieve();
    
            // Get access to name property using DBManager because $bean->name return null
            $sql = "SELECT name FROM accounts WHERE id = '{$bean->id}'";
            $name = $GLOBALS['db']->getOne($sql);
    
            // Assign a value to non-db field
            $bean->name_c = empty($name) ? $bean->nombre_persona_c : $name;
        }
    }
    

    我不熟悉方法$bean-&gt;custom_fields-&gt;retrieve(),目前我不知道为什么name字段为空,我知道其他字段仍然为空。

    希望对你有用

    【讨论】:

    • name 字段是由 first_name 和 last_name 字段组成的,这可能是它处理方式不同的原因。可能最简单的解决方案是将这两个字段连接起来,从而避免额外的查询。
    【解决方案2】:

    我们通过 after_retrieve 钩子实现了这一点,非常快速和简单 - 这取自一个工作示例。

    public function RemoveCost(&$bean, $event, $arguments)
    {
     global $current_user;
     include_once(‘modules/ACLRoles/ACLRole.php’);
     $roles =  ACLRole::getUserRoleNames($current_user->id);
     if(!array_search("CanSeeCostRoleName",$roles)){
      $bean->cost = 0;
      $bean->cost_usdollar = 0;
     }
    }
    

    你只需要定义这个函数并将其添加到模块 logic_hooks.php

    您甚至可以针对特定呼叫进行定制:

    if (isset($_REQUEST['module'],$_REQUEST['action']) &amp;&amp; $_REQUEST['module'] == 'Opportunities' &amp;&amp; $_REQUEST['action'] == 'DetailView')

    有时您确实希望显示该字段的视图,例如快速搜索弹出窗口。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 2017-06-18
      • 1970-01-01
      • 2017-03-31
      相关资源
      最近更新 更多