字段名称 province-en 还是 province_en 对后端操作没有太大的影响,但是把数据传给前端,在操作时就有一个别扭的事情。
前端获取字段值时,含中划线的字段名会提示错误,中划线识别为减号了。
因此joomla5.1中 修改允许包含下划线的字段名就要解决。那在joomla 如何找到这个问题的原因呢?
新增/编辑一个扩展字段,form提交路径:
http://bwg.home.com:8080/administrator/index.php?option=com_fields&context=com_content.article&layout=edit&id=0
根据路径,找到保存提交值的方法save()。
位置如下:
(1)administrator/components/com_fields/src/Model/FieldModel.php
public function save(
d
a
t
a
)
i
f
(
!
p
a
r
e
n
t
:
:
s
a
v
e
(
data) if (!parent::save(
data)if(!parent::save(data)) {
(2)libraries/src/MVC/Model/AdminModel.php
public function save(
d
a
t
a
)
i
f
(
!
data) if (!
data)if(!table->check()) {
(3)adminstrator/components/com_fields/src/Table/FieldTable.php
public function check()
t
h
i
s
−
>
n
a
m
e
=
A
p
p
l
i
c
a
t
i
o
n
H
e
l
p
e
r
:
:
s
t
r
i
n
g
U
R
L
S
a
f
e
(
this->name = ApplicationHelper::stringURLSafe(
this−>name=ApplicationHelper::stringURLSafe(this->name, $this->language);
(4)libraries/src/Application/ApplicationHelper.php
public static function stringURLSafe($string, $language = ‘’)
o
u
t
p
u
t
=
O
u
t
p
u
t
F
i
l
t
e
r
:
:
s
t
r
i
n
g
U
R
L
S
a
f
e
(
output = OutputFilter::stringURLSafe(
output=OutputFilter::stringURLSafe(string, $language);
(5)libraries/src/Filter/OutputFilter.php
public static function stringURLSafe($string, $language = ‘’)
// Remove any duplicate whitespace, and ensure all characters are alphanumeric
$str = preg_replace(‘/(\s|[^A-Za-z0-9-_])+/’, ‘-’, $str);
//add _ 允许包含下划线
根据数据处理流程,一步步找到了
$str = preg_replace('/(\s|[^A-Za-z0-9\-])+/', '-', $str);
这个正则表达式。
这个正则的意思就是,把str中除大小写字母/数值/中划线保留,其他都转化为中划线。
把这个修改为
$str = preg_replace('/(\s|[^A-Za-z0-9\-\_])+/', '-', $str);
至此就解决了下划线问题。