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

Fixed hooks for Automated list to allow alteration within consumer sites. #1287

Merged
merged 1 commit into from
Jul 15, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,11 @@ Feature: Automated list render
@api @testmode
Scenario: Automated list, different view from listing type field
Given "civictheme_page" content:
| title | created | status | moderation_state |
| [TEST] Page 16 | [relative:-5 days] | 1 | published |
| [TEST] Page 17 | [relative:-5 days] | 1 | published |
| title | created | status | moderation_state | field_c_n_vertical_spacing |
| [TEST] Page 16 | [relative:-5 days] | 1 | published | both |
| [TEST] Page 17 | [relative:-5 days] | 1 | published | both |
| [TEST] Page 18 | [relative:-5 days] | 1 | published | top |
| [TEST] Page 19 | [relative:-5 days] | 1 | published | top |

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

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

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

And I should see an ".ct-list__filters" element
Expand Down
2 changes: 0 additions & 2 deletions tests/phpunit/CivicthemeCreateSubthemeScriptUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ public function testLocation(string $civictheme_dir, string $newtheme_rel_dir, s
$this->assertFileExists($expected_new_theme_dir_full . '.stylelintrc.json');
$this->assertFileExists($expected_new_theme_dir_full . $newtheme_name . '.info.yml');
$this->assertFileExists($expected_new_theme_dir_full . $newtheme_name . '.libraries.yml');
$this->assertFileExists($expected_new_theme_dir_full . $newtheme_name . '.theme');
$this->assertFileExists($expected_new_theme_dir_full . 'gulpfile.js');
$this->assertFileExists($expected_new_theme_dir_full . 'composer.json');
$this->assertFileExists($expected_new_theme_dir_full . 'package.json');
Expand Down Expand Up @@ -246,7 +245,6 @@ public function testExamplesRemoval(): void {
$this->assertFileExists($expected_new_theme_dir_full . '.stylelintrc.json');
$this->assertFileExists($expected_new_theme_dir_full . $newtheme_name . '.info.yml');
$this->assertFileExists($expected_new_theme_dir_full . $newtheme_name . '.libraries.yml');
$this->assertFileExists($expected_new_theme_dir_full . $newtheme_name . '.theme');
$this->assertFileExists($expected_new_theme_dir_full . 'gulpfile.js');
$this->assertFileExists($expected_new_theme_dir_full . 'package.json');
$this->assertFileExists($expected_new_theme_dir_full . 'README.md');
Expand Down
43 changes: 43 additions & 0 deletions web/modules/custom/civictheme_dev/civictheme_dev.module
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

declare(strict_types=1);

use Drupal\civictheme\CivicthemeConstants;
use Drupal\civictheme\CivicthemeVersionManager;
use Drupal\Core\Site\Settings;
use Drupal\views\ViewExecutable;

require_once __DIR__ . '/styleguide.inc';

Expand Down Expand Up @@ -48,3 +50,44 @@ function civictheme_dev_styleguide_alter(array &$items): void {
_civictheme_dev_styleguide__form_test__checkboxes($items);
_civictheme_dev_styleguide__form_test__radios($items);
}

/**
* Implements hook_civictheme_automated_list_view_info_alter().
*/
function civictheme_dev_civictheme_automated_list_view_info_alter(array &$info, array $settings): void {
if ($settings['content_type'] == 'civictheme_event') {
// Use a different view and display for 'civictheme_event' content type.
$info['view_name'] = 'civictheme_automated_list_examples';
$info['display_name'] = 'page_multiple_filters';
}
}

/**
* Implements hook_civictheme_automated_list_view_alter().
*/
function civictheme_dev_civictheme_automated_list_view_alter(ViewExecutable $view): void {
if ($view->id() === 'civictheme_automated_list_test') {
if (!property_exists($view, 'component_settings') || $view->component_settings === NULL) {
return;
}

$view->component_settings['theme'] = CivicthemeConstants::THEME_LIGHT;
$view->component_settings['item_theme'] = CivicthemeConstants::THEME_DARK;

// Example of setting view arguments based on the expected contextual
// filters of this specific view.
$view_args = [];
// First view argument - content types.
$view_args[] = $view->component_settings['content_type'] ?? 'all';
// Second view argument - Topics.
$view_args[] = empty($view->component_settings['topics']) ? 'all' : implode('+', array_keys($view->component_settings['topics']));
// Third view argument - Site Sections.
$view_args[] = empty($view->component_settings['site_sections']) ? 'all' : implode('+', array_keys($view->component_settings['site_sections']));
// Fourth view argument - Content ID value.
$view_args[] = 'all';
// Fifth view argument - Vertical Spacing value (used as an example).
$view_args[] = CivicthemeConstants::VERTICAL_SPACING_TOP;

$view->setArguments($view_args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies:
- taxonomy.vocabulary.civictheme_topics
module:
- node
- options
- taxonomy
- user
id: civictheme_automated_list_test
Expand Down Expand Up @@ -300,6 +301,45 @@ display:
validate_options: { }
break_phrase: false
not: true
field_c_n_vertical_spacing_value:
id: field_c_n_vertical_spacing_value
table: node__field_c_n_vertical_spacing
field: field_c_n_vertical_spacing_value
relationship: none
group_type: group
admin_label: ''
plugin_id: string_list_field
default_action: ignore
exception:
value: all
title_enable: false
title: All
title_enable: false
title: ''
default_argument_type: fixed
default_argument_options:
argument: ''
summary_options:
base_path: ''
count: true
override: false
items_per_page: 25
summary:
sort_order: asc
number_of_records: 0
format: default_summary
human: false
specify_validation: false
validate:
type: none
fail: 'not found'
validate_options: { }
glossary: false
limit: 0
case: none
path_case: none
transform_dash: false
break_phrase: false
filters:
status:
id: status
Expand Down Expand Up @@ -572,7 +612,6 @@ display:
- 'languages:language_interface'
- url
- url.query_args
- user
- 'user.node_grants:view'
- user.permissions
tags: { }
Expand All @@ -595,7 +634,6 @@ display:
- 'languages:language_interface'
- url
- url.query_args
- user
- 'user.node_grants:view'
- user.permissions
tags: { }
109 changes: 68 additions & 41 deletions web/themes/contrib/civictheme/civictheme.api.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,23 @@
use Drupal\views\ViewExecutable;

/**
* Alter the view info used in the Automated list component.
* Alter the info about the view used in the Automated list component.
*
* This hook allows to alter which view and display are used to power the
* Automated list component based on the settings provided.
* The settings are extracted from the fields provided by the CivicTheme within
* the Automated list paragraph entity.
* Note that for any custom fields added to the Automated list paragraph entity,
* the settings would need to be extracted from those fields using
* $settings['paragraph']->get('field_name')->getString() or similar methods as
* CivicTheme cannot predict the field names used in the custom implementation.
*
* @param array $info
* View info array to alter passed by reference. Keys are:
* - view_name: (string) A view machine name.
* - display_name: (string) A view display machine name.
* @param array $settings
* The Automated list component settings passed by reference with the
* The Automated list component settings with the
* following keys:
* - title: (string) List title.
* - type: (string) List type (view name that powers Automated list).
Expand All @@ -33,8 +42,9 @@
* - topics: (array) Array of Topic entities.
* - site_sections: (array) Array of Site section entities.
* - cache_tags: (array) Array of the cache tags.
* - paragraph: (Paragraph) The paragraph entity.
*/
function hook_civictheme_automated_list_view_info_alter(array &$info, array &$settings): void {
function hook_civictheme_automated_list_view_info_alter(array &$info, array $settings): void {
// Change the view name and block based on the conditions set in the
// Automated list settings.
if ($settings['content_type'] == 'event') {
Expand All @@ -49,48 +59,65 @@ function hook_civictheme_automated_list_view_info_alter(array &$info, array &$se
}

/**
* Alter the CivicTheme view preprocess settings.
* Alter the view used in the Automated list component before rendering.
*
* @param array $variables
* Array of preprocess variables of the Automated list view.
* The view instance already has settings applied from the default fields
* provided by the CivicTheme within the Automated list paragraph entity.
* Any additional settings can be extracted from the fields provided by the
* Automated list paragraph entity using $view->component_settings['paragraph'].
*
* @param \Drupal\views\ViewExecutable $view
* The view object to alter.
*/
function hook_civictheme_automated_list_preprocess_view_alter(array &$variables, ViewExecutable &$view): void {
if ($view->id() == 'civictheme_view_examples') {
$variables['theme'] = CivicthemeConstants::THEME_DARK;
$variables['with_background'] = TRUE;
$variables['vertical_spacing'] = 'both';
function hook_civictheme_automated_list_view_alter(ViewExecutable $view): void {
// Example of altering the view theme, item theme and arguments.
if ($view->id() === 'custom_view_id') {
$view->component_settings['theme'] = CivicthemeConstants::THEME_LIGHT;
$view->component_settings['item_theme'] = CivicthemeConstants::THEME_DARK;

// Example of setting view arguments based on the expected contextual
// filters of this specific view.
// In thi example, the view has 3 contextual filters.
$view_args = [];
// First view argument - content types. Read from settings.
$view_args[] = $view->component_settings['content_type'] ?? 'all';
// Second view argument - Content ID value. Use `all` to skip it.
$view_args[] = 'all';
// Third view argument - Vertical Spacing value. Use a constant value.
$view_args[] = CivicthemeConstants::VERTICAL_SPACING_TOP;
// Set the arguments to the view.
$view->setArguments($view_args);
}
}

/**
* Allow to suppress page regions for pages with Layout Builder enabled.
*
* @param array $variables
* Array of variables passed to the page template.
* @param array $context
* Array of context data.
* - node: The node object.
* - layout_builder_settings_per_view_mode: An array of the layout builder
* settings keyed by view mode.
*
* @SuppressWarnings(PHPMD.StaticAccess)
*/
function hook_civictheme_layout_suppress_page_regions_alter(array &$variables, array $context): void {
/** @var \Drupal\node\NodeInterface $node */
$node = $variables['node'];
if ($node->bundle() == 'civictheme_page' && $context['layout_builder_settings_per_view_mode']['full']['enabled']) {
$variables['page']['sidebar_top_left'] = [];
$variables['page']['sidebar_bottom_left'] = [];
$variables['page']['sidebar_top_right'] = [];
$variables['page']['sidebar_bottom_right'] = [];
/**
* Allow to suppress page regions for pages with Layout Builder enabled.
*
* @param array $variables
* Array of variables passed to the page template.
* @param array $context
* Array of context data.
* - node: The node object.
* - layout_builder_settings_per_view_mode: An array of the layout builder
* settings keyed by view mode.
*
* @SuppressWarnings(PHPMD.StaticAccess)
*/
function hook_civictheme_layout_suppress_page_regions_alter(array &$variables, array $context): void {
/** @var \Drupal\node\NodeInterface $node */
$node = $variables['node'];
if ($node->bundle() == 'civictheme_page' && $context['layout_builder_settings_per_view_mode']['full']['enabled']) {
$variables['page']['sidebar_top_left'] = [];
$variables['page']['sidebar_bottom_left'] = [];
$variables['page']['sidebar_top_right'] = [];
$variables['page']['sidebar_bottom_right'] = [];

// Do not forget to merge the cache contexts.
$variables['#cache']['contexts'] = Cache::mergeContexts(
$variables['#cache']['contexts'] ?? [],
[
'user.roles:authenticated',
]
);
}
// Do not forget to merge the cache contexts.
$variables['#cache']['contexts'] = Cache::mergeContexts(
$variables['#cache']['contexts'] ?? [],
[
'user.roles:authenticated',
]
);
}

}

This file was deleted.

Loading
Loading