I have a custom component where I was to be able to select other articles to show in the frontend. I have thought the best way to do this would be through the SQL field.
This is good that it gives me the ID of the article, but I want to be able to display the article name, intro image and intro text in the frontend of my custom component.
I have the following in my component XML
<field name="interested_in" type="sql" default="10" label="Select an article" query="SELECT id, title FROM #__content" key_field="id" value_field="title" />
This echo's the ID:
<?PHP echo $this->item->interested_in; ?>
But ideally, I need something along the lines of:
<?php
$id = $this->item->interested_in;
$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="">';
}
?>
But this is giving me an error in the frontend:
Call to a member function getItem() on bool
name="interested_in"
however you confirmed that$this->item->interest_in
echoes the ID. Note the difference betweeninterested_in
andinterest_in
. Was this a typo in the question or if there a mismatch?interested_in
is used in both the XML and PHP