Skip to content
Permalink
Browse files

Update _macro-mergedPosts.twig

  • Loading branch information...
davidhellmann committed Mar 11, 2019
1 parent 8195eca commit 78febffabb65aed776973df097bbddeb60c68087
@@ -4,11 +4,11 @@
Set Active Class
------------------------------------------------------------
{% import '_partials/macros/_macro-mergedPosts.twig' as macroMergedPosts %}
{% import '_partials/macros/_macro-mergedPosts.html' as macroMergedPosts %}
{{ macroMergedPosts.mergedPosts('news', -1, 'cn') }}
#}

{% macro mergedPosts(field, channel, channelOrder, maxPostCount) %}
{% macro mergedPosts(field, channel, channelOrder, maxPostCount, entry) %}

{# -- Set Default Variables -- #}
{% set field = field | default(null) %}
@@ -17,7 +17,6 @@
{% set maxPostCount = maxPostCount | default(12) %}
{% set finalEntries = null %}


{% set entriesFromFieldCount = 0 %}
{% set entriesFromField = [] %}
{% set excludePostsIds = [] %}
@@ -28,6 +27,10 @@
{% set entriesFromFieldCount = field | length %}
{% set entriesFromField = field.all() %}
{% set excludePostsIds = field.ids() %}

{% if entry ?? null %}
{% set excludePostsIds = excludePostsIds | merge([entry.id]) %}
{% endif %}
{% set excludePosts = excludePostsIds | join(', not ') %}
{% endif %}

@@ -45,56 +48,6 @@
{% set finalEntries = finalEntries | merge(entriesFromField) %}
{% endif %}

{# -- Output -- #}
{% return finalEntries %}
{% endmacro %}{# @var craft \craft\web\twig\variables\CraftVariable #}
{# @var entry \craft\elements\Entry #}
{#
Set Active Class
------------------------------------------------------------
{% import '_partials/macros/_macro-mergedPosts.twig' as macroMergedPosts %}
{{ macroMergedPosts.mergedPosts('news', -1, 'cn') }}
#}

{% macro mergedPosts(field, channel, channelOrder, maxPostCount) %}

{# -- Set Default Variables -- #}
{% set field = field | default(null) %}
{% set channel = channel | default(null) %}
{% set channelOrder = channelOrder | default('postDate desc') %}
{% set maxPostCount = maxPostCount | default(12) %}
{% set finalEntries = null %}


{% set entriesFromFieldCount = 0 %}
{% set entriesFromField = [] %}
{% set excludePostsIds = [] %}
{% set excludePosts = [] %}

{# -- If field exists do this -- #}
{% if field ?? null %}
{% set entriesFromFieldCount = field | length %}
{% set entriesFromField = field.all() %}
{% set excludePostsIds = field.ids() %}
{% set excludePosts = excludePostsIds | join(', not ') %}
{% endif %}

{# -- Calculate Entry Count -- #}
{% set entriesCount = (maxPostCount - entriesFromFieldCount) %}
{% set finalEntries = entriesFromField %}

{# -- Some Condition Magic -- #}
{% if entriesCount == 0 %}
{% set finalEntries = entriesFromField %}
{% elseif entriesCount == maxPostCount %}
{% set finalEntries = craft.entries.section(channel).limit(entriesCount).orderBy(channelOrder).all() %}
{% else %}
{% set finalEntries = craft.entries.section(channel).id('and, not '~excludePosts).limit(entriesCount).orderBy(channelOrder).all() %}
{% set finalEntries = finalEntries | merge(entriesFromField) %}
{% endif %}

{# -- Output -- #}
{% return finalEntries %}
{% endmacro %}

0 comments on commit 78febff

Please sign in to comment.
You can’t perform that action at this time.