Skip to content

Commit 289d358

Browse files
committed
Fixed hooks for Automated list to allow alteration withiin consumer sites.
1 parent 0d5d7b9 commit 289d358

File tree

6 files changed

+175
-88
lines changed

6 files changed

+175
-88
lines changed

tests/behat/features/paragraph.civictheme_automated_list.render.feature

+10-5
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,11 @@ Feature: Automated list render
238238
@api @testmode
239239
Scenario: Automated list, different view from listing type field
240240
Given "civictheme_page" content:
241-
| title | created | status | moderation_state |
242-
| [TEST] Page 16 | [relative:-5 days] | 1 | published |
243-
| [TEST] Page 17 | [relative:-5 days] | 1 | published |
241+
| title | created | status | moderation_state | field_c_n_vertical_spacing |
242+
| [TEST] Page 16 | [relative:-5 days] | 1 | published | both |
243+
| [TEST] Page 17 | [relative:-5 days] | 1 | published | both |
244+
| [TEST] Page 18 | [relative:-5 days] | 1 | published | top |
245+
| [TEST] Page 19 | [relative:-5 days] | 1 | published | top |
244246

245247
And "field_c_n_components" in "civictheme_page" "node" with "title" of "Test page with Automated list content" has "civictheme_automated_list" paragraph:
246248
| field_c_p_title | [TEST] Automated list title |
@@ -260,7 +262,9 @@ Feature: Automated list render
260262
And I should see an ".ct-list__filters" element
261263

262264
# Add a Test view as a list type.
263-
# This view only shows items older than 2 days and has a Title filter exposed.
265+
# This view only shows items older than 2 days, having a 'top' vertical
266+
# spacing (to assert the adjustems via preprocess hook) and has a Title
267+
# filter exposed.
264268
When I am logged in as a user with the "Administrator" role
265269
And I go to "admin/structure/paragraphs_type/civictheme_automated_list/fields/paragraph.civictheme_automated_list.field_c_p_list_type"
266270
And I fill in the following:
@@ -277,7 +281,8 @@ Feature: Automated list render
277281
And I press "Save"
278282

279283
And I should see 2 ".ct-promo-card" elements
280-
And I should see 2 ".ct-promo-card.ct-theme-light" elements
284+
# Preprocess hook overrides the 'item_theme' to be 'dark'.
285+
And I should see 2 ".ct-promo-card.ct-theme-dark" elements
281286
And I should not see an ".ct-list__pagination" element
282287

283288
And I should see an ".ct-list__filters" element

web/modules/custom/civictheme_dev/civictheme_dev.module

+43
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
declare(strict_types=1);
99

10+
use Drupal\civictheme\CivicthemeConstants;
1011
use Drupal\civictheme\CivicthemeVersionManager;
1112
use Drupal\Core\Site\Settings;
13+
use Drupal\views\ViewExecutable;
1214

1315
require_once __DIR__ . '/styleguide.inc';
1416

@@ -48,3 +50,44 @@ function civictheme_dev_styleguide_alter(array &$items): void {
4850
_civictheme_dev_styleguide__form_test__checkboxes($items);
4951
_civictheme_dev_styleguide__form_test__radios($items);
5052
}
53+
54+
/**
55+
* Implements hook_civictheme_automated_list_view_info_alter().
56+
*/
57+
function civictheme_dev_civictheme_automated_list_view_info_alter(array &$info, array $settings): void {
58+
if ($settings['content_type'] == 'civictheme_event') {
59+
// Use a different view and display for 'civictheme_event' content type.
60+
$info['view_name'] = 'civictheme_automated_list_examples';
61+
$info['display_name'] = 'page_multiple_filters';
62+
}
63+
}
64+
65+
/**
66+
* Implements hook_civictheme_automated_list_preprocess_view_alter().
67+
*/
68+
function civictheme_dev_civictheme_automated_list_preprocess_view_alter(ViewExecutable $view): void {
69+
if ($view->id() === 'civictheme_automated_list_test') {
70+
if (!property_exists($view, 'component_settings') || $view->component_settings === NULL) {
71+
return;
72+
}
73+
74+
$view->component_settings['theme'] = CivicthemeConstants::THEME_LIGHT;
75+
$view->component_settings['item_theme'] = CivicthemeConstants::THEME_DARK;
76+
77+
// Example of setting view arguments based on the expected contextual
78+
// filters of this specific view.
79+
$view_args = [];
80+
// First view argument - content types.
81+
$view_args[] = $view->component_settings['content_type'] ?? 'all';
82+
// Second view argument - Topics.
83+
$view_args[] = empty($view->component_settings['topics']) ? 'all' : implode('+', array_keys($view->component_settings['topics']));
84+
// Third view argument - Site Sections.
85+
$view_args[] = empty($view->component_settings['site_sections']) ? 'all' : implode('+', array_keys($view->component_settings['site_sections']));
86+
// Fourth view argument - Content ID value.
87+
$view_args[] = 'all';
88+
// Fifth view argument - Vertical Spacing value (used as an example).
89+
$view_args[] = CivicthemeConstants::VERTICAL_SPACING_TOP;
90+
91+
$view->setArguments($view_args);
92+
}
93+
}

web/modules/custom/civictheme_dev/config/install/views.view.civictheme_automated_list_test.yml

+40-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ dependencies:
77
- taxonomy.vocabulary.civictheme_topics
88
module:
99
- node
10+
- options
1011
- taxonomy
1112
- user
1213
id: civictheme_automated_list_test
@@ -300,6 +301,45 @@ display:
300301
validate_options: { }
301302
break_phrase: false
302303
not: true
304+
field_c_n_vertical_spacing_value:
305+
id: field_c_n_vertical_spacing_value
306+
table: node__field_c_n_vertical_spacing
307+
field: field_c_n_vertical_spacing_value
308+
relationship: none
309+
group_type: group
310+
admin_label: ''
311+
plugin_id: string_list_field
312+
default_action: ignore
313+
exception:
314+
value: all
315+
title_enable: false
316+
title: All
317+
title_enable: false
318+
title: ''
319+
default_argument_type: fixed
320+
default_argument_options:
321+
argument: ''
322+
summary_options:
323+
base_path: ''
324+
count: true
325+
override: false
326+
items_per_page: 25
327+
summary:
328+
sort_order: asc
329+
number_of_records: 0
330+
format: default_summary
331+
human: false
332+
specify_validation: false
333+
validate:
334+
type: none
335+
fail: 'not found'
336+
validate_options: { }
337+
glossary: false
338+
limit: 0
339+
case: none
340+
path_case: none
341+
transform_dash: false
342+
break_phrase: false
303343
filters:
304344
status:
305345
id: status
@@ -572,7 +612,6 @@ display:
572612
- 'languages:language_interface'
573613
- url
574614
- url.query_args
575-
- user
576615
- 'user.node_grants:view'
577616
- user.permissions
578617
tags: { }
@@ -595,7 +634,6 @@ display:
595634
- 'languages:language_interface'
596635
- url
597636
- url.query_args
598-
- user
599637
- 'user.node_grants:view'
600638
- user.permissions
601639
tags: { }

web/themes/contrib/civictheme/civictheme.api.php

+68-41
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,23 @@
1212
use Drupal\views\ViewExecutable;
1313

1414
/**
15-
* Alter the view info used in the Automated list component.
15+
* Alter the info about the view used in the Automated list component.
16+
*
17+
* This hook allows to alter which view and display are used to power the
18+
* Automated list component based on the settings provided.
19+
* The settings are extracted from the fields provided by the CivicTheme within
20+
* the Automated list paragraph entity.
21+
* Note that for any custom fields added to the Automated list paragraph entity,
22+
* the settings would need to be extracted from those fields using
23+
* $settings['paragraph']->get('field_name')->getString() or similar methods as
24+
* CivicTheme cannot predict the field names used in the custom implementation.
1625
*
1726
* @param array $info
1827
* View info array to alter passed by reference. Keys are:
1928
* - view_name: (string) A view machine name.
2029
* - display_name: (string) A view display machine name.
2130
* @param array $settings
22-
* The Automated list component settings passed by reference with the
31+
* The Automated list component settings with the
2332
* following keys:
2433
* - title: (string) List title.
2534
* - type: (string) List type (view name that powers Automated list).
@@ -33,8 +42,9 @@
3342
* - topics: (array) Array of Topic entities.
3443
* - site_sections: (array) Array of Site section entities.
3544
* - cache_tags: (array) Array of the cache tags.
45+
* - paragraph: (Paragraph) The paragraph entity.
3646
*/
37-
function hook_civictheme_automated_list_view_info_alter(array &$info, array &$settings): void {
47+
function hook_civictheme_automated_list_view_info_alter(array &$info, array $settings): void {
3848
// Change the view name and block based on the conditions set in the
3949
// Automated list settings.
4050
if ($settings['content_type'] == 'event') {
@@ -49,48 +59,65 @@ function hook_civictheme_automated_list_view_info_alter(array &$info, array &$se
4959
}
5060

5161
/**
52-
* Alter the CivicTheme view preprocess settings.
62+
* Alter the view used in the Automated list component before rendering.
5363
*
54-
* @param array $variables
55-
* Array of preprocess variables of the Automated list view.
64+
* The view instance already has settings applied from the default fields
65+
* provided by the CivicTheme within the Automated list paragraph entity.
66+
* Any additional settings can be extracted from the fields provided by the
67+
* Automated list paragraph entity using $view->component_settings['paragraph'].
68+
*
69+
* @param \Drupal\views\ViewExecutable $view
70+
* The view object to alter.
5671
*/
57-
function hook_civictheme_automated_list_preprocess_view_alter(array &$variables, ViewExecutable &$view): void {
58-
if ($view->id() == 'civictheme_view_examples') {
59-
$variables['theme'] = CivicthemeConstants::THEME_DARK;
60-
$variables['with_background'] = TRUE;
61-
$variables['vertical_spacing'] = 'both';
72+
function hook_civictheme_automated_list_preprocess_view_alter(ViewExecutable $view): void {
73+
// Example of altering the view theme, item theme and arguments.
74+
if ($view->id() === 'custom_view_id') {
75+
$view->component_settings['theme'] = CivicthemeConstants::THEME_LIGHT;
76+
$view->component_settings['item_theme'] = CivicthemeConstants::THEME_DARK;
77+
78+
// Example of setting view arguments based on the expected contextual
79+
// filters of this specific view.
80+
// In thi example, the view has 3 contextual filters.
81+
$view_args = [];
82+
// First view argument - content types. Read from settings.
83+
$view_args[] = $view->component_settings['content_type'] ?? 'all';
84+
// Second view argument - Content ID value. Use `all` to skip it.
85+
$view_args[] = 'all';
86+
// Third view argument - Vertical Spacing value. Use a constant value.
87+
$view_args[] = CivicthemeConstants::VERTICAL_SPACING_TOP;
88+
// Set the arguments to the view.
89+
$view->setArguments($view_args);
6290
}
91+
}
6392

64-
/**
65-
* Allow to suppress page regions for pages with Layout Builder enabled.
66-
*
67-
* @param array $variables
68-
* Array of variables passed to the page template.
69-
* @param array $context
70-
* Array of context data.
71-
* - node: The node object.
72-
* - layout_builder_settings_per_view_mode: An array of the layout builder
73-
* settings keyed by view mode.
74-
*
75-
* @SuppressWarnings(PHPMD.StaticAccess)
76-
*/
77-
function hook_civictheme_layout_suppress_page_regions_alter(array &$variables, array $context): void {
78-
/** @var \Drupal\node\NodeInterface $node */
79-
$node = $variables['node'];
80-
if ($node->bundle() == 'civictheme_page' && $context['layout_builder_settings_per_view_mode']['full']['enabled']) {
81-
$variables['page']['sidebar_top_left'] = [];
82-
$variables['page']['sidebar_bottom_left'] = [];
83-
$variables['page']['sidebar_top_right'] = [];
84-
$variables['page']['sidebar_bottom_right'] = [];
93+
/**
94+
* Allow to suppress page regions for pages with Layout Builder enabled.
95+
*
96+
* @param array $variables
97+
* Array of variables passed to the page template.
98+
* @param array $context
99+
* Array of context data.
100+
* - node: The node object.
101+
* - layout_builder_settings_per_view_mode: An array of the layout builder
102+
* settings keyed by view mode.
103+
*
104+
* @SuppressWarnings(PHPMD.StaticAccess)
105+
*/
106+
function hook_civictheme_layout_suppress_page_regions_alter(array &$variables, array $context): void {
107+
/** @var \Drupal\node\NodeInterface $node */
108+
$node = $variables['node'];
109+
if ($node->bundle() == 'civictheme_page' && $context['layout_builder_settings_per_view_mode']['full']['enabled']) {
110+
$variables['page']['sidebar_top_left'] = [];
111+
$variables['page']['sidebar_bottom_left'] = [];
112+
$variables['page']['sidebar_top_right'] = [];
113+
$variables['page']['sidebar_bottom_right'] = [];
85114

86-
// Do not forget to merge the cache contexts.
87-
$variables['#cache']['contexts'] = Cache::mergeContexts(
88-
$variables['#cache']['contexts'] ?? [],
89-
[
90-
'user.roles:authenticated',
91-
]
92-
);
93-
}
115+
// Do not forget to merge the cache contexts.
116+
$variables['#cache']['contexts'] = Cache::mergeContexts(
117+
$variables['#cache']['contexts'] ?? [],
118+
[
119+
'user.roles:authenticated',
120+
]
121+
);
94122
}
95-
96123
}

web/themes/contrib/civictheme/civictheme_starter_kit/civictheme_starter_kit.theme

-34
This file was deleted.

web/themes/contrib/civictheme/includes/automated_list.inc

+14-6
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,19 @@ function civictheme_preprocess_paragraph__civictheme_automated_list(array &$vari
3939
_civictheme_preprocess_paragraph__paragraph_field__background($variables);
4040

4141
// Views settings.
42-
// We want to have a single place to gather all the view settings to pass
42+
// We want to have a single place to gather all initial view settings to pass
4343
// them further in the pipeline. There should not be any other places to
4444
// access these fields variables directly (separation of concerns). Only
4545
// these settings should be used.
46+
// Consumer sites can alter these settings in
47+
// hook_civictheme_automated_list_preprocess_view_alter().
4648
$settings = [];
49+
// Store paragraph entity in the settings to allow accessing the values of
50+
// fields that may be added in the consumer sites (in addition to the fields
51+
// provided by the CivicTheme Automated List paragraph).
52+
$settings['paragraph'] = $paragraph;
53+
$settings['cache_tags'] = $paragraph->getCacheTags();
54+
// Settings provided by the CivicTheme Automated List paragraph's fields.
4755
$settings['type'] = civictheme_get_field_value($paragraph, 'field_c_p_list_type');
4856
$settings['content_type'] = civictheme_get_field_value($paragraph, 'field_c_p_list_content_type');
4957
$settings['limit'] = civictheme_get_field_value($paragraph, 'field_c_p_list_limit');
@@ -53,14 +61,10 @@ function civictheme_preprocess_paragraph__civictheme_automated_list(array &$vari
5361
$settings['item_theme'] = civictheme_get_field_value($paragraph, 'field_c_p_list_item_theme');
5462
$settings['topics'] = civictheme_get_field_value($paragraph, 'field_c_p_list_topics');
5563
$settings['site_sections'] = civictheme_get_field_value($paragraph, 'field_c_p_list_site_sections');
56-
$settings['cache_tags'] = $paragraph->getCacheTags();
57-
// Use existing value getters to populate settings.
58-
$settings['paragraph'] = $paragraph;
5964
_civictheme_preprocess_paragraph__paragraph_field__theme($settings);
6065
_civictheme_preprocess_paragraph__paragraph_field__title($settings);
6166
_civictheme_preprocess_paragraph__paragraph_field__column_count($settings);
6267
_civictheme_preprocess_paragraph__paragraph_field__fill_width($settings);
63-
unset($settings['paragraph']);
6468

6569
// Get view with already set display.
6670
try {
@@ -128,7 +132,7 @@ function _civictheme_automated_list__get_view(array &$settings): ViewExecutable
128132
'display_name' => $parts[1] ?? 'default',
129133
];
130134

131-
// Allow to alter view information to modules and themes.
135+
// Allow modules and themes to alter view information.
132136
\Drupal::moduleHandler()->alter('civictheme_automated_list_view_info', $info, $settings);
133137
\Drupal::service('theme.manager')->alter('civictheme_automated_list_view_info', $info, $settings);
134138
}
@@ -252,6 +256,10 @@ function _civictheme_automated_list__update_view(ViewExecutable $view): void {
252256
}
253257
}
254258

259+
// Allow modules and themes to alter view based on settings.
260+
\Drupal::moduleHandler()->alter('civictheme_automated_list_preprocess_view', $view);
261+
\Drupal::service('theme.manager')->alter('civictheme_automated_list_preprocess_view', $view);
262+
255263
// Mark these settings.
256264
$view->component_settings['processed'] = TRUE;
257265
}

0 commit comments

Comments
 (0)