2

For the native Joomla module "Similar Tags", we have only the article title. How to have also the introtext and the intro image?

Here is the native code with my code tests :

defined('_JEXEC') or die;

JLoader::register('TagsHelperRoute', JPATH_BASE . '/components/com_tags/helpers/route.php');

if ($list) : ?>
    <ul class="tagssimilar">
    <?php foreach ($list as $i => $item) : ?>
        <?php if (empty($item->core_title)) continue; ?>
        <li>
        <?php if (($item->type_alias == 'com_users.category') || ($item->type_alias == 'com_banners.category')) : ?>
            <?= htmlspecialchars($item->core_title, ENT_COMPAT, 'UTF-8') ?>
        <?php else: $item->route = new JHelperRoute; ?>
            <a href="<?= JRoute::_(TagsHelperRoute::getItemRoute($item->content_item_id, $item->core_alias, $item->core_catid, $item->core_language, $item->type_alias, $item->router)) ?>">
                <?= htmlspecialchars($item->core_title, ENT_COMPAT, 'UTF-8') ?>
            </a>
        <?php endif

My first attempt (part 1): Modify Joomla Tags Result Page

    //image
    $images  = json_decode($item->core_images);
    echo '<p><img class="intro-image" src="'.$images->image_intro.'"></p>';                  
    //introtext
    $limit =300;
    if (strlen($item->text) > $limit) {
        echo (substr($item->text, 0, $limit)) ?> <a href="<?php echo JRoute::_(TagsHelperRoute::getItemRoute($item->content_item_id, $item->core_alias, $item->core_catid, $item->core_language, $item->type_alias, $item->router)); ?>">...</a>
        <?php
    }
    else {
        echo $item->text;
    }

My first attempt (part 2) : Modify Joomla Tags Result Page

//introtext
echo $item->text;
//image
$images  = json_decode($item->core_images);
echo '<img src="'.$images->image_intro.'">';
//get category name by id
$db = JFactory::getDbo();
$id = $item->core_catid;
$db->setQuery("SELECT cat.title FROM #__categories cat WHERE cat.id='$id'");
$category = $db->loadResult();
echo '<p>'.$category.'</p>';

My second attempt: Adding Images to Latest Articles module ?>

    // This gets the image data associated with the article and processes it so PHP can use it properly.
    $images = json_decode($item->images);

    // This checks the Intro Image exists and isn't empty.
    if (isset($images->image_intro) && !empty($images->image_intro)) :
        // This displays it.
        ?>
        <div class="thumbnail item-image">
            <img src="<?php echo htmlspecialchars($images->image_intro); ?>" alt="<?php echo htmlspecialchars($images->image_intro_alt); ?>" itemprop="image"/>
        </div>
    <?php endif; ?>

My third attempt: https://www.joomlashack.com/blog/joomla/most-read-intro-images/

$images = json_decode($item->images);
?>  
<div class="span4">
    <?php if( $images->image_intro ) : ?>
        <img src="/<?php echo $images->image_intro; ?>" alt="<?php echo htmlspecialchars($item->title); ?>" />
    <?php endif; ?>

    <a href="/<?php echo $item->link; ?>">
        <?php echo $item->title; ?></a>
</div>

...and the end of the first code block...

    </li>
<?php endforeach ?>
</ul>
<?php else : ?>
<span><?= JText::_('MOD_TAGS_SIMILAR_NO_MATCHING_TAGS') ?></span>
<?php endif ?>
7
  • This question is incredibly light on details, clarity, and context. Please improve your question asap by explaining what you've tried to self-solve, what you are currently getting as output and what your desired output is. Your post has been flagged by the system as "Low Quality". Commented Nov 16, 2018 at 20:59
  • I completed the post but I can't explain more Commented Nov 17, 2018 at 9:24
  • Please take the Joonla Stack Exchange tour and learn about how to ask for support here. I don't want you to go away, but your question does still need more added to it. Specifically, questions seeking coding support are expected to supply evidence of effort. Have you attempted to write any code? Have you done any research? Unless you tell us, we have to assume that you haven't and you are expecting free work to be done for you. Again, we want to help you, but your question isn't yet meeting the expected standard. Keep going. Commented Nov 17, 2018 at 9:30
  • Hello I understand, yes I have search before many solutions. So I put my testing code. But I can't post the entire post. The code are not entire I don't understand how to put my entire code ? Commented Nov 17, 2018 at 11:38
  • Thank you for adding your efforts to the page. I personally find your coding standard to be very hard to read because of the colon-syntax of your if and foreach loops AND the constant bouncing between <?php and ?> In the future please make every attempt to clean up your code and practice consistent tabbing to make your question more attractive to answer. Now, with your coding attempts, which once showed some promise / partial success? What errors did they cause. I am not going to be the one to answer this question, but those who are able will have an easier time if you keep clarifying. Commented Nov 17, 2018 at 22:17

1 Answer 1

0

In the template override, before the item loop query the database to get the data you need. This will return an array of objects indexed on core_content_id.

$db = JFactory::getDbo();
$query = $db->getQuery(true)
    ->select($db->quoteName(array('core_content_id', 'core_images', 'core_body')))
    ->from($db->quoteName('#__ucm_content'))
    ->where($db->quoteName('core_content_id') . ' IN (' . implode(',', array_column($list, 'core_content_id')) . ')');
$extraData = $db->setQuery($query)->loadObjectList('core_content_id');

Then, inside the item loop, you can use the data in a few different ways. You can merge the item with the data:

$item = (object) array_merge((array) $item, (array) $extraData[$item->core_content_id]);

Or you can assign the properties separately:

$item->images    = $extraData[$item->core_content_id]->core_images;
$item->introtext = $extraData[$item->core_content_id]->core_body;

Or you can use the data directly:

<?php // Display introtext ?>
<?php echo $extraData[$item->core_content_id]->core_body; ?>

<?php // Display intro image ?>
<?php $images = json_decode($extraData[$item->core_content_id]->core_images); ?>
<?php if (!empty($images->image_intro)) : ?>
    <img src="<?php echo htmlspecialchars($images->image_intro, ENT_QUOTES, 'UTF-8'); ?>" alt="<?php echo htmlspecialchars($images->image_intro_alt, ENT_QUOTES, 'UTF-8'); ?>">
<?php endif; ?>

By the way, I can see this feature being implemented in core. If you agree, you can try submitting a feature request to the issue tracker.

3
  • Thanks a lot for your help Sharky it works fine ! >>>By the way, I can see this feature being implemented in core. If you agree, you can try submitting a feature request to the issue tracker. I don't know how to do that. I am not an expert. I will be nice of course if all native module can have these possibilities for each module in the module admin configuration Commented Nov 21, 2018 at 9:05
  • Just another : in my article override I add that : <?php echo JText::_('MOD_TAGS_SIMILAR_NO_MATCHING_TAGS'); ?> but how to add code to display the module only if module is present on this page ? Commented Nov 21, 2018 at 9:11
  • I find, it's <?php if($this->countModules('mymodule')) : ?> Commented Nov 23, 2018 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.