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

SHS-5515: Refactor vertical card main link code #1505

Merged
merged 1 commit into from
Apr 17, 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
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.