Integration

You can include UPS shipping functionality in other extensions by creating backend links which create new UPS Shipping entries. These links can contain parameters that populate the UPS-Shipping entry with default values.

use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;

$returnUrl = 'Return url';
$shippingPageUid = 'Page uid of container with UPS shipment entries';

$defVals = [];
$defVals['address_line'] = 'Example address 1';
$defVals['weight_unit'] = 'KGS';
$defVals['package_dimensions_unit'] = 'CM';

$uriParameter = [
    'returnUrl' => $returnUrl,
    'edit' => [
        'tx_ups_api_domain_model_shipping' => [
            $shippingPageUid => 'new'
        ]
    ],
    'defVals' => [
        'tx_ups_api_domain_model_shipping' => $defVals
        ]
    ];

$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
$uri = (string)$uriBuilder->buildUriFromRoute('record_edit', $uriParameter);

Example

This example shows how to add a button to an extension data record that creates a UPS-Shipping entry

1.Create a form element to display the button and a link to create a UPS Shipping entry.

<?php

namespace Vendor\Extension\Form\Element;

use TYPO3\CMS\Backend\Form\Element\AbstractFormElement;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class CreateShippingButton extends AbstractFormElement
{

    public function render(): array
    {
        $shippingPageUid = $this->data['parentPageRow']['uid'];
        $returnUrl = $this->data['returnUrl'];

        $dbRow = $this->data['databaseRow'];
        // Hier werden die UPS-Shipping Felder befüllt
        // z.B.
        $defVals = [
            'address_line' => $dbRow['address']
        ];

        $uriParameter = [
            'returnUrl' => $returnUrl,
            'edit' => [
                'tx_ups_api_domain_model_shipping' => [
                    $shippingPageUid => 'new'
                ]
            ],
            'defVals' => [
                'tx_ups_api_domain_model_shipping' => $defVals
            ]
        ];

        /**@var UriBuilder $uriBuilder */
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
        $uri = (string)$uriBuilder->buildUriFromRoute('record_edit', $uriParameter);

        $result = $this->initializeResultArray();
        $mainFieldHtml = [];
        $mainFieldHtml[] = '<div class="form-control-wrap">';
        $mainFieldHtml[] = '<div class="form-wizards-wrap">';
        $mainFieldHtml[] = '<div class="form-wizards-element">';
        $mainFieldHtml[] = '<a href="' . $uri . '" class="btn btn-default">New shipping</a>';
        $mainFieldHtml[] = '</div>';
        $mainFieldHtml[] = '</div>';
        $mainFieldHtml[] = '</div>';

        $result['html'] = implode(LF, $mainFieldHtml);
        return $result;
    }
}

2.Register a render type in ext_localconf.php

$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry']['1610980654'] = [
    'nodeName' => 'createShippingButton',
    'priority' => 40,
    'class' => \Vendor\Extension\Form\Element\CreateShippingButton::class,
];

3.Add button to TCA in Configuration/TCA/Overrides/tx_extension_domain_model_tablename.php

<?php
defined('TYPO3_MODE') or die();

$extKey = 'extension';
$table = 'tx_extension_domain_model_tablename';
$lll = 'LLL:EXT:' . $extKey . '/Resources/Private/Language/locallang_db.xlf:' . $table;

if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('ups_api')){

    \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
        'tx_extension_domain_model_tablename',
        [
            'create_shipping' => [
                'label' => $lll . '.createShipping',
                'config' => [
                    'type' => 'none',
                    'renderType' => 'createShippingButton'
                ]
            ]
        ]
    );
    \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
        'tx_extension_domain_model_tablename',
        'create_shipping'
    );
}