Skip to content

Commit

Permalink
fix(shs-5515): Refactor vertical card main link code (#1505)
Browse files Browse the repository at this point in the history
* fix(shs-5515): Refactor vertical card main link code.
  • Loading branch information
cienvaras authored Apr 17, 2024
1 parent d4a1b0d commit d5a8121
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<div class="hb-card__title">
{% set title_content %}
{% if href %}
<a href="{{ href }}" data-vertical-card-link>{{ title }}</a>
<a href="{{ href }}">{{ title }}</a>
{% else %}
{{ title }}
{% endif %}
Expand Down
19 changes: 3 additions & 16 deletions docroot/themes/humsci/humsci_basic/humsci_basic.theme
Original file line number Diff line number Diff line change
Expand Up @@ -242,29 +242,16 @@ function humsci_basic_preprocess_pattern_vertical_link_card(&$variables) {
function humsci_basic_preprocess_pattern_vertical_card(&$variables) {
$context = $variables['context'];
if ($context->isOfType('layout')) {
// Functionality for paragraphs
// Functionality for paragraphs.
_humsci_basic_make_title_the_link($variables);
} else {
// Functionality for Views.
$renderer = \Drupal::service('renderer');
$title_rendered = $renderer->renderPlain($variables['title']);

// Create a new DOMDocument instance and load the rendered title HTML.
$dom = new DOMDocument();
$dom->loadHTML($title_rendered);

// Create a DOMXPath instance to query the DOM.
$xpath = new DOMXPath($dom);

// Find all anchor elements in the DOM.
$links = $xpath->query('//a');

// If the markup is a link, set a new attribute to it.
if ($links->length >= 1) {
$links->item(0)->setAttribute('data-vertical-card-link', '');
$variables['title'] = ['#markup' => $dom->saveHTML()];
} else {
if (strpos($title_rendered, '<a') === FALSE) {
$original_title = $variables['title'];
// If the title is not linked, get the link from the button.
_humsci_basic_make_title_the_link($variables);
// Restore original title, because _humsci_basic_make_title_the_link() overwrote it.
$variables['title'] = $original_title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function verticalCard() {
// Loop through each card
cards.forEach((card) => {
// Find the main link within each card
const mainLink = card.querySelector('[data-vertical-card-link]');
const mainLink = card.querySelector('.hb-card__title a');

if (!mainLink) {
return;
Expand Down
2 changes: 1 addition & 1 deletion docroot/themes/humsci/humsci_colorful/js/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docroot/themes/humsci/humsci_traditional/js/index.js

Large diffs are not rendered by default.

0 comments on commit d5a8121

Please sign in to comment.