palmiak / timber-acf-wp-blocks

Create Gutenberg blocks from Twig templates and ACF fields.
https://palmiak.github.io/timber-acf-wp-blocks/
MIT License
242 stars 24 forks source link

Would you consider adding a couple more filters? #19

Open landwire opened 4 years ago

landwire commented 4 years ago

Hi Maciej, because I would like to/am doing all my data processing in an example.php file before the corresponding example.twig file is called, it would be great if you could add a couple of filters. One I use to filter out the directories, as I do not want so many directories passed around, if my structure is always like below. I have a lot of blocks and I prefer to just have the paths that are really needed. The other one is to include the .php file where I can do all my data processing and then return the updated $context.

componentA
  -- componentA.php
  -- componentA.twig

componentB
  -- componentB.php
  -- componentB.twig
$paths = timber_acf_path_render($slug);

$context = apply_filters( 'timber/acf-gutenberg-blocks-data', $slug, $paths, $context);

Timber::render($paths, $context);
function timber_acf_path_render($slug) {
        $directories = timber_block_directory_getter();

    $directories = apply_filters( 'timber/acf-gutenberg-blocks-directories', $directories, $slug);

    $ret = [];
    foreach ($directories as $directory) {
        $ret[] = $directory . "/{$slug}.twig";
    }

    return $ret;
}
palmiak commented 4 years ago

I think the first can be solved just by using timber/acf-gutenberg-blocks-templates - you just pass what you need. Am I right?

The second one - I think I would just have to add timber/acf-gutenberg-blocks-data/ but for all blocks (now it always need a block_id / slug).

landwire commented 4 years ago

Hi Maciej, unfortunately not. I would like to filter out the subdirectories based on the $slug. I can only do it a that time. Maybe for a better understanding what I am doing I post my code here. Maybe there is a better solution to this. But for me it is crucial to be able to prepare/preprocess data in PHP before it gets passed to the twig file to keep the twig template as lean and clean as possible. That pre-processing should happen in a file directly related to the template and not somewhere else in a functions.php or similar.

function include_data_processor($slug, $paths, $context) {
    $directory = str_replace($slug . '.twig', '', $paths[0]);

    if (file_exists(locate_template($directory . $slug . '.php'))) {
        $file = locate_template($directory . $slug . '.php');
        $context = include_once($file);
    }

    return $context;
}
add_filter('timber/acf-gutenberg-blocks-data', 'include_data_processor', 10, 3);

function filter_directories($directories, $slug) {
    // filter out other directories
    $directories = array_filter($directories, function ($value) use ($slug) {
        if (strpos(strtolower($value), $slug) === FALSE) {
            return FALSE;
        }
        else {
            return TRUE;
        }
    });

    return $directories;
}
add_filter('timber/acf-gutenberg-blocks-directories', 'filter_directories', 10, 2);

If you have a better idea then let me know. The filtering out of sub-directories is not crucial, but I will have a lot of blocks all in their own directory and it will pass all those directories into the $paths. That is not necessary in my use case.

landwire commented 4 years ago

Hi Maciej, I created a merge request based on the master branch. Maybe you could consider adding those 2 filters and then close this issue? Thanks.

landwire commented 3 years ago

Hi Maciej, not sure if you saw already, but I replied to your question in the MR (https://github.com/palmiak/timber-acf-wp-blocks/pull/28) a while back. If you want me to change/adapt anything let me know.