Skip to content

Instantly share code, notes, and snippets.

@tantebootsy
Forked from anonymous/gist:888297
Last active February 14, 2023 19:21
Show Gist options
  • Save tantebootsy/fca52d4bbb59371a7d01 to your computer and use it in GitHub Desktop.
Save tantebootsy/fca52d4bbb59371a7d01 to your computer and use it in GitHub Desktop.
<?php
namespace NameOfVendor\NameOfExtension\ViewHelpers;
/***************************************************************
* Copyright notice
*
* (c) 2010 Franz Koch <typo3@elements-net.de>, Koch & Koch GbR
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Is fetching property values from TCA
*
* Usage:
* <nameOfNamespace:TcaOptions property="nameOfProperty" subject="NameOfVendor\\NameOfExtension\\Domain\\Model\\NameOfModel" />
*
* respectively via inline-syntax:
* <f:form.select options="{nameOfNamespace:TcaOptions(property:'nameOfProperty',subject:'NameOfVendor\\NameOfExtension\\Domain\\Model\\NameOfModel')}" />
*
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
*/
class TcaOptionsViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
/**
* local cache for dataMaps
* @var array
*/
protected $dataMaps = array();
/**
* @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
* @inject
*/
protected $dataMapper;
/**
* objectManager
*
* @var \TYPO3\CMS\Extbase\Object\ObjectManager
* @inject
*/
protected $objectManager;
/**
* Is returning select values for a property or propertyPath of a given object or className
* @param mixed $subject The object or className the property belongs to
* @param string $property The property itself
* @return array The select options as array
*/
public function render($subject = NULL, $property = '') {
if (!is_object($subject) && is_string($subject) && class_exists($subject)) {
$subject= $this->objectManager->get($subject);
}
return self::getSelectOptions($property, $subject);
}
/**
* Is resolving the select values for a property or propertyPath of a given object or className
* @param string $propertyPath The property itself
* @param object $subject The object or className the property belongs to
* @return array The select options as array
*/
private function getSelectOptions($propertyPath, $subject) {
$selectOptions = array();
if (is_object($subject)) {
$propertyPathSegments = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode('.', $propertyPath);
$object = clone($subject);
$propertyName = $propertyPath;
// resolve a property path
if (count($propertyPathSegments) > 1) {
foreach($propertyPathSegments as $key => $propertyName) {
$propertyType = $this->dataMapper->getType(get_class($tempObject), $propertyName);
if (strpos($propertyType,'_')) {
$object = $this->objectManager->create($propertyType);
} else {
break;
}
}
}
$dataMap = self::getDataMap($object);
if ($dataMap == NULL || !$dataMap->isPersistableProperty($propertyName)) {
return $selectOptions;
}
$tableName = $this->dataMapper->convertClassNameToTableName(get_class($object));
$columnName = $dataMap->getColumnMap($propertyName)->getColumnName();
// only convert select items which do not have a DB relation
$columnConfig = $GLOBALS['TCA'][$tableName]['columns'][$columnName]['config'];
if ($columnConfig['type'] == 'select' && count($columnConfig['items']) && !$columnConfig['foreign_table']) {
foreach ($columnConfig['items'] as $option) {
$selectOptions[$option[1]] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($option[0], $this->controllerContext->getRequest()->getControllerExtensionName());
}
}
}
return $selectOptions;
}
/**
* Resolve the dataMap for the given object
* @param object $object The domain object to get the dataMap for
* @return Tx_Extbase_Persistence_Mapper_DataMap
*/
private function getDataMap($object) {
$class = is_object($object) ? get_class($object) : $object;
if (!isset($this->dataMaps[$class])) {
$this->dataMaps[$class] = $this->dataMapper->getDataMap($class);
}
return $this->dataMaps[$class];
}
}
?>
@typo3ua
Copy link

typo3ua commented Jan 28, 2023

Hello! Something wrong... it does not work..
I have the TCA

                 'citytown' => [
                    'exclude' => true,
                    'label' => 'City's',
                    'config' => [
                        'type' => 'select',
                        'renderType' => 'selectSingle',
                        'items' => [
                            ['City 0','0'],
                            ['City 1','1'],
                            ['City 2','2'],
                        ],
                        'size' => 1,
                        'maxitems' => 1,
                    ],
                ],

then I added...

<f:form.select options="{nameOfNamespace:TcaOptions(property:'citytown',subject:'NameOfVendor\\NameOfExtension\\Domain\\Model\\NameOfModel')}" />

I get ...

Extbase Variable Dump
array(empty)

I expected get

<select required="required" class=" form-control" id="citytown" name="citytown>
   <option value="0">City 0</option>
   <option value="1">City 1</option>
   <option value="2">City 2</option>
</select>

what is wrong?

@tantebootsy
Copy link
Author

tantebootsy commented Feb 14, 2023

hi : ) it cannot work like that, because you try to do it with the default select-viewhelper of TYPO3, which – as far as i know – does not offer to get its values from TCA. you have to create your own vewhelper and try to achieve it with that one. checkout https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/Fluid/DevelopCustomViewhelper.html to understand the basics first.

and check out the comment of this class in which it is explained how to use the viewhelper:

<nameOfNamespace:TcaOptions property="nameOfProperty" subject="NameOfVendor\NameOfExtension\Domain\Model\NameOfModel" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment