2

I am writing a module to allow admins to select upto 3 different articles (taken from Joomla Content) to show in the frontend of a site.

I want to show the article title and the intro image, along with a link on both of them to the article.

I have written the following in the module XML file, which shows the article list in the module backend:

<fields name="params">
    <fieldset name="basic">
        <field
            name="article_title_1"
            type="sql"
            default="10"
            label="Select an article"
            query="SELECT id, title FROM #__content"
            value_field="title">
            <option value="">Please select your option</option>
        </field>
    </fieldset>
</fields>

But in my default.php file, when I use the following

<?php echo $params->get('article_title_1') ; ?>

This only shows the article ID, and not the title.

2

Firstly, I'll start off with a side note. I'm assuming you're doing this in Joomla 3.6, which is fine, but I tested it in Joomla 3.7 and it returns a PHP error, unless you specific value_field="title", so be sure you use this instead:

<field
    name="article_title_1"
    type="sql"
    default="10"
    label="Select an article"
    query="SELECT id, title FROM #__content"
    key_field="id"
    value_field="title"
/>

The query itself will provide you a dropdown list of articles, however the <option> values will be the ID of the article, hence why echo $params->get('article_title_1'); only gives you the ID.

You can then use the following to get the title and intro image:

$id      = $params->get('article_title_1');
$article = JControllerLegacy::getInstance('Content')->getModel('Article')->getItem($id);
$image   = json_decode($article->images);

echo '<a href="' . JRoute::_('index.php?option=com_content&view=article&id=' . $id) . '">' . $article->title . '</a>';

if ($image !== null)
{
    echo '<img src="' . $image->image_intro . '" alt="">';
}

Hope this helps

  • Thanks @Lodder - although when I add this to a page, the page then gives a 404 error – RustyJoomla Feb 23 '17 at 14:23
  • Have you selected an article in the parameters? If so, what happes if you add var_dump($article); exit;? Does information about the article get dumped? – Lodder Feb 23 '17 at 14:26
  • oddly, the parameters reset themselves to 'Please select your option' when you save the module – RustyJoomla Feb 23 '17 at 14:30
  • Ok, so it's saving now, but added key_field="id" to the XML – RustyJoomla Feb 23 '17 at 14:40
  • I've updated the XML in my answer. Try using that instead. – Lodder Feb 23 '17 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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