Manipulating the subject, adding links

Started by spiros, January 16, 2022, 11:53:09 AM

Previous topic - Next topic

spiros

I want to linkify the subject, for example, words between commas become a link (for example this topic's subject would become: Manipulating the subject, adding links). Is there a hook for that or does it need to be done on a specific file(s)?

Aleksi "Lex" Kilpinen

There are places where the subject is linked automatically to the topic, how do you figure this would work?
Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

How you can help SMF

spiros

Only the highlighted position should be affected. (Display.template.php?)

Mick.

Open Display.template.php

Find:
// Show new topic info here?
echo '
<div id="display_head" class="information">
<h2 class="display_title">
<span id="top_subject">', $context['subject'], '</span>', ($context['is_locked']) ? ' <span class="main_icons lock"></span>' : '', ($context['is_sticky']) ? ' <span class="main_icons sticky"></span>' : '', '
</h2>
<p>', $txt['started_by'], ' ', $context['topic_poster_name'], ', ', $context['topic_started_time'], '</p>';

Add the message link inside the h2 tags...

// Show new topic info here?
echo '
<div id="display_head" class="information">
<h2 class="display_title">
<a href="', $message['link'], '"><span id="top_subject">', $context['subject'], '</span>', ($context['is_locked']) ? ' <span class="main_icons lock"></span>' : '', ($context['is_sticky']) ? ' <span class="main_icons sticky"></span></a>' : '', '
</h2>
<p>', $txt['started_by'], ' ', $context['topic_poster_name'], ', ', $context['topic_started_time'], '</p>';

See if this does it for ya.

Kindred

Mick, if I understood correctly,  spiris wants multiple links to different places within the same subject line.
What you posted won't do that...  actually, there is nothing that will do that without some major reprocessing of the subject before display.
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

spiros

I guess the subject need to be parsed somehow and create an array with the results which will then become linkified, i.e. for Google search.

Kindred

Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

spiros

Lol, just guesswork, from your answers I gather there is no hook for that?
I do have a google BBC tag though, can it be made to work for that purpose?

Kindred

Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Arantor

To answer the first question, no there is no hook for this and in any case none are recommended (because attempting to modify $context['subject'] in a hook to produce the effect will break quick reply)

Additionally, the code your Display.template.php has is pretty customised, it doesn't look like either 2.0 or 2.1's which isn't going to help.

But I believe I can at least simplify the situation you're dealing with so that you can insert the code yourself.

At the start of template_main, just after the global definitions, you can add the following:

$context['subject_as_links'] = [];
$parts = explode(',', $context['subject']);
foreach ($parts as $part) {
    $part = trim($part);
    $linkable = html_entity_decode($part, ENT_QUOTES, 'UTF-8');
    $linkable = preg_replace('/\s+/iu', ' ', $linkable);
    $context['subject_as_links'][] = '<a href="https://www.google.com/?q="' . urlencode($linkable) . '">' . $part . '</a>';
}

$context['subject_as_links'] = implode(', ', $context['subject_as_links']);

The idea is to split the string up into its parts based on the commans, then remove any spaces at the start or end, convert it into a format suitable for encoding, actually encoding to make a set of links and then making a single final thing out of it so you can just echo $context['subejct_as_links'] wherevr you want it shown.

Note I haven't tested it but it seems simple enough.

spiros

Wow, a stroke of genius! I really envy coders and the way their brain can work to find a solution (maybe because I am so ignorant, lol).
Yeah, my display template is Sunrise theme by Sycho with some customisation.

Steve

DO NOT pm me for support!

Advertisement: