Skip to content

Commit

Permalink
Merge pull request #152 from City-of-Helsinki/UHF-8719
Browse files Browse the repository at this point in the history
UHF-8719: Group service channels by type
  • Loading branch information
teroelonen committed Sep 1, 2023
2 parents 0fd5ff1 + 13329c4 commit f73c27c
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 4 deletions.
7 changes: 7 additions & 0 deletions helfi_tpr.module
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ function helfi_tpr_theme() : array {
'tpr_ontology_word_details_form' => [
'render element' => 'form',
],
'tpr_service_channel_group' => [
'variables' => [
'label' => NULL,
'content' => NULL,
'type' => NULL,
],
],
];
}

Expand Down
21 changes: 21 additions & 0 deletions src/Entity/ChannelType.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,25 @@ public function label() : TranslatableMarkup {
};
}

/**
* Gets the type label.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* The type label.
*/
public function getTypeLabel() : TranslatableMarkup {
return match($this->id) {
self::MAIL => new TranslatableMarkup('Send us mail', [], ['context' => 'Service channel type verb']),
self::EMAIL => new TranslatableMarkup('Send us an email', [], ['context' => 'Service channel type verb']),
self::TELEFAX => new TranslatableMarkup('Send us a telefax', [], ['context' => 'Service channel type verb']),
self::TELEPHONE => new TranslatableMarkup('Call us', [], ['context' => 'Service channel type verb']),
self::SMS => new TranslatableMarkup('Send a text message', [], ['context' => 'Service channel type verb']),
self::LOCAL => new TranslatableMarkup('Visit us', [], ['context' => 'Service channel type verb']),
self::CHAT => new TranslatableMarkup('Ask in the chat', [], ['context' => 'Service channel type verb']),
self::ESERVICE => new TranslatableMarkup('Use the online service', [], ['context' => 'Service channel type verb']),
self::WEBPAGE => new TranslatableMarkup('Online information', [], ['context' => 'Service channel type verb']),
self::PRINTABLE_FORM => new TranslatableMarkup('Fill in a form', [], ['context' => 'Service channel type verb']),
};
}

}
21 changes: 17 additions & 4 deletions src/Plugin/Field/FieldFormatter/ServiceChannelFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,26 @@ public function viewElements(FieldItemListInterface $items, $langcode) : array {
$elements = parent::viewElements($items, $langcode);
$channelTypes = $this->getChannelTypes();

foreach ($elements as $delta => $element) {
$grouped = [];
foreach ($elements as $element) {
/** @var \Drupal\helfi_tpr\Entity\Channel $entity */
$entity = $element['#tpr_service_channel'];
$elements[$delta]['#weight'] = $channelTypes[$entity->getType()]->weight;
$type = $entity->getType();
$channelType = $channelTypes[$type];

if (!isset($grouped[$type])) {
$grouped[$type] = [
'#theme' => 'tpr_service_channel_group',
'#label' => $channelType->getTypeLabel(),
'#weight' => $channelType->weight,
'#type' => strtolower($type),
];
}

$grouped[$type]['#content'][] = $element;
}
uasort($elements, [SortArray::class, 'sortByWeightProperty']);
return $elements;
uasort($grouped, [SortArray::class, 'sortByWeightProperty']);
return array_values($grouped);
}

}
4 changes: 4 additions & 0 deletions templates/tpr-service-channel-group.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
<h3>{{ label }}</h3>
{{ content }}
</div>
2 changes: 2 additions & 0 deletions tests/src/Unit/ChannelTypeCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public function testIterator() : void {
* @covers ::count
* @covers \Drupal\helfi_tpr\Entity\ChannelType::__construct
* @covers \Drupal\helfi_tpr\Entity\ChannelType::label
* @covers \Drupal\helfi_tpr\Entity\ChannelType::getTypeLabel
*/
public function testGetLabel() : void {
$sut = ChannelTypeCollection::all();
Expand All @@ -146,6 +147,7 @@ public function testGetLabel() : void {
$index = 0;
foreach ($sut as $item) {
$this->assertInstanceOf(TranslatableMarkup::class, $item->label());
$this->assertInstanceOf(TranslatableMarkup::class, $item->getTypeLabel());
$index++;
}
$this->assertEquals($total, $index);
Expand Down
40 changes: 40 additions & 0 deletions translations/fi.po
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,43 @@ msgstr "Muualla verkossa"

msgid "Other contact information"
msgstr "Muut yhteystiedot"

msgctxt "Service channel type verb"
msgid "Use the online service"
msgstr "Asio verkossa"

msgctxt "Service channel type verb"
msgid "Fill in a form"
msgstr "Täytä lomake"

msgctxt "Service channel type verb"
msgid "Call us"
msgstr "Soita"

msgctxt "Service channel type verb"
msgid "Send us an email"
msgstr "Lähetä sähköpostia"

msgctxt "Service channel type verb"
msgid "Send us mail"
msgstr "Lähetä postia"

msgctxt "Service channel type verb"
msgid "Ask in the chat"
msgstr "Kysy chatissa"

msgctxt "Service channel type verb"
msgid "Visit us"
msgstr "Asioi paikan päällä"

msgctxt "Service channel type verb"
msgid "Online information"
msgstr "Tietoa verkossa"

msgctxt "Service channel type verb"
msgid "Send a text message"
msgstr "Lähetä tekstiviesti"

msgctxt "Service channel type verb"
msgid "Send us a telefax"
msgstr "Lähetä faksia"
40 changes: 40 additions & 0 deletions translations/sv.po
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,43 @@ msgstr "På webben"

msgid "Other contact information"
msgstr "Andra kontaktuppgifter"

msgctxt "Service channel type verb"
msgid "Use the online service"
msgstr "Uträtta ärenden på webben"

msgctxt "Service channel type verb"
msgid "Fill in a form"
msgstr "Fyll i blankett"

msgctxt "Service channel type verb"
msgid "Call us"
msgstr "Ring"

msgctxt "Service channel type verb"
msgid "Send us an email"
msgstr "Skicka e-post"

msgctxt "Service channel type verb"
msgid "Send us mail"
msgstr "Skicka post"

msgctxt "Service channel type verb"
msgid "Ask in the chat"
msgstr "Fråga i chatten"

msgctxt "Service channel type verb"
msgid "Visit us"
msgstr "Sköta ärenden på plats"

msgctxt "Service channel type verb"
msgid "Online information"
msgstr "Information på webben"

msgctxt "Service channel type verb"
msgid "Send a text message"
msgstr "Skicka textmeddelande"

msgctxt "Service channel type verb"
msgid "Send us a telefax"
msgstr "Skicka fax"

0 comments on commit f73c27c

Please sign in to comment.