Page-specific Plugin Configuration

I tried following learn/plugin-tutorial in regards to setting up page-level configuration for a plugin, as well as using the SmartyPants Plugin for reference, but came up short.

The plugin does some preg_replace action to unwrap images from Markdown output (from <p><img></p> to <img>), and looks like the following:

unwrapimages.php:

<?php
namespace Grav\Plugin;

use Grav\Common\Data;
use Grav\Common\Plugin;
use Grav\Common\Grav;
use Grav\Common\Page\Page;
use RocketTheme\Toolbox\Event\Event;

class UnwrapImagesPlugin extends Plugin
{
    public static function getSubscribedEvents() {
        return [ 
            'onPageContentProcessed' => ['onPageContentProcessed', 0]
        ];
    }

    public function onPageContentProcessed(Event $event)
    {
        $page = $event['page'];
        $config = $this->mergeConfig($page);
        if ($config->get('process_content')) {
            $buffer = $page->content();
            $url = $page->url();
            $buffer = preg_replace("/<p>\s*?(<a .*<img.*<\/a>|<img.*)?\s*<\/p>/",
                "$1",
                $buffer);
            $buffer = preg_replace("/<img\\s+src\\s*=\\s*(["'][^"']+["']|[^>]+)>/",
                '<img src=$1 class="col-md-8 col-md-offset-2" />',
                $buffer);
            $buffer = preg_replace("/<h1>(.*?)<\/h1>/",
                '<h1 class="col-md-8 col-md-offset-2">$1</h1>',
                $buffer);
            $buffer = preg_replace("/<h2>(.*?)<\/h2>/",
                '<h2 class="col-md-8 col-md-offset-2">$1</h2>',
                $buffer);
            $buffer = preg_replace("/<h3>(.*?)<\/h3>/",
                '<h3 class="col-md-8 col-md-offset-2">$1</h3>',
                $buffer);
            $buffer = preg_replace("/<h4>(.*?)<\/h4>/",
                '<h4 class="col-md-8 col-md-offset-2">$1</h4>',
                $buffer);
            $buffer = preg_replace("/<h5>(.*?)<\/h5>/",
                '<h5 class="col-md-8 col-md-offset-2">$1</h5>',
                $buffer);
            $buffer = preg_replace("/<h6>(.*?)<\/h6>/",
                '<h6 class="col-md-8 col-md-offset-2">$1</h6>',
                $buffer);
            $buffer = preg_replace("/<p>(.*?)<\/p>/",
                '<p class="col-md-8 col-md-offset-2">$1</p>',
                $buffer);
            $page->setRawContent($buffer);
        }
    }
}

Which I need for a Bootstrap-specific layout (hence the col-md-8) with varied image sizes. The page frontmatter has:

unwrap_images:
	process_content: true

And user/config/plugins/unwrapimages.yaml: enabled: false.

In essence, I need to process the plugin on specific pages. I’m not quite sure what I am missing with the above setup, hopefully someone can spot it.

Simply

process_content: true

without nesting it under unwrap_images works?

@Gingah @flaviocopes It’s the enabled: false option. If this is set to false, then the plugin won’t even be processed by Grav. Therefore enabled: true is mandatory. Use

user/config/plugins/unwrapimages.yaml:

enabled: true
process_content: false

and in your page header

unwrap_images:
  process_content: true

You can shorten that a bit in your page header to unwrap_images: false, but then you need to check that in your plugin:

public function onPageContentProcessed(Event $event)
{ 
        $page = $event['page'];
        $config = $this->mergeConfig($page);
        if ($config->get('enabled') || $config->get('process_content')) {
            // Code
        }
}

I set the unwrapimages.php as you suggested above, and plugins/unwrapimages.yaml to:

enabled: true
process_content: true

and user/config/plugins/unwrapimages.yaml to:

enabled: true
process_content: false

and the page frontmatter to:

unwrap_images:
	process_content: true

But this now applies to all pages, even those without the page frontmatter set.

Finding the $this->grav['debugger']->addMessage('message'); made debugging this massively easier. Some changes to unwrapimages.php, in particular how plugin and page configurations are checked, and it works:

<?php
namespace Grav\Plugin;

use Grav\Common\Data;
use Grav\Common\Plugin;
use Grav\Common\Grav;
use Grav\Common\Page\Page;
use RocketTheme\Toolbox\Event\Event;

class UnwrapImagesPlugin extends Plugin
{
    public static function getSubscribedEvents() {
        return [
            'onPageContentProcessed' => ['onPageContentProcessed', 0]
        ];
    }
 
    public function onPageContentProcessed(Event $event)
    {
        $page = $event['page'];
        $pluginsobject = (array) $this->config->get('plugins');
        $pageobject = $this->grav['page'];
        if (isset($pluginsobject['unwrapimages'])) {
            if ($pluginsobject['unwrapimages']['enabled'] && isset($pageobject->header()->unwrap_images['process_content']))  {
                $buffer = $page->content();
                $url = $page->url();
                $buffer = preg_replace("/<p>\s*?(<a .*<img.*<\/a>|<img.*)?\s*<\/p>/",
                    "$1",
                    $buffer);
                $buffer = preg_replace("/<img\\s+src\\s*=\\s*(["'][^"']+["']|[^>]+)>/",
                    '<img src=$1 class="col-md-8 col-md-offset-2" />',
                    $buffer);
                $buffer = preg_replace("/<h1>(.*?)<\/h1>/",
                    '<h1 class="col-md-8 col-md-offset-2">$1</h1>',
                    $buffer);
                $buffer = preg_replace("/<h2>(.*?)<\/h2>/",
                    '<h2 class="col-md-8 col-md-offset-2">$1</h2>',
                    $buffer);
                $buffer = preg_replace("/<h3>(.*?)<\/h3>/",
                    '<h3 class="col-md-8 col-md-offset-2">$1</h3>',
                    $buffer);
                $buffer = preg_replace("/<h4>(.*?)<\/h4>/",
                    '<h4 class="col-md-8 col-md-offset-2">$1</h4>',
                    $buffer);
                $buffer = preg_replace("/<h5>(.*?)<\/h5>/",
                    '<h5 class="col-md-8 col-md-offset-2">$1</h5>',
                    $buffer);
                $buffer = preg_replace("/<h6>(.*?)<\/h6>/",
                    '<h6 class="col-md-8 col-md-offset-2">$1</h6>',
                    $buffer);
                $buffer = preg_replace("/<p>(.*?)<\/p>/",
                    '<p class="col-md-8 col-md-offset-2">$1</p>',
                    $buffer);
                $page->setRawContent($buffer);
            }
        }
    }
}

also there’s a handy dump($var); function you can use in any PHP file.