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

Would you consider adding a couple more filters? #19

Open
landwire opened this issue Jan 29, 2020 · 4 comments
Open

Would you consider adding a couple more filters? #19

landwire opened this issue Jan 29, 2020 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@landwire
Copy link

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
Copy link
Owner

palmiak commented Jan 29, 2020

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).

@palmiak palmiak self-assigned this Jan 29, 2020
@palmiak palmiak added the enhancement New feature or request label Jan 29, 2020
@landwire
Copy link
Author

landwire commented Jan 29, 2020

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
Copy link
Author

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
Copy link
Author

landwire commented Nov 1, 2020

Hi Maciej,
not sure if you saw already, but I replied to your question in the MR (#28) a while back. If you want me to change/adapt anything let me know.

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

No branches or pull requests

2 participants