Skip to content

Commit

Permalink
feat: Filter stories by theme
Browse files Browse the repository at this point in the history
Refs: #UNO-866
  • Loading branch information
attiks committed Nov 27, 2024
1 parent ef5b364 commit 915f161
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 7 deletions.
14 changes: 11 additions & 3 deletions config/core.entity_form_display.paragraph.stories.default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependencies:
- entity_browser.browser.stories
- field.field.paragraph.stories.field_limit
- field.field.paragraph.stories.field_node
- field.field.paragraph.stories.field_themes
- field.field.paragraph.stories.field_title
- field.field.paragraph.stories.paragraph_view_mode
- paragraphs.paragraphs_type.stories
Expand All @@ -26,7 +27,7 @@ content:
third_party_settings: { }
field_node:
type: entity_browser_entity_reference
weight: 3
weight: 4
region: content
settings:
entity_browser: stories
Expand All @@ -39,6 +40,12 @@ content:
view_mode: card_preview
selection_mode: selection_prepend
third_party_settings: { }
field_themes:
type: options_select
weight: 3
region: content
settings: { }
third_party_settings: { }
field_title:
type: string_textfield
weight: 1
Expand All @@ -58,16 +65,17 @@ content:
default: '0'
default_view_mode: cards
form_mode_bind: false
apply_to_preview: false
third_party_settings: { }
status:
type: boolean_checkbox
weight: 5
weight: 6
region: content
settings:
display_label: true
third_party_settings: { }
translation:
weight: 4
weight: 5
region: content
settings: { }
third_party_settings: { }
Expand Down
2 changes: 2 additions & 0 deletions config/core.entity_view_display.paragraph.stories.cards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependencies:
- core.entity_view_mode.paragraph.cards
- field.field.paragraph.stories.field_limit
- field.field.paragraph.stories.field_node
- field.field.paragraph.stories.field_themes
- field.field.paragraph.stories.field_title
- field.field.paragraph.stories.paragraph_view_mode
- paragraphs.paragraphs_type.stories
Expand All @@ -31,4 +32,5 @@ content:
hidden:
field_limit: true
field_node: true
field_themes: true
paragraph_view_mode: true
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ dependencies:
config:
- core.entity_view_mode.paragraph.cards_with_featured
- field.field.paragraph.stories.field_limit
- field.field.paragraph.stories.field_node
- field.field.paragraph.stories.field_themes
- field.field.paragraph.stories.field_title
- field.field.paragraph.stories.paragraph_view_mode
- paragraphs.paragraphs_type.stories
Expand All @@ -30,4 +32,5 @@ content:
hidden:
field_limit: true
field_node: true
field_themes: true
paragraph_view_mode: true
2 changes: 2 additions & 0 deletions config/core.entity_view_display.paragraph.stories.default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dependencies:
config:
- field.field.paragraph.stories.field_limit
- field.field.paragraph.stories.field_node
- field.field.paragraph.stories.field_themes
- field.field.paragraph.stories.field_title
- field.field.paragraph.stories.paragraph_view_mode
- paragraphs.paragraphs_type.stories
Expand All @@ -24,4 +25,5 @@ content:
hidden:
field_limit: true
field_node: true
field_themes: true
paragraph_view_mode: true
3 changes: 3 additions & 0 deletions config/core.entity_view_display.paragraph.stories.teasers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ dependencies:
- core.entity_view_mode.paragraph.teasers
- field.field.paragraph.stories.field_limit
- field.field.paragraph.stories.field_node
- field.field.paragraph.stories.field_themes
- field.field.paragraph.stories.field_title
- field.field.paragraph.stories.paragraph_view_mode
- paragraphs.paragraphs_type.stories
id: paragraph.stories.teasers
targetEntityType: paragraph
Expand All @@ -16,5 +18,6 @@ content: { }
hidden:
field_limit: true
field_node: true
field_themes: true
field_title: true
paragraph_view_mode: true
29 changes: 29 additions & 0 deletions config/field.field.paragraph.stories.field_themes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
uuid: bccb5284-4029-4b33-a5fc-a3d5abb9ea9b
langcode: en
status: true
dependencies:
config:
- field.storage.paragraph.field_themes
- paragraphs.paragraphs_type.stories
- taxonomy.vocabulary.theme
id: paragraph.stories.field_themes
field_name: field_themes
entity_type: paragraph
bundle: stories
label: Theme
description: ''
required: false
translatable: false
default_value: { }
default_value_callback: ''
settings:
handler: 'default:taxonomy_term'
handler_settings:
target_bundles:
theme: theme
sort:
field: name
direction: asc
auto_create: false
auto_create_bundle: ''
field_type: entity_reference
20 changes: 20 additions & 0 deletions config/field.storage.paragraph.field_themes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
uuid: 5b5105c9-bf9f-4ab1-97eb-10751e0ce898
langcode: en
status: true
dependencies:
module:
- paragraphs
- taxonomy
id: paragraph.field_themes
field_name: field_themes
entity_type: paragraph
type: entity_reference
settings:
target_type: taxonomy_term
module: core
locked: false
cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false
23 changes: 19 additions & 4 deletions html/modules/custom/unocha_paragraphs/unocha_paragraphs.module
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ function unocha_paragraphs_preprocess_paragraph__stories(array &$variables) {
// Retrieve the featured stories.
$featured = array_slice($paragraph->field_node?->referencedEntities() ?? [], 0, $limit, TRUE);

// Parameters for the "view all" link.
$view_all_query = [];

// Get selected themes.
$themes = $paragraph->field_themes?->referencedEntities() ?? [];
$themes_filter = [];
if (!empty($themes)) {
$theme_id = $themes[0]->id();
$themes_filter = ['field_theme', $theme_id, '='];
$view_all_query['themes'] = $theme_id;
}

// Add the featured stories to the story list. They are ordered by delta so
// reflect the order in the form.
$stories = [];
Expand All @@ -38,16 +50,14 @@ function unocha_paragraphs_preprocess_paragraph__stories(array &$variables) {
}
$limit = $limit - count($stories);

// Parameters for the "view all" link.
$view_all_query = [];

// Filter the stories based on the parent entity.
if ($limit > 0) {
if (isset($parent) && $parent instanceof NodeInterface && !empty($parent->id())) {
if ($parent->bundle() === 'response') {
// Get the stories tagged with the response.
$stories += unocha_paragraphs_get_stories([
['field_responses', $parent->id(), '='],
$themes_filter,
], $limit, $stories);

// Filter the News and Stories page by the response.
Expand All @@ -60,6 +70,7 @@ function unocha_paragraphs_preprocess_paragraph__stories(array &$variables) {
if (isset($region) && $region->bundle() === 'region') {
$stories += unocha_paragraphs_get_stories([
['field_regions', $region->id(), '='],
$themes_filter,
], $limit - count($stories), $stories);

// Also add the region as filter to have consistent results in the
Expand All @@ -72,6 +83,7 @@ function unocha_paragraphs_preprocess_paragraph__stories(array &$variables) {
// Get the stories tagged with the region.
$stories += unocha_paragraphs_get_stories([
['field_regions', $parent->id(), '='],
$themes_filter,
], $limit, $stories);

// Filter the News and Stories page by the region.
Expand Down Expand Up @@ -106,6 +118,7 @@ function unocha_paragraphs_preprocess_paragraph__stories(array &$variables) {
// Exclude the current story so we can show related ones only.
$exclude = array_merge($stories, $parent->bundle() === 'story' ? [$parent] : []);

$conditions[] = $themes_filter;
$stories += unocha_paragraphs_get_stories($conditions, $limit, $exclude);
}
}
Expand All @@ -114,7 +127,9 @@ function unocha_paragraphs_preprocess_paragraph__stories(array &$variables) {
// paragraph doesn't yet have a parent as it's not yet saved.
// @todo add disclaimer in the preview in the form?
else {
$stories += unocha_paragraphs_get_stories([], $limit, $stories);
$stories += unocha_paragraphs_get_stories([
$themes_filter,
], $limit, $stories);
}
}

Expand Down

0 comments on commit 915f161

Please sign in to comment.