[教程] typecho实现文章增加表单

查看: 871|回复: 0
crll 发表于 2023-4-6 15:42:45
教程开始

1.前期准备,想好自己要添加怎样的字段和表单类型,比如我想添加一个select表单,额外显示文章的状态,字段名称state,字段类型为int。
那么,打开phpmyadmin,首先在数据中新建名为state的字段,类型为int,允许为空,默认值为0

然后,准备好html代码,按照typecho原有的格式,
  1. <section class="typecho-post-option">
  2.     <label for="token-input-tags" class="typecho-label"><?php _e('审核状态'); ?></label>
  3.     <p>
  4.     <select id="state" name="state">
  5.         <option value="0" >审核中</option>
  6.         <option value="1" >不通过</option>
  7.         <option value="2" >审核通过?></option>
  8.         <option value="3" >已结算</option>
  9.     </select>
  10.     </p>
  11. </section>
复制代码


2.接下来,就要开始修改Widget文件。
首先是要让表单的提交接收一个新的参数,具体的文件在/var/Widget/Contents/Post/Edit.php
找到大概719行的位置,将如下代码:
  1. $contents = $this->request->from('password', 'allowComment',
  2.             'allowPing', 'allowFeed', 'slug', 'tags', 'text', 'visibility');
复制代码


修改为
  1. $contents = $this->request->from('password', 'allowComment',
  2.             'allowPing', 'allowFeed', 'slug', 'tags', 'text', 'visibility', 'state');
复制代码


控制文章增删改查的文件是/var/Widget/Abstract/Contents.php
大概257行,将原本的代码
  1. return $this->db->select('table.contents.cid', 'table.contents.title', 'table.contents.slug', 'table.contents.created', 'table.contents.authorId',
  2.         'table.contents.modified', 'table.contents.type', 'table.contents.status', 'table.contents.text', 'table.contents.commentsNum', 'table.contents.order',
  3.         'table.contents.template', 'table.contents.password', 'table.contents.allowComment', 'table.contents.allowPing', 'table.contents.allowFeed',
  4.         'table.contents.parent')->from('table.contents');
复制代码


修改为
  1. return $this->db->select('table.contents.cid', 'table.contents.title', 'table.contents.slug', 'table.contents.created', 'table.contents.authorId',
  2.         'table.contents.modified', 'table.contents.type', 'table.contents.status', 'table.contents.text', 'table.contents.commentsNum', 'table.contents.order',
  3.         'table.contents.template', 'table.contents.password', 'table.contents.allowComment', 'table.contents.allowPing', 'table.contents.allowFeed',
  4.         'table.contents.parent','table.contents.state')->from('table.contents');
复制代码


大概288行,插入内容字段定义的地方,增加如下代码(记得给前面的加上逗号,防止代码报错)
  1. 'state'        =>  empty($content['state']) ? 0 : intval($content['state'])
复制代码


大概333行,更新内容字段定义的地方,增加如下代码
  1. 'state'        =>  empty($content['state']) ? 0 : intval($content['state'])
复制代码


3.完成字段定义后,就可以将html放进文章的发布文件了。

不过在这之前要修改一下,比如改为如下:
  1. <section class="typecho-post-option">
  2.     <label for="token-input-tags" class="typecho-label"><?php _e('审核状态'); ?></label>
  3.     <p>
  4.     <select id="state" name="state">
  5.         <option value="0" <?php if ($post->state == 0): ?> selected<?php endif; ?>><?php _e('审核中'); ?></option>
  6.         <option value="1" <?php if ($post->state == 1): ?> selected<?php endif; ?>><?php _e('不通过'); ?></option>
  7.         <option value="2" <?php if ($post->state == 2): ?> selected<?php endif; ?>><?php _e('审核通过'); ?></option>
  8.         <option value="3" <?php if ($post->state == 3): ?> selected<?php endif; ?>><?php _e('已结算'); ?></option>
  9.     </select>
  10.     </p>
  11. </section>
复制代码

因为我要的是select表单,所以要根据$post->state的值来判断是否选择,那么其实如果要在其它地方将值调用出来,或者就是子级要用其它表单来调用值,用以下代码就好了
  1. <?php $post->state(); ?>
复制代码

保存之后,测试发布以下,确认没有报错,并且可以调出选中的select,就代表增加成了。

这个实现在二次开发中有很大的好处,比如让文章拥有更多类型,新增更多定义,让扩展性更强。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
博主论坛 bzlt.net
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于本站联系我们FAQ友情链接免责声明生存法则

Build with for "make" Copyright © 2020-2022. Powered by Discuz! GMT+8, 2024-4-20 07:05

快速回复 返回顶部 返回列表