2

I am trying to develop a custom module where I have options to select 4 different Joomla articles.

In my module XML file, I have the following:

<field name="articleOne"
    type="sql"
    label="Select an article"
    query="SELECT id AS value, title AS articleOne FROM #__content"
/>

<field name="articleTwo"
    type="sql"
    label="Select an article"
    query="SELECT id AS value, title AS articleOne FROM #__content"
/>

<field name="articleThree"
    type="sql"
    label="Select an article"
    query="SELECT id AS value, title AS articleOne FROM #__content"
/>

<field name="articleFour"
    type="sql"
    label="Select an article"
    query="SELECT id AS value, title AS articleOne FROM #__content"
/>

I can get this to output the ID in the frontend. But how can I output the title, introtext and image?

I would like it to display in the frontend as follows:

enter image description here

2
  • Can you give us more of an idea about how would see this being displayed? Is it a series of article title/introtext/image combinations from which the user can select 4 using checkboxes? It seems a lot to put into a module. Or are you wanting an html select input field which allows the user to select 4? Again, it's all not going to fit into that. Commented Apr 28, 2020 at 8:13
  • Thanks, I've updated Commented Apr 28, 2020 at 8:28

1 Answer 1

1

As far as I know, the SQL form field will always return the ID of an article.

So you can get the title, into text and intro image, you can use Joomla's Table class:

use Joomla\CMS\Table\Table;

$array = ['articleOne', 'articleTwo', 'articleThree', 'articleFour'];

// Loop through each array item
foreach ($array as $value)
{
    $article = Table::getInstance('content'); 
    $article->load($params->get($value));
    $articleImages = json_decode($article->get('images'));

    // Check if an intro image exists
    if (isset($articleImages->image_intro))
    {
        echo '<img src="' . $articleImages->image_intro . '" alt="">';
    }
    echo $article->get('title');
    echo $article->get('introtext');
}

Hope this helps

2
  • Thanks @Lodder, this is great but it's only showing the first article? Commented Apr 29, 2020 at 10:53
  • @RustyJoomla Updated the code. Make sure you have selected an article for all four parameters
    – Lodder
    Commented Apr 29, 2020 at 11:33

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.