Skip to content

Commit

Permalink
[FEATURE] Get categories by relation field instead of uid
Browse files Browse the repository at this point in the history
  • Loading branch information
schloram authored Sep 20, 2023
1 parent 520124e commit 6127b6c
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions Classes/DataProcessing/CategoriesProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;

/*
* Example usage (get categories by relation record):
* Example usage (get categories by relation field):
categories = JSON
categories {
dataProcessing {
10 = headless-categories
10 {
relationTable = pages
relationUid.field = uid
relation.fieldName = categories
as = categories
}
Expand Down Expand Up @@ -66,27 +65,38 @@ public function process(
return $processedData;
}

$relationTable = $cObj->stdWrapValue('relationTable', $processorConfiguration);
$relationUid = (int)$cObj->stdWrapValue('relationUid', $processorConfiguration);
$categoryIdList = (string)$cObj->stdWrapValue('categoryIdList', $processorConfiguration, '');

$defaultQueryConfig = [
'pidInList' => 'root',
'selectFields' => 'uid AS id,title',
];
$queryConfig = [];

$categoryIdList = (string)$cObj->stdWrapValue('categoryIdList', $processorConfiguration, '');
if (empty($categoryIdList) === false) {
$queryConfig = [
'where' => '{#sys_category.uid} IN (' . $categoryIdList . ')',
'languageField' => 0,
];
}

if (empty($relationTable) === false && empty($relationUid) === false) {
$queryConfig = [
'join' => 'sys_category_record_mm on sys_category_record_mm.uid_local = sys_category.uid',
'where' => '({#sys_category_record_mm.tablenames} = \'' . $relationTable . '\' AND {#sys_category_record_mm.uid_foreign}=' . $relationUid . ')',
];

if (!empty($processorConfiguration['relation.'])) {
$referenceConfiguration = $processorConfiguration['relation.'];
$relationField = $cObj->stdWrapValue('fieldName', $referenceConfiguration ?? []);
if (!empty($relationField)) {
$relationTable = $cObj->stdWrapValue('table', $referenceConfiguration, $cObj->getCurrentTable());

if (!empty($relationTable)) {
$queryConfig = [
'join' => 'sys_category_record_mm on sys_category_record_mm.uid_local = sys_category.uid',
'where' => '({#sys_category_record_mm.tablenames} = \'' . $relationTable . '\' AND {#sys_category_record_mm.fieldname} = \'' . $relationField . '\' AND {#sys_category_record_mm.uid_foreign}=' . $cObj->data['uid'] . ')',
];
}
}
}

if (empty($queryConfig) === true) {
return $processedData;
}

ArrayUtility::mergeRecursiveWithOverrule(
Expand Down

0 comments on commit 6127b6c

Please sign in to comment.