Skip to content

Commit

Permalink
Display tag items as pills
Browse files Browse the repository at this point in the history
We already had this styling in our FollowTag formatting, but it's
actually a universal design pattern for tags in Open Social that they're
displayed as small pills. To accomplish this we add the classes in our
TagSplitFormatter so that themes can use this.

This is accompanied by a field file change in socialbase that ensures
the main label for the profile tag field is not shown but the individual
labels of the subfields are rendered as labels instead.
  • Loading branch information
Kingdutch committed Aug 22, 2023
1 parent 17f93c1 commit 957aa19
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,30 @@ function (EntityReferenceItem $item) use ($parent_id) {
protected function viewHierarchy(TermInterface $parent, EntityReferenceFieldItemListInterface $items, string $langcode) : array {
return [
'label' => ["#plain_text" => $parent->label()],
'items' => parent::viewElements($items, $langcode),
'items' => array_map(
function (array $element) {
// Move any '#options.attributes' to '#attributes' which makes for a
// more ergonomic DX for extending formatters.
$element['#attributes'] = $element['#options']['attributes'] ?? [];
unset($element['#options']['attributes']);

if (!isset($element['#attributes']['class'])) {
$element['#attributes']['class'] = [];
}

// In Open Social our tags are shown as pills, so we add the badge
// classes that allow styling this way.
$element['#attributes']['class'] += [
'badge',
'badge-default',
'badge--large',
'badge--pill',
];

return $element;
},
parent::viewElements($items, $langcode)
),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ protected function viewHierarchy(TermInterface $parent, EntityReferenceFieldItem
$classes[] = "term-followed";
}
$classes += [
'badge',
'badge-default',
'badge--large',
'badge--pill',
'btn-action__term',
];
$field['items'][$delta] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ content:
field_profile_profile_tag:
type: social_tag_split
weight: 6
label: visually_hidden
label: inline
settings:
link: true
third_party_settings: { }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
core.entity_view_display.profile.profile.default:
expected_config:
content:
field_profile_profile_tag:
label: visually_hidden
update_actions:
change:
content:
field_profile_profile_tag:
label: inline
18 changes: 18 additions & 0 deletions modules/social_features/social_profile/social_profile.install
Original file line number Diff line number Diff line change
Expand Up @@ -2036,3 +2036,21 @@ function social_profile_update_12025() : string {
// Output logged messages to related channel of update execution.
return $updater->logger()->output();
}

/**
* Ensure profile tag labels are displayed inline.
*/
function social_profile_update_12026() : string {
// The profile tag field is shown as individual fields but they respect the
// label display set on the parent (even though the label itself is never
// rendered). Thus the label must be inline to ensure that happens for every
// individual "subfield".
/** @var \Drupal\update_helper\Updater $updater */
$updater = \Drupal::service('update_helper.updater');

// Execute configuration update definitions with logging of success.
$updater->executeUpdate('social_profile', 'social_profile_update_12026');

// Output logged messages to related channel of update execution.
return $updater->logger()->output();
}

0 comments on commit 957aa19

Please sign in to comment.