phpcms表单formguide模块如何添加万能字段

使用过phpcms万能字段的朋友应该有所体会,万能字段的功能非常强大,能够把同模型字段或者其他模型通过万能字段的方式进行有机组合,从而能够实现大部分很复杂的数据增改场景

但是这个字段类型只保留在了content模块,如果需要开发留言功能,我们发现formguide模块里面是没有这个字段的,那么我们应该怎么参考content模块,然后扩展万能字段呢,网上找了些教程,只有“文件上传字段”的
扩展 方法,没有提供“万能字段”的扩展方法,所以在博客里做了笔记,感兴趣的朋友可以继续往下面看

如何在formguide表单模块添加万能字段

1.打开目录\phpcms\modules\content\fields\,把文件夹“omnipotent”拷贝到目录\phpcms\modules\formguide\fields里面

2. 打开文件\phpcms\modules\formguide\fields\fields.inc.php,在数组$fields添加值

'omnipotent'=>'万能字段',

3.打开\phpcms\modules\formguide\templates里面的“formguide_field_add.tpl.php”,添加如下内容,表示其他字段可以设置为万能字段里的子字段

<tr>
  <th><strong>作为万能字段的附属字段</strong><br>必须与万能字段结合起来使用,否则内容添加的时候不会正常显示</th>
  <td><input type="radio" name="info[isomnipotent]" value="1" /> <?php echo L('yes');?> <input type="radio" name="info[isomnipotent]" value="0" checked/> <?php echo L('no');?></td>
</tr>

打开 “formguide_field_edit.tpl.php” ,添加如下内容

<tr> 
  <th><strong>作为万能字段的附属字段</strong><br>必须与万能字段结合起来使用,否则内容添加的时候不会正常显示</th>
  <td><input type="radio" name="info[isomnipotent]" value="1" <?php if($isomnipotent) echo 'checked';?> <?php if(in_array($field,$forbid_fields)) echo 'disabled'; ?>/> <?php echo L('yes');?> <input type="radio" name="info[isomnipotent]" value="0" <?php if(!$isomnipotent) echo 'checked';?> /> <?php echo L('no');?></td>
</tr>

4.至此,表单里面可以正常设置万能字段了,但是前端显示还有个问题,默认的前端显示模板\phpcms\templates\default\formguide\show.html,有个地方写错了,导致前端万能字段不能正常显示,如下

{loop}
{if $info['formtype']=='omnipotent'}
  {loop $forminfos_data $_fm $_fm_value}
  {if $_fm_value['iscomnipotent']}
  {php $info['form'] = str_replace('{'.$_fm.'}',$_fm_value['form'],$info['form']);}
{/if}
  {/loop}
{/if}
<tr>
  <th width="80">{if $info['star']} <font color="red">*</font>{/if} {$info['name']}</th>
  <td>{$info['form']} {$info['tips']}</td>
</tr>
{/loop}

“iscomnipotent”多了个字母c,并且少了个判断,将其改为下面的内容就可以了,同理,如果需要js方式调用表单,记得同时改下show_js.html的相同地方

{loop $forminfos_data $field $info}
  {if $info['formtype']=='omnipotent'}
  {loop $forminfos_data $_fm $_fm_value}
  {if $_fm_value['isomnipotent']}
  {php $info['form'] = str_replace('{'.$_fm.'}',$_fm_value['form'],$info['form']);}
{/if}
{/loop}
  {/if}
  {if !$info['isomnipotent']}
  <tr>
  <th width="160">{if $info['star']} <font color="red">*</font>{/if} {$info['name']}</th>
  <td>{$info['form']} {$info['tips']}</td>
  </tr>
  {/if}
{/loop}

最终效果如下

相关推荐