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.

1 Answer 1

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

6
  • Thanks @Lodder - although when I add this to a page, the page then gives a 404 error Feb 23, 2017 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, 2017 at 14:26
  • oddly, the parameters reset themselves to 'Please select your option' when you save the module Feb 23, 2017 at 14:30
  • Ok, so it's saving now, but added key_field="id" to the XML Feb 23, 2017 at 14:40
  • I've updated the XML in my answer. Try using that instead.
    – Lodder
    Feb 23, 2017 at 14:40

Your Answer

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

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