mortscode/feedback

一个用于用户评论和提问的Craft插件

安装: 669

依赖项: 0

建议者: 0

安全: 0

星星: 0

观察者: 2

分支: 0

开放问题: 0

类型:craft-plugin

4.0.24 2024-08-18 14:45 UTC

README

为Craft CMS 3.x提供的评论和评价插件

Screenshot

要求

此插件需要Craft CMS 3.0.0-beta.23或更高版本。

安装

要安装此插件,请按照以下说明操作。

  1. 打开您的终端并转到您的Craft项目

     cd /path/to/project
    
  2. 然后告诉Composer加载插件

     composer require mortscode/feedback
    
  3. 在控制面板中,转到设置→插件,并为“反馈”点击“安装”按钮。

反馈概述

反馈插件为您的用户提供两种类型的反馈表单:评价和问题。其理念是,有时您需要用户的实际评价,而其他时候,您只想提供一个让用户提问或提供建议的方式。

下面将提供两个针对这两种类型用户的表单。

评价表单

必填字段

  • 姓名
  • 电子邮件

评价特定字段

  • 评分
<form class="js-feedback-form" role="form" method="post" accept-charset="UTF-8">
     {{ hiddenInput('action', 'feedback/feedback/save') }}
     {{ hiddenInput('entryId', entry.id) }}
     {{ hiddenInput('feedbackType', 'review') }}
     {{ hiddenInput('token', "", {class: 'js-token-input'}) }} <-- if using recaptcha
     {{ csrfInput() }}
     <input type="text" name="name" placeholder="name">
     {{ feedback is defined and feedback ? errorList(feedback.getErrors('name')) }}
     <input type="email" name="email" placeholder="email">
     <div>
         <input type="radio" id="rating-1" name="rating" value="1"><label for="rating-1">1</label>
         <input type="radio" id="rating-2" name="rating" value="2"><label for="rating-2">2</label>
         <input type="radio" id="rating-3" name="rating" value="3"><label for="rating-3">3</label>
         <input type="radio" id="rating-4" name="rating" value="4"><label for="rating-4">4</label>
         <input type="radio" id="rating-5" name="rating" value="5"><label for="rating-5">5</label>
     </div>
     <textarea name="comment" cols="30" rows="10"></textarea>
     <button type="submit">Submit Review</button>
 </form>

问题表单

添加以下表单,以启用用户提问(无评分)

<form class="js-feedback-form" role="form" method="post" accept-charset="UTF-8">
     {{ hiddenInput('action', 'feedback/feedback/save') }}
     {{ hiddenInput('entryId', entry.id) }}
     {{ hiddenInput('feedbackType', 'question') }}
     {{ hiddenInput('token', "", {class: 'js-token-input'}) }} <-- if using recaptcha
     {{ csrfInput() }}
     <input type="text" name="name" placeholder="name">
     <input type="email" name="email" placeholder="email">
     <textarea name="comment" cols="30" rows="10"></textarea>
     <button type="submit">Submit Question</button>
 </form>

配置反馈

-在此处插入文本-

使用反馈

可用变量

  • name
  • email
  • rating
  • comment
  • response

模板

在模板中按如下方式呈现评价

{% set reviews = craft.reviews.getEntryReviews(entry.id) %}
<ol>
    {% for review in reviews %}
        <li>
            <h3>{{ review.name }}</h3>
            <p>{{ review.email }}</p>
            <p>Rating:
                {{ review.rating }}</p>
            <p>{{ review.comment }}</p>
            {% if review.response %}
                <p>{{ review.response }}</p>
            {% endif %}
        </li>
    {% endfor %}
</ol>

ReCaptcha

  1. 此处注册您的网站以使用Google ReCaptcha。
  2. 将您的新站点密钥和密钥添加到Craft CP中的“设置 > 评价”页面
  3. 使用评价插件将JS脚本添加到页面。您可以从设置页面使用craft.reviews.getRecaptchaKey助手获取密钥。
<script src="https://www.google.com/recaptcha/api.js?render={{ craft.reviews.getRecaptchaKey }}"></script>
  1. 在提交时,从ReCaptcha获取令牌,并在提交之前将其传递给表单。
<script>
    var reviewForm = document.getElementById('reviews-form');

    reviewForm.addEventListener('submit', handleRecaptcha);

    function handleRecaptcha(e) {
        e.preventDefault();

        grecaptcha.ready(function() {
            grecaptcha.execute('{{ craft.reviews.getRecaptchaKey }}', {action: 'submit'}).then(function(token) {
                tokenizeForm(token);
            }).then(function() {
                reviewForm.submit();
            });
        });

        function tokenizeForm(token) {
            const tokenInput = document.querySelector('.js-token-input');
            console.log(tokenInput);
            tokenInput.value = token;
        }
    }

    reviewForm.addEventListener('submit', handleRecaptcha, false);
</script>
  1. 将隐藏字段添加到您的表单中
<form>
   [...]
   {{ hiddenInput('token', "", {class: 'js-token-input'}) }}
   [...]
</form>

反馈路线图

一些待办事项和潜在功能的想法

  • 发布

Scot Mortimer提供