Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comment system #3472

Closed
gwbonline opened this issue May 2, 2015 · 30 comments
Closed

Comment system #3472

gwbonline opened this issue May 2, 2015 · 30 comments

Comments

@gwbonline
Copy link

I miss the build in comment system of PivotX. Why is this not a part of Bolt anymore? Not everyone likes external services like Disqus. I think this service has an over complete form and I just need a simple comment system that I can host myself. Users don't have to log in, they only have to provide their name, email address and a comment.

@GwendolenLynch
Copy link
Contributor

I have seen a couple pop up over time on the extensions marketplace.

That said… I've never tried this, but BoltForms can save to a contenttype, and you can specify hidden fields… So technically you could implement it already with Bolt + BoltForms.

Just food for thought/conversation.

@gwbonline
Copy link
Author

I will try that. Why do I have to use hidden fields?

@GwendolenLynch
Copy link
Contributor

I must have been attempting to imitate Captain Obtuse 😀

The thought was if you had some meta, such as the record number and the contenttype of the page you were on, you could use those. Then on loading of a page, you can just do a {{ getcontent() }} for the comment contenttypes that are specific to that page. As I said, haven't tried it, but it should work and you at least get reCAPTCHA built-in for SPAM reduction.

@gwbonline
Copy link
Author

I need some more info about getcontent in general and using it to display content for a specific entry. It is not in the documentation and I haven't tried something like this before.

And when I use the following code:

#  database:
#    contenttype: contentcomment
#    table: bolt_content_comment

Are all fields that I use in the comment form stored in the database for that specific entry?

@CarsonF
Copy link
Member

CarsonF commented May 3, 2015

@gwbonline
Copy link
Author

I've read the info about setting content before. Now I'm confused. GawainLynch mentioned the 'getcontent' tag (instead of the 'set content' tag) and about that there is no information in the documentation.

@GwendolenLynch
Copy link
Contributor

Sorry, my bad… you want {% setcontent % } in Twig… that uses Storage::getContent() in PHP and I got the two confused… not enough (any) 🍻 today!

@gwbonline
Copy link
Author

Thanks! I know I can use 'setcontent' to display specific content from records in a specific content type. Is that why I have to use the entry id as a hidden field in the comment form to display comments for only the entry with that id later on? The Bolt Forms extension contains database settings. When I uncomment these, is all the info of the comment form then stored in the given contenttype?

@GwendolenLynch
Copy link
Contributor

I know I can use 'setcontent' to display specific content from records in a specific content type. Is that why I have to use the entry id as a hidden field in the comment form to display comments for only the entry with that id later on?

Possibly. Was thinking aloud, mostly, but that seems to be something I think you'll need.

The Bolt Forms extension contains database settings. When I uncomment these, is all the info of the comment form then stored in the given contenttype?

Each form will have a section like this:

#  database:
#    contenttype: mycontenttype  # Contenttype to create
#    table: bolt_secret_table    # Specific database table to write to

You would only need to do this:

database:
  contenttype: mycontenttype

…and then make sure mycontenttype has at fields with names that match the form ones that you want saved. Job done.

@gwbonline
Copy link
Author

And that contenttype with fields have to be in the main config file of Bolt also?

@GwendolenLynch
Copy link
Contributor

Yep, in app/config/contenttypes.yml you could add something like this:

comments:
    name: Comments
    singular_name: Comment
    fields:
        title:
            type: text
        slug:
            type: slug
            uses: title
        record_type:
            type: text
        record_id:
            type: text
        author:
            type: text
        comment:
            type: textarea
    default_status: published
    searchable: false
    viewless: true

NOTE: Above is untested and straight off the top of my head 😁

@gwbonline
Copy link
Author

I think I'm almost there... The extension config file doesn't accept {{ record.id }} (unknowm characters). Is there a way to bypass that? I need this to add the record id as a field to the form (and save the id to the comment contenttype). I use 'where' to display only records that have a specific id.

In the extension config file this is what I have for that part:

 record_id:
        type: text
        value: {{ record.id }}

@GwendolenLynch
Copy link
Contributor

We might be on different pages here… but the example I gave was for app/config/contenttypes.yml to create the Contenttype called "Comments"… is that what you're referring to?

@gwbonline
Copy link
Author

Yes, I know. But to add the record id to 'record_id' (comments contenttype) after the comment is send, I need to add the record id to the form first. Just like how the comment text itself is saved to the database (via a field in both config files). Or am I thinking wrong?

@GwendolenLynch
Copy link
Contributor

Ah, cool. OK, well first thing to know is that YAML doesn't understand Twig, i.e. {{ }} and then YAML is read in early, and not processed in that way.

What you need to do a custom boltforms_form.twig, see this section… you can create an exception and set defaults on specific files like so:

{{ form_widget(form.record_id, { 'value' : record.id }) }}

@gwbonline
Copy link
Author

Or add a input field with a pre-set value (record id) manually? Sorry... I understand what the code you provided does, but it goes beyond my knowledge to implement it.

I also discovered something else. In the source code, an input field has a name like 'comments[name]' or 'comments[message]'. In this code 'comments' is the name of the form and '[...]' is the name of the field. The names in the config file for the contenttype are different. Should these also be 'comments[...]' instead of only the field name (like they are defined in the extension config file?

Have I already said that I'm really thankful for your help? Thanks!

@GwendolenLynch
Copy link
Contributor

OK, I shall have a go here at mocking up an example based on the current boltforms_form.twig file:

The relevant bit in app/config/boltforms.bolt/yml:

  fields:
    record_type:
      type: hidden
    record_id:
      type: hidden
{% form_theme form 'boltforms_custom.twig' %}

{% block boltforms_css %}
<link href="{{ app.paths.extensions }}vendor/bolt/boltforms/css/boltforms.css" rel="stylesheet" type="text/css" />
{% endblock boltforms_css %}

<div class="boltform">
{% if error and app.request.get(formname) %}
    <p class="boltform-error">{{ error }}</p>
{% endif %}

{% if message and app.request.get(formname) %}
    <p class="boltform-message">{{ message }}</p>
{% endif %}

{% if not sent %}

    {{ html_pre }}

    {% if recaptcha.theme|default('clean') %}
    <script>
        var RecaptchaOptions = {
            theme : '{{ recaptcha.theme }}'
        };
    </script>
    {% endif %}

    {{ form_start(form, {'attr': {'name': formname}}) }}
        {{ form_errors(form) }}

        {% for key, value in fields  %}
            {% if value.config.name != 'submit' and value.config.name != 'record_type'  and value.config.name != 'record_id' %}
            <div class="boltforms-row{% if form[key].vars.attr.class is defined %} boltforms-{{ form[key].vars.attr.class }}-row{% endif %}">
                {{ form_label(form[key]) }}
                {{ form_errors(form[key]) }}
                {{ form_widget(form[key]) }}
            </div>
            {% elseif value.config.name == 'record_type' %}
                {{ form_widget(form.record_type, { 'value' : record.contenttype }) }}
            {% elseif value.config.name == 'record_id' %}
                {{ form_widget(form.record_id, { 'value' : record.id }) }}
            {% endif %}
        {% endfor %}

        {% if recaptcha.enabled %}

            {% if not recaptcha.valid %}
            <ul class="boltform-error">
                <li class="boltform-errors">{{ recaptcha.error_message }}</li>
            </ul>
            {% endif %}

            <div class="boltform-row">
                <label for="form_message" class="required">{{ recaptcha.label }}</label>
                <script src="https://www.google.com/recaptcha/api.js?hl={{ htmllang() }}" async defer></script>
                <div class="g-recaptcha" data-sitekey="{{ recaptcha.public_key }}"></div>
            </div>

        {% endif %}

        <br>

        <div class="boltforms-row{% if form.submit.vars.attr.class is defined %} boltforms-{{ form.submit.vars.attr.class }}-row{% endif %}">
            {{ form_row(form.submit) }}
        </div>

    {{ form_end(form) }}

{% else %}
    {{ html_post }}
{% endif %}
</div>

DISCLAIMER: again done mostly off the top of my head 😁

@gwbonline
Copy link
Author

Thanks again! Is the contenttype as a hidden field relevant? With a template tag I can make the form visible on only entry pages. And I can sort based on the record id.

I will try to make it work and let you know when it's implemented. I'm really thankful for your help and won't bother you again with this. I'm just not such a code expert as you guys. Everyday it's getting better.

@GwendolenLynch
Copy link
Contributor

Thanks again!

Ah, zero stress, you're very welcome!

Is the contenttype as a hidden field relevant?

That's the extension config this time, it just means that they won't show on the actual form as displayed to the user. The matching fields in the Contenttype are just text fields.

I will try to make it work and let you know when it's implemented. I'm really thankful for your help and won't bother you again with this. I'm just not such a code expert as you guys. Everyday it's getting better.

Cool, it's always great to see what people come up with from your work… I am sure @bobdenotter gets a kick out of it every day, just seeing how Bolt has grown and is being used 👍

@gwbonline
Copy link
Author

That's the extension config this time, it just means that they won't show on the actual form as displayed to the user. The matching fields in the Contenttype are just text fields.

That is correct. I know that a hidden field won't show on the actual form. I was only wondering why the contenttype hidden input field is relevant for the comment form to function when I can publish comments and sort with the comment id. Btw: it returns 'Array'...

It is implemented and when I publish a comment, it is stored in the database. So that is looking good! Does Bolt assign an independent id to it that has nothing to do with the id that is set with the form and that is used to sort comments?

@GwendolenLynch
Copy link
Contributor

The idea of saving the comments record with the page record contenttype and ID is for doing look ups, that way you can in the template get the record Conenttype ("pages" in this example) and the record's ID (42 in this example) and get:

{% setcontent comments = "pages/42" %}
{% for comment in comments %}
   {# Display comment data here #}
{% endfor %}

@gwbonline
Copy link
Author

I understand... I only don't see the use of it, because the contenttype is given in the extension config file (database settings) and also in Bolts config file (as the actual contenttype). When the hidden field for the contenttype isn't present, the comment is still stored in the database and I can use setcontent to display entries from the comments contenttype.

@GwendolenLynch
Copy link
Contributor

I only don't see the use of it, because the contenttype is given in the extension config file (database settings) and also in Bolts config file (as the actual contenttype).

What you are trying to store in the comment record is the Contenttype and ID of the site page it relates to, or in another way of putting it, the record it is "commenting on".

@gwbonline
Copy link
Author

I'm sorry... I'm confused now. I know the basic principals of 'setcontent' and 'contenttype'. But the contenttype tag puts out 'array' instead of the actual contenttype. That aside, I'm thinking this way:

  • comments are stored in the database under contenttype 'comments'
  • when I want to show the comments for a specific entry/record, I can use setcontent to display records from the comments contenttype

In the example you gave me record 42 from 'pages' is set. And then comes some code to display comments. But contenttype 'pages' doesn't contain fields like contenttype 'comments' does. So how can I display comments when the comments contenttype isn't fetched?

When I remove the hidden contenttype field and someone leaves a comment, the comment is stored in the database with only the id of the record the comment is for.

My thought was to add the following code to the record template:

{% setcontent comments = 'comments' where { record_id: '{{ record.id }}' } %}
{% for comment in comments %}
{{ comment.name }} {{ comment.body }}
{% endfor %}

I haven't tested it yet. I think this code only sets content from the contenttype 'comments' that have the record_id from the record they are on.

@gwbonline
Copy link
Author

It looks like this also doesn't work. When I add the record id manually, it does. Is is not possible to do this below?

{% setcontent comments = 'comments' where { record_id: '{{ record.id }}' } %}

@GwendolenLynch
Copy link
Contributor

What about:

{% setcontent comments = 'comments' where { record_id: record.id } %}

@gwbonline
Copy link
Author

Nope... that doesn't set the id of the page the comments are on. Just to clear things up: I hope you didn't get the feeling that I was pedantic with my last post yesterday. I know much less then you about this and I admire the fact that you can produce such code like writing a text. I just try to understand the things I'm doing.

UPDATE: It does work! A typo messed things up. Now I have a comment form that stores comments in the database and these comments are displayed below the record with a corresponding record id.

@GwendolenLynch
Copy link
Contributor

Just to clear things up: I hope you didn't get the feeling that I was pedantic with my last post yesterday

Hehe… No, your input is never taken badly by me. My work on Bolt is for fun, and as such I do the best I can to not let any of it stress me… and keep it fun 😁

With respect to what we're trying to do here, if there is nothing coming back in that {{ setcontent }} call, then there is either a bug in Bolt, or developer derp-a-derp… or as we say in on IRC all the time, "blame the koala!"

OK, so just stepping though the obvious places that Gawain would make mistakes — believe me, it's a well developed skill of mine — the places/things to look/check are:

  • Are the Comments records published (this one gets me about once a week!!!)
  • Are there any records with the Contenttype Comments
  • Did you make a typo in the the Contenttype field name
  • Do the existing records when viewed in the backend editor have the record_id field and does it have values that match the record you're trying to do {{ setcontent }} from

@gwbonline
Copy link
Author

It was a typo... I did record_id and it should be record.id... Now I have a comment form that stores comments in the database and these comments are displayed below the record with a corresponding record id.

@GwendolenLynch
Copy link
Contributor

Closing in "clean up mode"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants