Keyword

I've created this plugin for K2

  • Martin Thorgaard
  • Martin Thorgaard's Avatar Topic Author
  • Offline
  • Senior Member
More
1 year 4 weeks ago - 1 year 4 weeks ago #180869 by Martin Thorgaard
I've created this plugin for K2 was created by Martin Thorgaard
I made a plugin that should fetch the title and thumbnail from another K2 article on my website when I enter this in an article:
{link="URL"}
It did work prior updating k2, but now its just blank in the article.
I might add, I also updated to php version 8.0.28 before updatiing K2, but the code should be compatible.
This is my (prior working) php code, I hope someone can help me finding the problem:

<?php
// No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.plugin.plugin');

class plgK2K2RelatedLinks extends JPlugin
{
    public function __construct(&$subject, $config)
    {
        parent::__construct($subject, $config);
    }

    public function onK2BeforeDisplay(&$item, &$params, $limitstart)
    {
        $regex = '#{link="(.*?)"}#s';
        preg_match_all($regex, $item->text, $matches, PREG_SET_ORDER);

        if ($matches) {
            foreach ($matches as $match) {
                $link = $match[1];
                $linkContent = $this->fetchLinkData($link);

                if ($linkContent) {
                    $replacement = '<div class="related-link-thumbnail">'
                    . '<a href="' . $link . '">'
                    . '<img src="' . $linkContent . '" alt="' . $linkContent . '"/>'
                    . '<div class="related-link-text">'
                    . '<span class="read-also">Læs også</span>'
                    . '<h2>' . $linkContent . '</h2>'
                    . '</div>'
                    . '</a>'
                    . '</div>';

                    $item->text = str_replace($match[0], $replacement, $item->text);
                } else {
                    $item->text = str_replace($match[0], '', $item->text);
                }
            }
        }
    }

    private function fetchLinkData($url)
    {
        $data = ;

        $html = @file_get_contents($url);

        if ($html === false) {
            error_log('Error fetching data from URL: ' . $url);
            return false;
        }

        libxml_use_internal_errors(true);
        $dom = new DOMDocument;
        $dom->loadHTML($html);
        libxml_clear_errors();

        $xpath = new DOMXPath($dom);

        
        $titleNode = $xpath->query('//h1[@class="itemTitle"]')->item(0);
        $imageNode = $xpath->query('//meta[@property="og:image"]')->item(0);

        if ($titleNode && $imageNode) {
         $title = $titleNode->nodeValue;
         $title_parts = explode('Skrevet af', $title);
         $title = trim($title_parts[0]);
         $data = $title;
         $data = $imageNode->getAttribute('content');
        } else {
            error_log('Error parsing data from URL: ' . $url);
            return false;
        }

        return $data;
    }
}
?>



Sincerely,
Martin
Last edit: 1 year 4 weeks ago by Martin Thorgaard.

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum