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

Active comments per page in the frontmatter #93

Open
NicoHood opened this issue Oct 1, 2020 · 1 comment
Open

Active comments per page in the frontmatter #93

NicoHood opened this issue Oct 1, 2020 · 1 comment

Comments

@NicoHood
Copy link

NicoHood commented Oct 1, 2020

I am trying to only active the comments function when to active flag in the frontmatter is set. This is not yet implemented and I think this might not be possible at all. It would be nice, if you could give me some hints:

  1. onPluginsInitialized Event will check if the route is set in the config and enables the plugin
  2. onFormPageHeaderProcessed Adds the form to the page header
  3. onPageInitialized Gives us the page object along with its header settings

The problem is, that I must check if the plugin should be enabled in step 3. But I cannot add the form from step 1 at this late step. This means I can only enable/disable the comments plugin from uris?

Enabling the plugin on a per-page basis has multiple usecases:

  • You dont need to edit the plugin config every time, just the page
  • You do not need to use uris, which might change
  • You can also enable comments for a special page template, e.g. only for blog posts, no matter at which route they are
  • This plugin will only be active, where is should. currently you can send a POST to a page without comments form and a comment will be silently added to the data directory

Is there a way to add a form to the page header at a later time?

@NicoHood
Copy link
Author

NicoHood commented Oct 1, 2020

I've found a solution that works inside the onPageInitialized event:

public function onPageInitialized(Event $event)
{
    // Check if the plugin should be enabled.
    // We need to check this in the page initialized event
    // in order to access the page template property.
    $this->calculateEnable();

    // Enable the main events we are interested in
    if ($this->enable) {
        // NOTE: We must add the form here and not in the onFormPageHeaderProcessed event.
        // The mentioned event will run before onPageInitialized, but we can only validate
        // the page template filters after the page got initialized. Thatswhy the form will be
        // added at this later stage.
        $page->addForms([$this->grav['config']->get('plugins.comments.form')]);

        $this->enable([
            'onFormValidationProcessed' => ['onFormValidationProcessed', 0],
            'onFormProcessed' => ['onFormProcessed', 0],
            'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
        ]);
    }
}

If this functionality is also desired for the comments plugin I am happy to create a PR.

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

1 participant