PHP code example of hryvinskyi / magento2-page-layout-manager

1. Go to this page and download the library: Download hryvinskyi/magento2-page-layout-manager library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hryvinskyi / magento2-page-layout-manager example snippets



namespace MyModule\Model\Validator;

use Hryvinskyi\PageLayoutManager\Api\RequestValidatorInterface;

class MyCustomValidator implements RequestValidatorInterface
{
    public function isRequestAllowed(
        array $parameters = [],
        ?string $defaultHandle = null,
        array $context = []
    ): bool {
        // Your validation logic here
        // Return true to allow entity-specific caching
        // Return false to block it

        // Example: Allow only specific product pages
        if ($defaultHandle === 'catalog_product_view') {
            $productId = $parameters['id'] ?? null;
            return in_array($productId, [1, 2, 3]); // Only these products
        }

        return false;
    }
}


namespace MyModule\Model\Modifier;

use Hryvinskyi\PageLayoutManager\Api\ModificationResultInterface;
use Hryvinskyi\PageLayoutManager\Api\ParameterModifierInterface;
use Hryvinskyi\PageLayoutManager\Model\ModificationResult;

class MyCustomParameterModifier implements ParameterModifierInterface
{
    public function modifyParameters(
        array $parameters,
        ?string $defaultHandle,
        array $context = []
    ): ModificationResultInterface {
        // Modify parameters when validator allows request
        $modifiedParameters = $parameters;
        $modifiedHandle = $defaultHandle;

        // Example: Add cache tags for better cache management
        if ($defaultHandle === 'catalog_product_view') {
            $modifiedParameters['cache_tags'] = ['product_cache_tag'];
            $modifiedHandle = 'catalog_product_view_optimized';
        }

        return new ModificationResult(
            $modifiedParameters,
            $modifiedHandle,
            $parameters, // original for comparison
            $defaultHandle // original for comparison
        );
    }
}