From 769a7ef1925b73cc6d78cbd26e5f39376af160f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fa=20G=C3=BCrdal?= Date: Mon, 16 Jul 2018 15:35:14 +0200 Subject: [PATCH] feat(Features/TimberLoader): add template file name filter (#279) * feat(Features/TimberLoader): add template file name filter * refactor(Features/TimberLoader): use namespaced functions for filter calls instead of anonymous ones * style(Features/TimberLoader): fix linting errors --- Features/TimberLoader/functions.php | 38 ++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/Features/TimberLoader/functions.php b/Features/TimberLoader/functions.php index 65673aa1..83693fb9 100644 --- a/Features/TimberLoader/functions.php +++ b/Features/TimberLoader/functions.php @@ -11,10 +11,24 @@ define(__NAMESPACE__ . '\NS', __NAMESPACE__ . '\\'); // Render Component with Timber (Twig) -add_filter('Flynt/renderComponent', function ($output, $componentName, $componentData, $areaHtml) { +add_filter('Flynt/renderComponent', NS . 'renderTwigIndex', 10, 4); + +// Convert ACF Images to Timber Images +add_filter('acf/format_value/type=image', NS . 'formatImage', 100); + +// Convert ACF Gallery Images to Timber Images +add_filter('acf/format_value/type=gallery', NS . 'formatGallery', 100); + +// Convert ACF Field of type post_object to a Timber\Post and add all ACF Fields of that Post +add_filter('acf/format_value/type=post_object', NS . 'formatPostObject', 100); + +function renderTwigIndex($output, $componentName, $componentData, $areaHtml) +{ // get index file $componentManager = Flynt\ComponentManager::getInstance(); - $filePath = $componentManager->getComponentFilePath($componentName, 'index.twig'); + $templateFilename = apply_filters('Flynt/Features/TimberLoader/templateFilename', 'index.twig'); + $templateFilename = apply_filters("Flynt/Features/TimberLoader/templateFilename?name=${componentName}", $templateFilename); + $filePath = $componentManager->getComponentFilePath($componentName, $templateFilename); $relativeFilePath = ltrim(str_replace(get_template_directory(), '', $filePath), '/'); if (!is_file($filePath)) { @@ -49,35 +63,35 @@ remove_filter('get_twig', $addArea); return $output; -}, 10, 4); +} -// Convert ACF Images to Timber Images -add_filter('acf/format_value/type=image', function ($value) { +function formatImage($value) +{ if (!empty($value)) { $value = new Image($value); } return $value; -}, 100); +} -// Convert ACF Gallery Images to Timber Images -add_filter('acf/format_value/type=gallery', function ($value) { +function formatGallery($value) +{ if (!empty($value)) { $value = array_map(function ($image) { return new Image($image); }, $value); } return $value; -}, 100); +} -// Convert ACF Field of type post_object to a Timber\Post and add all ACF Fields of that Post -add_filter('acf/format_value/type=post_object', function ($value) { +function formatPostObject($value) +{ if (is_array($value)) { $value = array_map(NS . 'convertToTimberPost', $value); } else { $value = convertToTimberPost($value); } return $value; -}, 100); +} function convertToTimberPost($value) {