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

refactor: streamline post preparation and icon handling in templates #756

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 1 addition & 32 deletions source/php/Module/Posts/Helper/GetPosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,7 @@ public function __construct(
*/
public function getPostsAndPaginationData(array $fields, int $page = 1) :array
{
$result = (array) $this->getPostsFromSelectedSites($fields, $page);

if( empty($result['posts'])) {
return $result;
}

$result['posts'] = array_map(function($post) use ($fields) {
$data['taxonomiesToDisplay'] = !empty($fields['taxonomy_display']) ? $fields['taxonomy_display'] : [];
$helperClass = '\Municipio\Helper\Post';
$helperMethod = 'preparePostObject';
$helperArchiveMethod = 'preparePostObjectArchive';

if(!class_exists($helperClass) || !method_exists($helperClass, $helperMethod) || !method_exists($helperClass, $helperArchiveMethod)) {
error_log("Class or method does not exist: {$helperClass}::{$helperMethod} or {$helperClass}::{$helperArchiveMethod}");
return $post;
}

if (in_array($fields['posts_display_as'], ['expandable-list'])) {
$post = call_user_func([$helperClass, $helperMethod], $post);
} else {
$post = call_user_func([$helperClass, $helperArchiveMethod], $post, $data);
}

if (!empty($post->schemaData['place']['pin'])) {
$post->attributeList['data-js-map-location'] = json_encode($post->schemaData['place']['pin']);
}

return $post;

}, $result['posts']);

return $result;
return (array) $this->getPostsFromSelectedSites($fields, $page);
}

private function getPostsFromSelectedSites(array $fields, int $page):array {
Expand Down
40 changes: 36 additions & 4 deletions source/php/Module/Posts/TemplateController/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,47 @@ public function addDataViewData(array $data, array $fields)
*/
public function preparePosts($posts = [])
{
$posts = array_map(function($post) {
$data['taxonomiesToDisplay'] = !empty($fields['taxonomy_display'] ?? null) ? $this->fields['taxonomy_display'] : [];
$helperClass = '\Municipio\Helper\Post';
$helperMethod = 'preparePostObject';
$helperArchiveMethod = 'preparePostObjectArchive';

if(!class_exists($helperClass) || !method_exists($helperClass, $helperMethod) || !method_exists($helperClass, $helperArchiveMethod)) {
error_log("Class or method does not exist: {$helperClass}::{$helperMethod} or {$helperClass}::{$helperArchiveMethod}");
return $post;
}

if (isset($this->fields['posts_display_as']) && in_array($this->fields['posts_display_as'], ['expandable-list'])) {
$post = call_user_func([$helperClass, $helperMethod], $post);
} else {
$post = call_user_func([$helperClass, $helperArchiveMethod], $post, $data);
}

if (!empty($post->schemaData['place']['pin'])) {
$post->attributeList['data-js-map-location'] = json_encode($post->schemaData['place']['pin']);
}

return $post;

}, $posts);

if(!empty($posts)) {
foreach ($posts as $index => &$post) {
$post = $this->setPostViewData($post, $index);

$post = array_filter((array) $post, function($value) {
return !empty($value) || $value === false || $value === "0";
});
// $post = array_filter((array) $post, function($value) {
// return !empty($value) || $value === false || $value === "0";
// });

// $post = (object) array_merge($this->getDefaultValuesForPosts(), $post);

$post = (object) array_merge($this->getDefaultValuesForPosts(), $post);
// Apply $this->getDefaultValuesForPosts() to the post object without turning it into an array
foreach ($this->getDefaultValuesForPosts() as $key => $value) {
if (!isset($post->$key)) {
$post->$key = $value;
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @package Modularity\Module\Posts\TemplateController
*/
class ExpandableListTemplate
class ExpandableListTemplate extends AbstractController
{
/**
* The instance of the Posts module associated with this template.
Expand Down Expand Up @@ -46,6 +46,7 @@ class ExpandableListTemplate
*/
public function __construct(\Modularity\Module\Posts\Posts $module)
{
parent::__construct($module);
$this->module = $module;
$this->args = $module->args;
$this->data = $module->data;
Expand Down Expand Up @@ -101,6 +102,8 @@ public function prepare(): ?array

$accordion = [];

$this->data['posts'] = $this->preparePosts($this->data['posts']);

if (!empty($this->data['posts']) && is_array($this->data['posts'])) {
foreach ($this->data['posts'] as $index => $item) {
if ($this->hasColumnValues($columnValues) && $this->hasColumnTitles($this->data)) {
Expand All @@ -113,7 +116,7 @@ public function prepare(): ?array
}
}
}
$accordion[$index]['heading'] = $item->postTitle ?? '';
$accordion[$index]['heading'] = $item->getTitle() ?? '';
$accordion[$index]['content'] = $item->postContentFiltered ?? '';
}
}
Expand Down
3 changes: 2 additions & 1 deletion source/php/Module/Posts/TemplateController/ListTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Class ListTemplate
* @package Modularity\Module\Posts\TemplateController
*/
class ListTemplate
class ListTemplate extends AbstractController
{
protected $args;
public $data = [];
Expand Down Expand Up @@ -39,6 +39,7 @@ public function prepare(array $postData)
{
$list = [];
if (!empty($this->data['posts']) && is_array($this->data['posts'])) {
$this->data['posts'] = $this->preparePosts($this->data['posts']);
foreach ($this->data['posts'] as $post) {
if ($post->getPostType() === 'attachment') {
$link = wp_get_attachment_url($post->getId());
Expand Down
6 changes: 5 additions & 1 deletion source/php/Module/Posts/views/partials/post/block.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
'classList' => ['t-posts-block', ' u-height--100'],
'context' => ['module.posts.block'],
'link' => $post->permalink,
'icon' => $post->termIcon,
'icon' => $post->getTermIcon() ? [
'icon' => $post->getTermIcon()->getIcon(),
'color' => 'white',
'backgroundColor' => $post->getTermIcon()->getColor(),
] : null,
'attributeList' => array_merge($post->attributeList, []),
])
@includeWhen(
Expand Down
6 changes: 5 additions & 1 deletion source/php/Module/Posts/views/partials/post/card.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
'containerAware' => true,
'hasPlaceholder' => $post->hasPlaceholderImage,
'image' => $post->image,
'icon' => $post->termIcon,
'icon' => $post->getTermIcon() ? [
'icon' => $post->getTermIcon()->getIcon(),
'color' => 'white',
'backgroundColor' => $post->getTermIcon()->getColor(),
] : null,
'attributeList' => array_merge($post->attributeList, []),
])
@slot('aboveContent')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,20 @@
])
{{ $post->postTitle }}
@endtypography
@if ($post->termIcon)
@if ($post->getTermIcon())
@inlineCssWrapper([
'styles' => ['background-color' => $post->termIcon['backgroundColor'], 'display' => 'flex'],
'styles' => ['background-color' => $post->getTermIcon()->getColor(), 'display' => 'flex'],
'classList' => [
$post->termIcon['backgroundColor'] ? '' : 'u-color__bg--primary',
$post->getTermIcon()->getColor() ? '' : 'u-color__bg--primary',
'u-rounded--full',
'u-detail-shadow-3'
]
])
@icon($post->termIcon)
@icon([
'icon' => $post->getTermIcon()->getIcon(),
'color' => 'white',
'backgroundColor' => $post->getTermIcon()->getColor(),
])
@endicon
@endinlineCssWrapper
@endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
'buttons' => [['text' => $lang['readMore'], 'href' => $post->permalink, 'color' => 'primary']],
'containerAware' => true,
'reverseColumns' => $imagePosition,
'icon' => $post->termIcon,
'icon' => $post->getTermIcon() ? [
'icon' => $post->getTermIcon()->getIcon(),
'color' => 'white',
'backgroundColor' => $post->getTermIcon()->getColor(),
] : null,
'hasPlaceholder' => $post->hasPlaceholderImage,
'attributeList' => $post->attributeList ?? [],
'classList' => $classList ?? [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
'hasPlaceholder' => !empty($post->hasPlaceholderImage),
'attributeList' => ['style' => 'z-index:' . (999 - $key) . ';'],
'classList' => ['u-height--100', 'project-card'],
'icon' => $post->termIcon ?? false
'icon' => $post->getTermIcon() ? [
'icon' => $post->getTermIcon()->getIcon(),
'color' => 'white',
'backgroundColor' => $post->getTermIcon()->getColor(),
] : null,
])
@slot('afterContent')
@if(!empty($post->project->statusBar))
Expand Down
Loading