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

UHF-9690 Character counter #704

Merged
merged 13 commits into from
Mar 6, 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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
"[#UHF-7008] Core localization file download URL is wrong (https://www.drupal.org/project/drupal/issues/3022876)": "https://git.drupalcode.org/project/drupal/-/commit/40a96136b2dfe4322338508dffa636f6cb407900.patch",
"[#UHF-7008] Add multilingual support for caching basefield definitions (https://www.drupal.org/project/drupal/issues/3114824)": "https://www.drupal.org/files/issues/2020-02-20/3114824_2.patch",
"[#UHF-7008] Admin toolbar and contextual links should always be rendered in the admin language (https://www.drupal.org/project/drupal/issues/2313309)": "https://www.drupal.org/files/issues/2023-12-19/2313309-179.patch",
"[#UHF-9388] Process translation config files for custom modules (https://www.drupal.org/i/2845437)": "https://www.drupal.org/files/issues/2023-10-16/2845437-61.patch"
"[#UHF-9388] Process translation config files for custom modules (https://www.drupal.org/i/2845437)": "https://www.drupal.org/files/issues/2023-10-16/2845437-61.patch",
"[#UHF-9690] Allow updating lists when switching from allowed values to allowed values function (https://www.drupal.org/i/2873353)": "https://www.drupal.org/files/issues/2021-05-18/allow-allowed-values-function-update-D9-2873353_1.patch"
},
"drupal/default_content": {
"https://www.drupal.org/project/default_content/issues/2640734#comment-14638943": "https://raw.githubusercontent.com/City-of-Helsinki/drupal-helfi-platform-config/main/patches/default_content_2.0.0-alpha2-2640734_manual_imports-e164a354.patch"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
field.widget.settings.formatted_text_character_counter:
type: mapping
label: 'Character counter formatted text widget display format settings'
mapping:
counter_step:
type: integer
label: 'Counter step'
counter_total:
type: integer
label: 'Counter total'
rows:
type: integer
label: 'Rows'
placeholder:
type: label
label: 'Placeholder'
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
field.widget.settings.textarea_character_counter:
type: mapping
label: 'Character counter text area widget display format settings'
mapping:
counter_step:
type: integer
label: 'Counter step'
counter_total:
type: integer
label: 'Counter total'
rows:
type: integer
label: 'Rows'
placeholder:
type: label
label: 'Placeholder'
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
field.widget.settings.textfield_character_counter:
type: mapping
label: 'Character counter textfield widget display format settings'
mapping:
counter_step:
type: integer
label: 'Counter step'
counter_total:
type: integer
label: 'Counter total'
size:
type: integer
label: 'Size of textfield'
placeholder:
type: label
label: 'Placeholder'
23 changes: 23 additions & 0 deletions modules/hdbt_admin_tools/hdbt_admin_tools.module
Original file line number Diff line number Diff line change
Expand Up @@ -1066,3 +1066,26 @@ function hdbt_admin_tools_entity_base_field_info_alter(&$fields) {
$fields['langcode']->setDescription($translation);
}
}

/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function hdbt_admin_tools_theme_suggestions_form_element_alter(array &$suggestions, array $variables): void {
// Add character count suggestions to configured form elements.
if (isset($variables['element']['#character_counter'])) {
$suggestions[] = $variables['theme_hook_original'] . '__character_count';
}
}

/**
* Implements hook_preprocess_HOOK().
*/
function hdbt_admin_tools_preprocess_form_element(array &$variables): void {
if (!isset($variables['element']['#character_counter'])) {
return;
}
// Set character counter values to variables.
$variables['counter_step'] = $variables['element']['#counter_step'];
$variables['counter_total'] = $variables['element']['#counter_total'];
$variables['counter_input_tag'] = $variables['element']['#type'] === 'textarea' ? 'textarea' : 'input';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Drupal\hdbt_admin_tools\Plugin\Field\FieldWidget;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;

/**
* Trait for handling character counter settings and form elements.
*/
trait CharacterCounterFieldWidgetTrait {

/**
* {@inheritdoc}
*/
public static function defaultSettings(): array {
return [
'counter_step' => 160,
'counter_total' => 200,
] + parent::defaultSettings();
}

/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state): array {
$element = parent::settingsForm($form, $form_state);
$element['counter_step'] = [
'#type' => 'number',
'#title' => $this->t('Suggestion text character count'),
'#default_value' => $this->getSetting('counter_step'),
'#required' => TRUE,
];
$element['counter_total'] = [
'#type' => 'number',
'#title' => $this->t('Warning text character count'),
'#default_value' => $this->getSetting('counter_total'),
'#required' => TRUE,
];
return $element;
}

/**
* {@inheritdoc}
*/
public function settingsSummary(): array {
$summary = parent::settingsSummary();
$summary[] = $this->t('Suggestion text character count: @count', ['@count' => $this->getSetting('counter_step')]);
$summary[] = $this->t('Warning text character count: @count', ['@count' => $this->getSetting('counter_total')]);
return $summary;
}

/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state): array {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$element['value']['#character_counter'] = TRUE;
$element['value']['#counter_step'] = $this->getSetting('counter_step');
$element['value']['#counter_total'] = $this->getSetting('counter_total');
return $element;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Drupal\hdbt_admin_tools\Plugin\Field\FieldWidget;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\text\Plugin\Field\FieldWidget\TextareaWidget;

/**
* Plugin implementation of the 'formatted_text_character_counter' widget.
*
* @FieldWidget(
* id = "formatted_text_character_counter",
* label = @Translation("Text area (formatted text, character counter)"),
* field_types = {
* "text_long",
* }
* )
*/
class FormattedTextCharacterCounterWidget extends TextareaWidget {

use CharacterCounterFieldWidgetTrait;

/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state): array {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$element['#character_counter'] = TRUE;
$element['#counter_step'] = $this->getSetting('counter_step');
$element['#counter_total'] = $this->getSetting('counter_total');
return $element;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Drupal\hdbt_admin_tools\Plugin\Field\FieldWidget;

use Drupal\Core\Field\Plugin\Field\FieldWidget\StringTextareaWidget;

/**
* Plugin implementation of the 'textarea_character_counter' widget.
*
* @FieldWidget(
* id = "textarea_character_counter",
* label = @Translation("Text area (character counter)"),
* field_types = {
* "string_long",
* }
* )
*/
class TextareaCharacterCounterWidget extends StringTextareaWidget {

use CharacterCounterFieldWidgetTrait;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Drupal\hdbt_admin_tools\Plugin\Field\FieldWidget;

use Drupal\Core\Field\Plugin\Field\FieldWidget\StringTextfieldWidget;

/**
* Plugin implementation of the 'textarea_character_counter' widget.
*
* @FieldWidget(
* id = "textfield_character_counter",
* label = @Translation("Textfield (character counter)"),
* field_types = {
* "string"
* }
* )
*/
class TextfieldCharacterCounterWidget extends StringTextfieldWidget {

use CharacterCounterFieldWidgetTrait;

/**
* {@inheritdoc}
*/
public static function defaultSettings(): array {
return [
'counter_step' => 0,
'counter_total' => 55,
] + parent::defaultSettings();
}

}
9 changes: 9 additions & 0 deletions modules/hdbt_admin_tools/translations/fi.po
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,12 @@ msgstr "Hero-lohko on pakollinen jos Hero-lohko -valintaruutu on valittuna. Pois

msgid "If you want to translate this page, do not change this value. You can translate this content from the 'Translate' -tab"
msgstr "Jos haluat luoda tästä sivusta uuden käännöksen, älä vaihda tätä arvoa, vaan luo uusi käännös 'Käännä'-välilehdeltä"

msgid "Text area (formatted text, character counter)"
msgstr "Tekstialue (muotoiltu, merkkimäärälaskuri)"

msgid "Text area (character counter)"
msgstr "Tekstialue (merkkimäärälaskuri)"

msgid "Textfield (character counter)"
msgstr "Tekstikenttä (merkkimäärälaskuri)"
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ content:
duplicate: '0'
third_party_settings: { }
field_lead_in:
type: string_textarea
type: textarea_character_counter
weight: 13
region: content
settings:
counter_step: 160
counter_total: 200
rows: 3
placeholder: ''
third_party_settings: { }
Expand Down Expand Up @@ -200,10 +202,12 @@ content:
display_label: true
third_party_settings: { }
title:
type: string_textfield
type: textfield_character_counter
weight: 1
region: content
settings:
counter_step: 0
counter_total: 55
size: 60
placeholder: ''
third_party_settings: { }
Expand Down
4 changes: 2 additions & 2 deletions modules/helfi_node_page/helfi_node_page.install
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ function helfi_node_page_update_9001() : void {
}

/**
* Set node as unpublished on default.
* UHF-9690 Added character counter to title and lead in.
*/
function helfi_node_page_update_9002() : void {
function helfi_node_page_update_9003() : void {
// Re-import configuration.
\Drupal::service('helfi_platform_config.config_update_helper')
->update('helfi_node_page');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,27 @@ dependencies:
- field.field.paragraph.hero.field_hero_title
- paragraphs.paragraphs_type.hero
module:
- allowed_formats
- hdbt_admin_tools
- media_library
- text
id: paragraph.hero.default
targetEntityType: paragraph
bundle: hero
mode: default
content:
field_hero_desc:
type: text_textarea
type: formatted_text_character_counter
weight: 3
region: content
settings:
counter_step: 160
counter_total: 200
rows: 5
placeholder: ''
third_party_settings: { }
third_party_settings:
allowed_formats:
hide_help: '0'
hide_guidelines: '0'
field_hero_design:
type: design_field_widget
weight: 0
Expand All @@ -44,6 +49,8 @@ content:
weight: 6
region: content
settings:
placeholder_url: ''
placeholder_title: ''
linkit_profile: helfi
linkit_auto_link_text: false
third_party_settings: { }
Expand All @@ -54,10 +61,12 @@ content:
settings: { }
third_party_settings: { }
field_hero_title:
type: string_textfield
type: textfield_character_counter
weight: 2
region: content
settings:
counter_step: 0
counter_total: 55
size: 60
placeholder: ''
third_party_settings: { }
Expand Down

This file was deleted.

This file was deleted.

2 changes: 2 additions & 0 deletions modules/helfi_paragraphs_hero/helfi_paragraphs_hero.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ dependencies:
- helfi_base_content:helfi_base_content
- helfi_image_styles:helfi_image_styles
- helfi_media:helfi_media
'interface translation project': helfi_paragraphs_hero
'interface translation server pattern': modules/contrib/helfi_platform_config/modules/helfi_paragraphs_hero/translations/%language.po
Loading
Loading