Thanks to this answer I have a viewhelper that gets an array of categories accociated with a file reference, and this works well in TYPO3 10, PHP 7.4.
After migrating to TYPO3 11, PHP 8.1, this now throws the error:
503 Call to a member function createQuery() on null
namespace myvendor\myext\ViewHelpers;
use TYPO3\CMS\Core\Resource\FileReference;
use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
/**
*
* @package TYPO3
* @subpackage myext
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 2 or later
* @author Marcus Biesioroff biesior@gmail.com>
*
* ViewHelper for listing file's categories
*
* Usage:
* {namespace toolbox=myvendor\myext\ViewHelpers}
* or in ext_tables.php:
* $GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces']['myext'] = ['myvendor\myext\ViewHelpers'];
*
* <cvh:fileCategories file="{file}" />
* or
* {cvh:fileCategories(file: file)}
*/
class FileCategoriesViewHelper extends AbstractViewHelper
{
/**
* @var \TYPO3\CMS\Extbase\Domain\Repository\CategoryRepository
* @TYPO3\CMS\Extbase\Annotation\Inject
*/
protected $categoryRepository ;
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('file', 'mixed', 'File');
}
public function render()
{
/** @var FileReference $fileRef */
$fileRef = $this->arguments['file'];
$file = $fileRef->getOriginalFile();
$uid = $file->getUid();
/** @var QueryResult $res */
$res = $this->getCategories($uid);
return $res->toArray();
}
private function getCategories($uid)
{
$query = $this->categoryRepository->createQuery();
$sql = "SELECT sys_category.* FROM sys_category
INNER JOIN sys_category_record_mm ON sys_category_record_mm.uid_local = sys_category.uid AND sys_category_record_mm.fieldname = 'categories' AND sys_category_record_mm.tablenames = 'sys_file_metadata'
INNER JOIN sys_file_metadata ON sys_category_record_mm.uid_foreign = sys_file_metadata.uid
WHERE sys_file_metadata.file = '" . (int)$uid . "'
AND sys_category.deleted = 0
ORDER BY sys_category_record_mm.sorting_foreign ASC";
return $query->statement($sql)->execute();
}
}
I tried making protected $categoryRepository = '';
but it gives the same error using string instead of null. So how can I make this work on TYPO3 11?
Try the following:
use TYPO3\CMS\Core\Resource\FileReference;
use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3\CMS\Extbase\Domain\Repository\CategoryRepository;
/**
*
* @package TYPO3
* @subpackage myext
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 2 or later
* @author Marcus Biesioroff biesior@gmail.com>
*
* ViewHelper for listing file's categories
*
* Usage:
* {namespace toolbox=myvendor\myext\ViewHelpers}
* or in ext_tables.php:
* $GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces']['myext'] = ['myvendor\myext\ViewHelpers'];
*
* <cvh:fileCategories file="{file}" />
* or
* {cvh:fileCategories(file: file)}
*/
class FileCategoriesViewHelper extends AbstractViewHelper
{
protected $categoryRepository;
public function __construct(
CategoryRepository $categoryRepository
) {
$this->categoryRepository = $categoryRepository;
}
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('file', 'mixed', 'File');
}
public function render()
{
/** @var FileReference $fileRef */
$fileRef = $this->arguments['file'];
$file = $fileRef;//->getOriginalFile();
$uid = $file->getUid();
/** @var QueryResult $res */
$res = $this->getCategories($uid);
return $res->toArray();
}
private function getCategories($uid)
{
$query = $this->categoryRepository->createQuery();
$sql = "SELECT sys_category.* FROM sys_category
INNER JOIN sys_category_record_mm ON sys_category_record_mm.uid_local = sys_category.uid AND sys_category_record_mm.fieldname = 'categories' AND sys_category_record_mm.tablenames = 'sys_file_metadata'
INNER JOIN sys_file_metadata ON sys_category_record_mm.uid_foreign = sys_file_metadata.uid
WHERE sys_file_metadata.file = '" . (int)$uid . "'
AND sys_category.deleted = 0
ORDER BY sys_category_record_mm.sorting_foreign ASC";
return $query->statement($sql)->execute();
}
}