Skip to content

Commit 5b11f19

Browse files
author
Jim Graham
authored
Fix problems in lexicon creation form (#15892)
* Updates to address issue #15223 * Update Create.php * Alter trimming of data to exclude value field
1 parent 08cb03d commit 5b11f19

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

core/src/Revolution/Processors/Workspace/Lexicon/Create.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/*
34
* This file is part of MODX Revolution.
45
*
@@ -43,12 +44,17 @@ public function getLanguageTopics()
4344
*/
4445
public function process()
4546
{
46-
if ($this->alreadyExists()) {
47+
$data = [];
48+
foreach ($this->getProperties() as $k => $v) {
49+
$data[$k] = $k === 'value' ? $v : trim($v);
50+
}
51+
52+
if ($this->alreadyExists($data)) {
4753
return $this->failure($this->modx->lexicon('entry_err_ae'));
4854
}
4955

5056
$this->entry = $this->modx->newObject(modLexiconEntry::class);
51-
$this->entry->fromArray($this->getProperties());
57+
$this->entry->fromArray($data);
5258
$this->entry->set('editedon', date('Y-m-d h:i:s'));
5359

5460
if ($this->entry->save() === false) {
@@ -61,13 +67,13 @@ public function process()
6167
/**
6268
* @return bool
6369
*/
64-
public function alreadyExists()
70+
public function alreadyExists($data)
6571
{
6672
return $this->modx->getCount(modLexiconEntry::class, [
67-
'name' => $this->getProperty('name'),
68-
'namespace' => $this->getProperty('namespace'),
69-
'language' => $this->getProperty('language'),
70-
'topic' => $this->getProperty('topic'),
73+
'name' => $data['name'],
74+
'namespace' => $data['namespace'],
75+
'language' => $data['language'],
76+
'topic' => $data['topic']
7177
]) > 0;
7278
}
7379

core/src/Revolution/Processors/Workspace/Lexicon/UpdateFromGrid.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/*
34
* This file is part of MODX Revolution.
45
*
@@ -52,6 +53,9 @@ public function initialize()
5253
if (empty($data)) {
5354
return $this->modx->lexicon('invalid_data');
5455
}
56+
foreach ($data as $k => $v) {
57+
$data[$k] = $k === 'value' ? $v : trim($v);
58+
}
5559
$this->setProperties($data);
5660
$this->unsetProperty('data');
5761

core/src/Revolution/modLexiconEntry.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,46 @@ public function clearCache()
3838
return false;
3939
}
4040

41+
/**
42+
* Ensures required values are present or set to their defaults, when applicable
43+
*
44+
* @return boolean True when valid
45+
*/
46+
private function validateEntry()
47+
{
48+
if (empty($this->get('name'))) {
49+
return false;
50+
}
51+
if (empty($this->get('namespace'))) {
52+
$this->set('namespace', 'core');
53+
}
54+
if (empty($this->get('topic'))) {
55+
$this->set('topic', 'default');
56+
}
57+
if (empty($this->get('language'))) {
58+
$defaultLanguage = $this->xpdo->getOption('cultureKey');
59+
$language = !empty($defaultLanguage) ? $defaultLanguage : 'en' ;
60+
$this->set('language', $language);
61+
}
62+
return true;
63+
}
64+
4165
/**
4266
* Overrides xPDOObject::save to clear lexicon cache on saving.
4367
*
4468
* {@inheritdoc}
4569
*/
4670
public function save($cacheFlag = null)
4771
{
72+
if (!$this->validateEntry()) {
73+
return false;
74+
}
4875
if ($this->_new) {
4976
if (!$this->get('createdon')) {
5077
$this->set('createdon', strftime('%Y-%m-%d %H:%M:%S'));
5178
}
5279
}
53-
$saved = parent:: save($cacheFlag);
80+
$saved = parent::save($cacheFlag);
5481
if ($saved && empty($this->xpdo->config[xPDO::OPT_SETUP])) {
5582
$this->clearCache();
5683
}

manager/assets/modext/workspace/lexicon/lexicon.grid.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,17 @@ MODx.window.LexiconEntryCreate = function(config) {
405405
,itemId: 'name'
406406
,name: 'name'
407407
,anchor: '100%'
408+
,msgTarget: 'under'
409+
,allowBlank: false
408410
},{
409411
xtype: 'modx-combo-namespace'
410412
,fieldLabel: _('namespace')
411413
,name: 'namespace'
412414
,id: 'modx-'+this.ident+'-namespace'
413415
,itemId: 'namespace'
414416
,anchor: '100%'
417+
,msgTarget: 'under'
418+
,allowBlank: false
415419
,listeners: {
416420
'select': {fn: function(cb,r,i) {
417421
cle = this.fp.getComponent('topic');
@@ -429,20 +433,25 @@ MODx.window.LexiconEntryCreate = function(config) {
429433
,id: 'modx-'+this.ident+'-topic'
430434
,itemId: 'topic'
431435
,anchor: '100%'
436+
,msgTarget: 'under'
437+
,allowBlank: false
432438
},{
433439
xtype: 'modx-combo-language'
434440
,fieldLabel: _('language')
435441
,name: 'language'
436442
,id: 'modx-'+this.ident+'-language'
437443
,itemId: 'language'
438444
,anchor: '100%'
445+
,msgTarget: 'under'
446+
,allowBlank: false
439447
},{
440448
xtype: 'textarea'
441449
,fieldLabel: _('value')
442450
,id: 'modx-'+this.ident+'-value'
443451
,itemId: 'value'
444452
,name: 'value'
445453
,anchor: '100%'
454+
,msgTarget: 'under'
446455
}]
447456
});
448457
MODx.window.LexiconEntryCreate.superclass.constructor.call(this,config);

0 commit comments

Comments
 (0)