Здравствуйте, есть плагин, добавляющий к блоку автоматически кнопку подробнее, что очень удобно, но проблема в том что он почему-то добавляет побробнее не только к статьям в блоге, а и к описанию самой категории. Помогите пожалуйтса решить эту проблему.
<?php
defined('_JEXEC') or die;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\Component\Content\Site\Helper\RouteHelper;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Uri\Uri;
class PlgContentAmsupermore extends CMSPlugin
{
// Enable automatic language loading for the plugin
protected $autoloadLanguage = true;
/**
* Event triggered when Joomla prepares content for display.
*
* @param string $context The context of the content being passed to the plugin.
* @param object $article The article object.
* @param object $params The article parameters.
* @param int $page The page number (for multi-page articles).
*/
public function onContentPrepare($context, &$article, &$params, $page = 0)
{
// Check if the "Add Read More" feature is enabled
$addReadMore = $this->params->get('add', 0); // Default: on
if (!$addReadMore || $context !== 'com_content.category') {
return; // Exit if the feature is disabled or the context is not a category blog layout
}
// Load the CSS file
Factory::getDocument()->addStyleSheet(Uri::root(true). 'plugins/content/amsupermore/assets/css/amsupermore.css');
// Get plugin parameters
$truncate = $this->params->get('truncate', 'word'); // Default: Word
$limit = (int) $this->params->get('limit', 100); // Default: 100
$readmoreStyle = $this->params->get('style', 'link'); // Default: Link
$readmoreLabel = $this->params->get('label', 'Read More ...'); // Default: 'Read More ...'
$format = $this->params->get('format', 0);
$btnstyle = $this->params->get('btnstyle', ' btn-secondary');
// Remove existing readmore tags
$article->text = preg_replace('/<hr\s+id="system-readmore"\s*\/?>/i', '', $article->text);
if ($format) {
$article->text = strip_tags($article->text);
}
// Truncate the article content based on the specified limit
if ($truncate === 'word') {
$truncatedText = $this->truncateByWords($article->text, $limit);
} else {
$truncatedText = mb_substr($article->text, 0, $limit). '...';
}
// Add the new readmore tag only if truncation occurred
if ($truncatedText !== $article->text) {
// Generate the "Read More" link or button
if ($readmoreStyle === 'button') {
// Bootstrap button style
$readMoreTag = '<a href="' . $this->getArticleLink($article). '" class="btn'.$btnstyle.'">'.htmlspecialchars($readmoreLabel).'</a>';
} else {
// Default link style
$readMoreTag = '<a href="'.$this->getArticleLink($article).'">'.htmlspecialchars($readmoreLabel).'</a>';
}
// Suppress Joomla's default readmore generation
$article->alternative_readmore = ''; // Disable Joomla's default "Read More" text
$article->readmore = false; // Disable Joomla's default "Read More" behavior
// Append the readmore tag after the truncated text, wrapped in a <div> for a new line
$article->text = $truncatedText.'<div class="readmore">'.$readMoreTag.'</div>';
}
}
/**
* Truncate text by words.
*
* @param string $text The input text.
* @param int $limit The word limit.
* @return string The truncated text.
*/
private function truncateByWords($text, $limit)
{
$words = preg_split('/\s+/', strip_tags($text), $limit + 1);
if (count($words) > $limit) {
array_pop($words); // Remove the last word to avoid cutting mid-sentence
return implode(' ', $words). '...';
}
return $text;
}
/**
* Truncate text by characters.
*
* @param string $text The input text.
* @param int $limit The character limit.
* @return string The truncated text.
*/
private function truncateByCharacters($text, $limit)
{
$text = strip_tags($text); // Remove HTML tags
if (strlen($text) > $limit) {
$text = substr($text, 0, $limit);
$text = rtrim($text, " \t\n\r.,!?"). '...'; // Trim cleanly and add ellipsis
}
return $text;
}
/**
* Helper method to generate the article link.
*
* @param object $article The article object.
* @return string The article link.
*/
private function getArticleLink($article)
{
// Get the menu item ID for SEF URLs
$menu = Factory::getApplication()->getMenu();
$itemId = $menu->getActive()? $menu->getActive()->id : null;
// Generate the article route using RouteHelper
$link = Route::_(RouteHelper::getArticleRoute($article->slug, $article->catid, $itemId));
return $link;
}
}