设置 TYPO3 的自定义图标可直接从数据库中选择选项



我已经为具有TCA设置的数据库输入创建了一个表单。

其中之一是选择字段,我需要为每个选项使用不同的图标。这些图标名称(核心图标)存储在数据库字段图标中(可以更改):

+-----+----------------+------------------+
| uid | title          | icon             |
+-----+----------------+------------------+
| 1   | Active         | overlay-approved |
| 2   | Inactive       | overlay-readonly |
| 3   | Old            | overlay-info     |
| 5   | Limited access | overlay-locked   |
+-----+----------------+------------------+

主要数据很容易加载:

'issuer_id' => [
'exclude' => true,
'label'   => 'LLL:EXT:lu_nas/Resources/Private/Language/locallang_db.xlf:document.status',
'config'  => [
'type'                => 'select',
'eval'                => 'required',
'minitems'            => 0,
'maxitems'            => 1,
'foreign_table'       => 'tx_lunas_domain_model_status',
'foreign_table_where' => 'ORDER BY tx_lunas_domain_model_status.title ASC',
'items'               => [['', '',]],
],
],

其中,TCAtx_lunas_domain_model_status.php设置ctrl设置为使用标题'label' => 'title'作为名称。

我知道我也可以添加'iconfile' => 'EXT:lu_nas/Resources/Public/Icons/Status.svg'作为所有条目的默认图标,但我不需要(每个条目都需要不同的图标)。

到目前为止,我还发现我可以添加带有图标的自定义项目,如下所示:

'items' => [
['', ''],
['Limited access', 5, 'overlay-locked'],
['Inactive', 3, 'overlay-info'],
['Old', 2, 'overlay-readonly'],
['Active', 1, 'overlay-approved'],
],

但是,如何知道图标名称保存在哪个数据库列中,以便我可以直接从数据库加载此数据?

我很确定,你目前不能在TYPO3中做到这一点,但你可以使用userFunction。在那里,您只需为图标添加类即可。

'config' => [
'type' => 'user',
'userFunc' => YYYXXXTCATcaReferenceField::class . '->render',
]

代码将是这样的:

public function render(array $configuration, UserElement $userElement) {
$row = $configuration['row'];
// Do some Magic here.
$select = '<label style="font-weight: 400;">' . self::MESSAGE_FIELD_LABEL;
$select .= '<select name="' . $configuration['itemFormElName'] . '" class="form-control form-control-adapt" ' .
'onchange='' . $configuration['fieldChangeFunc']['alert'] . ''>';
$select .= '<option value=""></option>';
foreach ($contentElementUids as $siteName => $contentElementUid) {
$isSelected = ($contentElementUid === (int) $configuration['itemFormElValue']);
$select .= '<option ' . ($isSelected ? 'selected' : '') . ' value="' . $contentElementUid . '">' .
$siteName . '</option>';
}
$select .= '</select></label>';
return $select;
}

最新更新