简体   繁体   中英

TYPO3 TCA userfunc

Kinda stuck.

We have products which we offer to companies. so it would be an mm field which is no problem to implement, but the problem comes with date, since each product has expire date based on company.

so decided to remove product mm and put product id with date into company as one field.

but since input which we send is an array it's not possible to save it there.

How can i solve this problem ? this is my tca for that field:

'product' => [
    'exclude' => true,
    'label' => 'LLL:EXT:wemessage_checklist/Resources/Private/Language/locallang_db.xlf:tx_wemessagechecklist_domain_model_company.product',
    'config' => [
       'type' => 'user',
       'userFunc' => Wemessage\WemessageChecklist\UserFunc\Checklist::class.'->renderChecklists',               
    ],
],

Here is function to render html:

public function renderChecklists($PA, $fObj){
    $checked = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'tx_wemessagechecklist_domain_model_company', 'hidden=0 and deleted=0 and uid='.$PA['row']['uid'].' and pid='.$PA['row']['pid']);
    $allProducts = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'tx_wemessagechecklist_domain_model_product', 'hidden= 0 and deleted = 0');
    foreach($allProducts as $product){
        $html .= '<div>';
        $html .= '<input type="hidden" name="data[tx_wemessagechecklist_domain_model_company]['.$PA['row']['uid'].'][product]['.$product['uid'].']" class="product"/>';
        $html .= '<input type="checkbox" class="tocheck" value="'.$product['uid'].'" /><span style="margin: 0 10px;">'.$product['name'].'</span><span style="margin: 0 10px;">Verval datum</span><input type="date" class="date" />';
        $html .= '</div>';
    }
    $html .= '<script>
        TYPO3.jQuery(".tocheck").click(function(){
            var val = TYPO3.jQuery(this).val();
            if(TYPO3.jQuery(this).prop("checked")){
                TYPO3.jQuery(this).parent().find(".product").val(val);
            }
        });
        TYPO3.jQuery(".date").change(function(){
            var x = new Date(TYPO3.jQuery(this).val()).getTime();
            var b = TYPO3.jQuery(this).parent().find(".product").val();
            TYPO3.jQuery(this).parent().find(".product").val(b+":"+x);
        });
        </script>';
    return $html;
}

probably another option is to implement AJAX calls to process data in standalone table. although if there is another solution will be glad to hear it.

You should still go for an MM relation or better inline (IRRE) connection from company to product, since these can have an own TCA too, thus making it possible to add starttime, endtime and other values to the MM relation record itself.

The concept is called "intermediate table" and is described here: https://docs.typo3.org/typo3cms/TCAReference/ColumnsConfig/Type/Inline.html#attributes-on-anti-symmetric-intermediate-table

Here is a modified TCA taken from the IRRE tutorial extension. It contains just the necessary settings for the connections and the timestamp field - feel free to add your own fields for companies and products:

$TCA["company"] = Array (
    "columns" => Array (
        "products" => Array (
            "config" => Array (
                "type" => "inline",
                "foreign_table" => "company_product_intermediate",
                "foreign_field" => "company_id",
                "foreign_sortby" => "companysort",
                "foreign_label" => "product_id",
                "maxitems" => 10,
                'appearance' => array(
                    'showSynchronizationLink' => 1,
                    'showAllLocalizationLink' => 1,
                    'showPossibleLocalizationRecords' => 1,
                    'showRemovedLocalizationRecords' => 1,
                ),
                'behaviour' => array(
                    'localizationMode' => 'select',
                ),
            )
        ),
    ),
);



$TCA["company_product_intermediate"] = Array (
    "columns" => Array (
        "company_id" => Array (
            "config" => Array (
                "type" => "select",
                "foreign_table" => "company",
                "maxitems" => 1,
            )
        ),
        "product_id" => Array (
            "config" => Array (
                "type" => "select",
                "foreign_table" => "product",
                "maxitems" => 1,
            )
        ),
        "companysort" => Array (
            "config" => Array (
                "type" => "passthrough",
            )
        ),
        "productsort" => Array (
            "config" => Array (
                "type" => "passthrough",
            )
        ),
        "timestamp" => Array (
            "config" => Array (
                "type" => "input",
            )
        ),
    ),
);



$TCA["product"] = Array (
    "columns" => Array (
        "companies" => Array (
            "config" => Array (
                "type" => "inline",
                "foreign_table" => "company_product_intermediate",
                "foreign_field" => "product_id",
                "foreign_sortby" => "productsort",
                "foreign_label" => "company_id",
                "maxitems" => 10,
                'appearance' => array(
                    'showSynchronizationLink' => 1,
                    'showAllLocalizationLink' => 1,
                    'showPossibleLocalizationRecords' => 1,
                    'showRemovedLocalizationRecords' => 1,
                ),
                'behaviour' => array(
                    'localizationMode' => 'select',
                ),
            )
        ),
    ),
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM