Skip to content

Commit

Permalink
FEATURE: Make taxonomies sortable
Browse files Browse the repository at this point in the history
Since all Taxonomy items are nodes, the sortingIndex of the nodes is already
respected. Instead of just having an alphabetical sorting by title, we allow
sorting taxonomies in the backend module. This is helpful if taxonomies are used
e.g. as a filter in the frontend and you explicitly want to sort the items.
  • Loading branch information
lorenzulrich committed Jun 24, 2024
1 parent fbc1528 commit c9e537d
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 10 deletions.
69 changes: 62 additions & 7 deletions Classes/Controller/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,6 @@ public function vocabularyAction(NodeInterface $vocabulary)
$this->view->assign('vocabulary', $vocabulary);
$this->view->assign('defaultVocabulary', $this->getNodeInDefaultDimensions($vocabulary));
$taxonomies = $this->fetchChildTaxonomies($vocabulary);
usort($taxonomies, function (array $taxonomyA, array $taxonomyB) {
return strcmp(
$taxonomyA['node']->getProperty('title') ?: '',
$taxonomyB['node']->getProperty('title') ?: ''
);
});
$this->view->assign('taxonomies', $taxonomies);
}

Expand Down Expand Up @@ -501,7 +495,7 @@ public function updateTaxonomyAction(NodeInterface $taxonomy, array $properties)
public function deleteTaxonomyAction(NodeInterface $taxonomy)
{
if ($taxonomy->isAutoCreated()) {
throw new \Exception('cannot delete autocrated taxonomies');
throw new \Exception('cannot delete autocreated taxonomies');
}

$flowQuery = new FlowQuery([$taxonomy]);
Expand All @@ -518,5 +512,66 @@ public function deleteTaxonomyAction(NodeInterface $taxonomy)
$this->redirect('vocabulary', null, null, ['vocabulary' => $vocabulary]);
}

/**
* Move taxonomy up
*
* @param NodeInterface $taxonomy
* @return void
*/
public function moveUpTaxonomyAction(NodeInterface $taxonomy)
{
$this->persistenceManager->allowObject($taxonomy);

$flowQuery = new FlowQuery([$taxonomy]);
$nextSiblingNode = $flowQuery->prev()->get(0);
$taxonomy->moveBefore($nextSiblingNode);
$this->persistenceManager->persistAll();

foreach ($taxonomy->getOtherNodeVariants() as $otherNodeVariant) {
$otherNodeVariant->setIndex($taxonomy->getIndex());
}
$this->persistenceManager->persistAll();

$this->addFlashMessage(
sprintf('Moved up taxonomy %s', $taxonomy->getPath())
);

$vocabulary = $flowQuery
->closest('[instanceof ' . $this->taxonomyService->getVocabularyNodeType() . ']')
->get(0);

$this->redirect('vocabulary', null, null, ['vocabulary' => $vocabulary]);
}

/**
* Move taxonomy down
*
* @param NodeInterface $taxonomy
* @return void
*/
public function moveDownTaxonomyAction(NodeInterface $taxonomy)
{
$this->persistenceManager->allowObject($taxonomy);

$flowQuery = new FlowQuery([$taxonomy]);
$nextSiblingNode = $flowQuery->next()->get(0);
$taxonomy->moveAfter($nextSiblingNode);
$this->persistenceManager->persistAll();

foreach ($taxonomy->getOtherNodeVariants() as $otherNodeVariant) {
$otherNodeVariant->setIndex($taxonomy->getIndex());
}
$this->persistenceManager->persistAll();

$this->addFlashMessage(
sprintf('Moved down taxonomy %s', $taxonomy->getPath())
);

$vocabulary = $flowQuery
->closest('[instanceof ' . $this->taxonomyService->getVocabularyNodeType() . ']')
->get(0);

$this->redirect('vocabulary', null, null, ['vocabulary' => $vocabulary]);
}

}
2 changes: 1 addition & 1 deletion Configuration/Policy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ privilegeTargets:
'Sitegeist.Taxonomy:Module.ManageVocabularyActions':
matcher: 'method(Sitegeist\Taxonomy\Controller\ModuleController->(newVocabulary|createVocabulary|editVocabulary|updateVocabulary|deleteVocabulary)Action())'
'Sitegeist.Taxonomy:Module.ManageTaxonomyActions':
matcher: 'method(Sitegeist\Taxonomy\Controller\ModuleController->(newTaxonomy|createTaxonomy|editTaxonomy|updateTaxonomy|deleteTaxonomy)Action())'
matcher: 'method(Sitegeist\Taxonomy\Controller\ModuleController->(newTaxonomy|createTaxonomy|editTaxonomy|updateTaxonomy|deleteTaxonomy|moveUpTaxonomy|moveDownTaxonomy)Action())'
'Sitegeist.Taxonomy:SecondaryInspector.Tree':
matcher: 'method(Sitegeist\Taxonomy\Controller\SecondaryInspectorController->treeAction())'

Expand Down
26 changes: 24 additions & 2 deletions Resources/Private/Fusion/Backend/Views/Taxonomy.List.fusion
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ prototype(Sitegeist.Taxonomy:Views.Module.Taxonomy.List) < prototype(Neos.Fusion
</thead>
<tbody>
<Neos.Fusion:Loop items={taxonomies} itemName="taxon">
<Sitegeist.Taxonomy:Views.Module.Taxonomy.List.Item taxon={taxon}/>
<Sitegeist.Taxonomy:Views.Module.Taxonomy.List.Item taxon={taxon} isFirst={iterator.isFirst} isLast={iterator.isLast}/>
</Neos.Fusion:Loop>
</tbody>
</table>
Expand Down Expand Up @@ -72,6 +72,8 @@ prototype(Sitegeist.Taxonomy:Views.Module.Taxonomy.List) < prototype(Neos.Fusion

prototype(Sitegeist.Taxonomy:Views.Module.Taxonomy.List.Item) < prototype(Neos.Fusion:Component) {
taxon = null
isFirst = false
isLast = false

renderer = afx`
<tr>
Expand All @@ -94,6 +96,26 @@ prototype(Sitegeist.Taxonomy:Views.Module.Taxonomy.List.Item) < prototype(Neos.F

<Neos.Fusion:Fragment @if={Security.hasAccess("Sitegeist.Taxonomy:Module.ManageTaxonomyActions")}>

<Neos.Fusion:Link.Action
@if={!props.isFirst}
href.action="moveUpTaxonomy"
href.arguments.taxonomy={props.taxon.node.contextPath}
title={props.i8n.id('taxon.moveUp')}
class="neos-button neos-button-primary"
>
<i class="fas fa-arrow-up"></i>
</Neos.Fusion:Link.Action>
<span @if={props.isFirst} class="neos-button neos-disabled" style="width: 46px;"></span>
<Neos.Fusion:Link.Action
@if={!props.isLast}
href.action="moveDownTaxonomy"
href.arguments.taxonomy={props.taxon.node.contextPath}
title={props.i8n.id('taxon.moveDown')}
class="neos-button neos-button-primary"
>
<i class="fas fa-arrow-down"></i>
</Neos.Fusion:Link.Action>
<span @if={props.isLast} class="neos-button neos-disabled" style="width: 46px;"></span>
<Neos.Fusion:Link.Action
href.action="newTaxonomy"
href.arguments.parent={props.taxon.node.contextPath}
Expand Down Expand Up @@ -149,7 +171,7 @@ prototype(Sitegeist.Taxonomy:Views.Module.Taxonomy.List.Item) < prototype(Neos.F
</tr>

<Neos.Fusion:Loop items={props.taxon.children} itemName="child">
<Sitegeist.Taxonomy:Views.Module.Taxonomy.List.Item taxon={child}/>
<Sitegeist.Taxonomy:Views.Module.Taxonomy.List.Item taxon={child} isFirst={iterator.isFirst} isLast={iterator.isLast}/>
</Neos.Fusion:Loop>

`
Expand Down

0 comments on commit c9e537d

Please sign in to comment.