Skip to content

Commit

Permalink
Issue #3409009 by alex.skrypnyk: Add summary settings for Snippet com…
Browse files Browse the repository at this point in the history
…ponent.
  • Loading branch information
AlexSkrypnyk committed Dec 15, 2023
1 parent 9c3a0b7 commit 9431275
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ civictheme.settings:
summary_length:
label: 'Summary length'
type: integer
snippet:
type: mapping
label: 'Snippet settings'
mapping:
summary_length:
label: 'Summary length'
type: integer
colors:
type: mapping
label: Colors
Expand Down
16 changes: 8 additions & 8 deletions web/themes/contrib/civictheme/includes/paragraphs.inc
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ function _civictheme_preprocess_paragraph__paragraph_field__links(array &$variab
function _civictheme_preprocess_paragraph__paragraph_field__summary(array &$variables): void {
$summary = civictheme_get_field_value($variables['paragraph'], 'field_c_p_summary', TRUE);
if (!empty($summary)) {
$length = CivicthemeConstants::CARD_SUMMARY_DEFAULT_LENGTH;
$length = CivicthemeConstants::COMPONENT_SUMMARY_DEFAULT_LENGTH;

$card_name = _civictheme_get_card_name_from_bundle($variables['paragraph']->bundle());
if ($card_name) {
$length = civictheme_get_theme_config_manager()->loadForComponent($card_name, 'summary_length', CivicthemeConstants::CARD_SUMMARY_DEFAULT_LENGTH);
$component_name = _civictheme_get_configurable_component_name_from_bundle($variables['paragraph']->bundle());
if ($component_name) {
$length = civictheme_get_theme_config_manager()->loadForComponent($component_name, 'summary_length', CivicthemeConstants::COMPONENT_SUMMARY_DEFAULT_LENGTH);
}

$variables['summary'] = text_summary($summary, NULL, $length);
Expand All @@ -231,11 +231,11 @@ function _civictheme_preprocess_paragraph__node_field__summary(array &$variables
if ($node) {
$summary = civictheme_get_field_value($node, 'field_c_n_summary', TRUE);
if (!empty($summary)) {
$length = CivicthemeConstants::CARD_SUMMARY_DEFAULT_LENGTH;
$length = CivicthemeConstants::COMPONENT_SUMMARY_DEFAULT_LENGTH;

$card_name = _civictheme_get_card_name_from_bundle($bundle ?? (!empty($variables['paragraph']) ? $variables['paragraph']->bundle() : 'none'));
if ($card_name) {
$length = civictheme_get_theme_config_manager()->loadForComponent($card_name, 'summary_length', CivicthemeConstants::CARD_SUMMARY_DEFAULT_LENGTH);
$component_name = _civictheme_get_configurable_component_name_from_bundle($bundle ?? (!empty($variables['paragraph']) ? $variables['paragraph']->bundle() : 'none'));
if ($component_name) {
$length = civictheme_get_theme_config_manager()->loadForComponent($component_name, 'summary_length', CivicthemeConstants::COMPONENT_SUMMARY_DEFAULT_LENGTH);
}

$variables['summary'] = text_summary($summary, NULL, $length);
Expand Down
2 changes: 1 addition & 1 deletion web/themes/contrib/civictheme/includes/snippet.inc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function civictheme_preprocess_paragraph__civictheme_snippet_ref(array &$variabl
*/
function _civictheme_preprocess_node__civictheme_snippet(array &$variables): void {
_civictheme_preprocess_paragraph__node_field__title($variables);
_civictheme_preprocess_paragraph__node_field__summary($variables);
_civictheme_preprocess_paragraph__node_field__summary($variables, $variables['view_mode'] ?? 'civictheme_snippet');
_civictheme_preprocess_paragraph__node_field__link($variables);
_civictheme_preprocess_paragraph__node_field__topics($variables);
// @see civictheme_preprocess_views_view_grid()
Expand Down
8 changes: 5 additions & 3 deletions web/themes/contrib/civictheme/includes/utilities.inc
Original file line number Diff line number Diff line change
Expand Up @@ -725,10 +725,12 @@ function civictheme_external_link_normalize_domain(string $domain): string {
}

/**
* Extract card name from card and reference card paragraph bundles.
* Extract a configurable component name from a paragraph bundle.
*/
function _civictheme_get_card_name_from_bundle(string $bundle): ?string {
if (preg_match('/civictheme_(.*_card).*/', $bundle, $matches)) {
function _civictheme_get_configurable_component_name_from_bundle(string $bundle): ?string {
// Accommodate for existing and additional components that could be added in
// the sub-themes.
if (preg_match('/civictheme_(.*_card|.*_?snippet).*/', $bundle, $matches)) {
if (!empty($matches[1])) {
return $matches[1];
}
Expand Down
9 changes: 9 additions & 0 deletions web/themes/contrib/civictheme/src/CivicthemeConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,17 @@ final class CivicthemeConstants {
*/
const NAVIGATION_DROPDOWN_DRAWER = 'drawer';

/**
* Defines component summary length.
*/
const COMPONENT_SUMMARY_DEFAULT_LENGTH = 160;

/**
* Defines card summary length.
*
* @deprecated in civictheme:1.6.2 and is removed from civicthtme:1.8.0.
* Replaceed by COMPONENT_SUMMARY_DEFAULT_LENGTH.
* @see https://www.drupal.org/project/civictheme/issues/3409009
*/
const CARD_SUMMARY_DEFAULT_LENGTH = 160;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ public function form(array &$form, FormStateInterface $form_state): void {
'#type' => 'number',
'#required' => TRUE,
'#min' => 0,
'#default_value' => $this->themeConfigManager->loadForComponent('event_card', 'summary_length', CivicthemeConstants::CARD_SUMMARY_DEFAULT_LENGTH),
'#default_value' => $this->themeConfigManager->loadForComponent('event_card', 'summary_length', CivicthemeConstants::COMPONENT_SUMMARY_DEFAULT_LENGTH),
];

$form['components']['navigation_card'] = [
Expand All @@ -368,7 +368,23 @@ public function form(array &$form, FormStateInterface $form_state): void {
'#type' => 'number',
'#required' => TRUE,
'#min' => 0,
'#default_value' => $this->themeConfigManager->loadForComponent('navigation_card', 'summary_length', CivicthemeConstants::CARD_SUMMARY_DEFAULT_LENGTH),
'#default_value' => $this->themeConfigManager->loadForComponent('navigation_card', 'summary_length', CivicthemeConstants::COMPONENT_SUMMARY_DEFAULT_LENGTH),
];

$form['components']['promo_card'] = [
'#type' => 'details',
'#title' => $this->t('Promo card'),
'#group' => 'components',
'#tree' => TRUE,
];

$form['components']['promo_card']['summary_length'] = [
'#title' => $this->t('Summary length'),
'#description' => $this->t('Set the length of the Summary field: the content will be trimmed to this length and ellipsis will be added. Set to 0 for no limit.'),
'#type' => 'number',
'#required' => TRUE,
'#min' => 0,
'#default_value' => $this->themeConfigManager->loadForComponent('promo_card', 'summary_length', CivicthemeConstants::COMPONENT_SUMMARY_DEFAULT_LENGTH),
];

$form['components']['publication_card'] = [
Expand All @@ -384,23 +400,23 @@ public function form(array &$form, FormStateInterface $form_state): void {
'#type' => 'number',
'#required' => TRUE,
'#min' => 0,
'#default_value' => $this->themeConfigManager->loadForComponent('publication_card', 'summary_length', CivicthemeConstants::CARD_SUMMARY_DEFAULT_LENGTH),
'#default_value' => $this->themeConfigManager->loadForComponent('publication_card', 'summary_length', CivicthemeConstants::COMPONENT_SUMMARY_DEFAULT_LENGTH),
];

$form['components']['promo_card'] = [
$form['components']['snippet'] = [
'#type' => 'details',
'#title' => $this->t('Promo card'),
'#title' => $this->t('Snippet'),
'#group' => 'components',
'#tree' => TRUE,
];

$form['components']['promo_card']['summary_length'] = [
$form['components']['snippet']['summary_length'] = [
'#title' => $this->t('Summary length'),
'#description' => $this->t('Set the length of the Summary field: the content will be trimmed to this length and ellipsis will be added. Set to 0 for no limit.'),
'#type' => 'number',
'#required' => TRUE,
'#min' => 0,
'#default_value' => $this->themeConfigManager->loadForComponent('promo_card', 'summary_length', CivicthemeConstants::CARD_SUMMARY_DEFAULT_LENGTH),
'#default_value' => $this->themeConfigManager->loadForComponent('snippet', 'summary_length', CivicthemeConstants::COMPONENT_SUMMARY_DEFAULT_LENGTH),
];

$form['#process'][] = [$this, 'processForm'];
Expand Down

0 comments on commit 9431275

Please sign in to comment.