Skip to main content

Hướng dẫn tạo custom button trên Action Tab trong SuiteCRM8

Bước 1: Tạo custom button trong Action Tab SuiteCRM8

1.1. Tạo custom button trong Action Tab SuiteCRM8, mở file public/legacy/custom/modules/Accounts/metadata/detailviewdefs.php và thêm

'recordActions' => [
    'actions' => [
      ....
      'add-custom-button' => [
          'key' => 'add-custom-button',
          'labelKey' => 'LBL_ADD_CUSTOM_BUTTON',
          'asyncProcess' => true,
          'modes' => ['detail']
      ]
      ....
    ]
],

Đặt ‘asyncProcess’ => true, nếu bạn muốn một quy trình được xử lý trong phần backend. Bạn có thể tham khảo button "print-as-pdf"

1.2. Tạo tên cho custom button trong file public/legacy/custom/modules/Accounts/language/en_us.lang.php

$mod_strings = array(
    'LBL_ADD_CUSTOM_BUTTON' => 'Custom Button',
);

Bước 2: Tạo service để xử lý custom button

Tạo file extensions/customButton/modules/Accounts/Service/AddCustomButtonAction.php trong extension front-end như hướng dẫn của SuiteCRM8

<?php

namespace App\Extension\cstmButton\modules\Accounts\Service;

use ApiPlatform\Core\Exception\InvalidArgumentException;
use App\Process\Entity\Process;
use App\Module\Service\ModuleNameMapperInterface;
use App\Process\Service\ProcessHandlerInterface;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;

class AddCustomButtonAction implements ProcessHandlerInterface, LoggerAwareInterface
{
    protected const MSG_OPTIONS_NOT_FOUND = 'Process options is not defined';
    protected const PROCESS_TYPE = 'record-add-custom-button';

    
    private $moduleNameMapper;
    protected $logger;

    public function __construct(ModuleNameMapperInterface $moduleNameMapper) {
        $this->moduleNameMapper = $moduleNameMapper;
    }

    public function getProcessType(): string {
        return self::PROCESS_TYPE;
    }

    public function requiredAuthRole(): string {
        return 'ROLE_USER';
    }

    public function configure(Process $process): void {
        //This process is synchronous
        //We aren't going to store a record on DB
        //thus we will use process type as the id
        $process->setId(self::PROCESS_TYPE);
        $process->setAsync(false);
    }

    public function validate(Process $process): void {
        if (empty($process->getOptions())) {
            throw new InvalidArgumentException(self::MSG_OPTIONS_NOT_FOUND);
        }
    }

    public function run(Process $process)
    {
        $options = $process->getOptions();
        $module =  $options['module']; // or "your-module"
        
        $responseData = [
            'handler' => 'redirect',
            'params' => [
                'route' => "$module/example",  //example - is a custom view called
                'queryParams' => [
                ]
            ]
        ];
        $process->setStatus('success');
        $process->setMessages([]);
        $process->setData($responseData);
    }

    public function setLogger(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }
}

Bạn có thể tham khảo file core/backend/Process/Service/RecordActions/PrintAsPdfAction.php

Bước 3: Rebuild extension và repair SuiteCRM

3.1. rebuild lại extension

yarn run build-dev:myExt --watch

3.2. repair & rebuild từ admin

Nguồn: https://community.suitecrm.com/t/error-occurs-when-clicking-custom-button-in-suitecrm-8/86486/2

Link Git :

  • https://github.com/nguyenphudung93dn/Learning-SuiteCRM8/commit/f990f3115695b681dcfe83625cc936cc2f84f481
  • https://github.com/nguyenphudung93dn/Learning-SuiteCRM8/commit/bdcfa1694005ac136ceffee2f49ca98481f16f11