Skip to content

Commit 35b91d1

Browse files
committed
allow configuring default sort behaviour in the model
1 parent 74be0b5 commit 35b91d1

File tree

5 files changed

+35
-22
lines changed

5 files changed

+35
-22
lines changed

bundles/org.zikula.modulestudio.generator/src/org/zikula/modulestudio/generator/cartridges/symfony/controller/config/ConfigureCrud.xtend

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ class ConfigureCrud implements ControllerMethodInterface {
8181
IF hasDateIntervalFieldsEntity»
8282
->setDateIntervalFormat(t('%%y year(s) %%m month(s) %%d day(s)'))
8383
«ENDIF*/»
84+
«IF null !== orderBy && !orderBy.empty»«/*
85+
* null values are not ignored,
86+
* see https://github.com/EasyCorp/EasyAdminBundle/issues/6879
87+
*/»
88+
->setDefaultSort([«orderByDetails(orderBy, 'crud')»])
89+
«ENDIF»
8490
->addFormTheme('@ZikulaTheme/Form/form_layout_addons.html.twig')
8591
'''
8692
}

bundles/org.zikula.modulestudio.generator/src/org/zikula/modulestudio/generator/cartridges/symfony/models/entity/Association.xtend

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class Association {
158158
*/
159159
#[ORM\ManyToMany(targetEntity: «entityClass»::class, mappedBy: '«targetName»'«additionalOptions(true)»)]
160160
«IF null !== orderByReverse && !orderByReverse.empty»
161-
#[ORM\OrderBy([«orderByDetails(orderByReverse)»])]
161+
#[ORM\OrderBy([«orderByDetails(orderByReverse, 'orm')»])]
162162
«ENDIF»
163163
«IF !nullable»
164164
«val aliasName = getRelationAliasName(false).toFirstLower»
@@ -232,7 +232,7 @@ class Association {
232232
«ENDIF»
233233
«joinDetails(true
234234
«IF null !== orderBy && !orderBy.empty»
235-
#[ORM\OrderBy([«orderByDetails(orderBy)»])]
235+
#[ORM\OrderBy([«orderByDetails(orderBy, 'orm')»])]
236236
«ENDIF»
237237
«IF !nullable»
238238
«val aliasName = getRelationAliasName(true).toFirstLower»
@@ -255,7 +255,7 @@ class Association {
255255
#[ORM\ManyToMany(targetEntity: «entityClass»::class«IF bidirectional», inversedBy: '«sourceName»'«ENDIF»«additionalOptions(false)»«outgoingMappingAdditions»)]
256256
«joinDetails(true
257257
«IF null !== orderBy && !orderBy.empty»
258-
#[ORM\OrderBy([«orderByDetails(orderBy)»])]
258+
#[ORM\OrderBy([«orderByDetails(orderBy, 'orm')»])]
259259
«ENDIF»
260260
«IF !nullable»
261261
«val aliasName = getRelationAliasName(true).toFirstLower»
@@ -342,23 +342,6 @@ class Association {
342342
else if (cascadeProperty == CascadeType.ALL) '\'all\''
343343
}
344344

345-
def private orderByDetails(String orderBy) {
346-
val criteria = newArrayList
347-
val orderByFields = orderBy.replace(', ', ',').split(',')
348-
349-
for (orderByField : orderByFields) {
350-
var fieldName = orderByField
351-
var sorting = 'ASC'
352-
if (orderByField.contains(':')) {
353-
val criteriaParts = orderByField.split(':')
354-
fieldName = criteriaParts.head
355-
sorting = criteriaParts.lastOrNull
356-
}
357-
criteria.add('"' + fieldName + '" = "' + sorting.toUpperCase + '"')
358-
}
359-
criteria.join(', ')
360-
}
361-
362345
def initCollections(Entity it) '''
363346
«FOR relation : getOutgoingCollections»«relation.initCollection(true)»«ENDFOR»
364347
«FOR relation : getIncomingCollections»«relation.initCollection(false)»«ENDFOR»

bundles/org.zikula.modulestudio.generator/src/org/zikula/modulestudio/generator/extensions/FormattingExtensions.xtend

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,28 @@ class FormattingExtensions {
134134

135135
output
136136
}
137+
138+
def orderByDetails(String orderBy, String context) {
139+
val criteria = newArrayList
140+
val orderByFields = orderBy.replace(', ', ',').split(',')
141+
142+
for (orderByField : orderByFields) {
143+
var fieldName = orderByField
144+
var sorting = 'ASC'
145+
if (orderByField.contains(':')) {
146+
val criteriaParts = orderByField.split(':')
147+
fieldName = criteriaParts.head
148+
sorting = criteriaParts.lastOrNull
149+
if (null === sorting || !#['ASC', 'DESC'].contains(sorting.toUpperCase)) {
150+
sorting = 'ASC'
151+
}
152+
}
153+
if ('orm' === context) {
154+
criteria.add('"' + fieldName + '" = "' + sorting.toUpperCase + '"')
155+
} else if ('crud' === context) {
156+
criteria.add('\'' + fieldName + '\' => \'' + sorting.toUpperCase + '\'')
157+
}
158+
}
159+
criteria.join(', ')
160+
}
137161
}

tests/org.zikula.modulestudio.generator.tests/model/test3.mostapp.xt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ application "SimpleNews" targets SF74 {
2626
lockType PESSIMISTIC_WRITE
2727
standardFields true
2828
approval true
29-
hasArchive true
3029
ownerPermission true
30+
hasArchive true
3131
fields {
3232
string "title" {
3333
sluggablePosition 1

tests/org.zikula.modulestudio.generator.tests/model/test4.mostapp.xt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ application "SimpleNews" targets SF74 {
2626
lockType OPTIMISTIC
2727
standardFields false
2828
approval false
29-
hasArchive false
3029
ownerPermission false
30+
hasArchive false
3131
fields {
3232
string "title" {
3333
sluggablePosition 1

0 commit comments

Comments
 (0)