Skip to content
This repository has been archived by the owner on Apr 30, 2020. It is now read-only.

Commit

Permalink
feat(Features/TimberLoader): add template file name filter (#279)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Doğa Gürdal committed Jul 16, 2018
1 parent 40fb705 commit 769a7ef
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions Features/TimberLoader/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 769a7ef

Please sign in to comment.