2

I was able to compose a url from a subform that included text input fields. I was able to display the link because it was in the same menu

<i class="fa fa-link fa-1x" aria-hidden="true"></i></span> Liens ressources web externes </h3>
    <div>
    <a href="/index.php?option=com_content&view=article&id=<?php echo $this->item->jcfields[40]->rawvalue; ?>&catid=168&Itemid=156&lang=fr"><?php echo $this->item->jcfields[40]->value; ?></a>

...

When I do :

 echo $this->item->jcfields[40]->rawvalue;

I get

{"row0":{"field42":"120","field41":""}}

1/ how to retrieve the value "120" from the field "field42" to extract the value "XXX" ? 2/ how to get the category "YY" of the item (in this case the id=120), see the itemid "ZZ" ?

to make the link of type

<a href="/index.php?option=com_content&view=article&id=<?php echo $this->item->XXX; ?>&catid=YY&Itemid=ZZ&lang=fr"><?php echo $this->item->XXX; ?></a>

enter image description here

0

2 Answers 2

1

First you want to handle the JSON value: {"row0":{"field42":"120","field41":""}}

$field_value = json_decode('{"row0":{"field42":"120","field41":""}}');
$article_id = $field_value->row0->field42; // expected == 120

Thus, for you for getting the field value, it is going to be:

$field_value = json_decode($this->item->jcfields[40]->rawvalue);
$article_id = $field_value->row0->field42; // expected == 120

As you have the article ID then you can get the category ID and in few different ways you can build a route/URI:

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Helper\RouteHelper;


// converting the json value
$field_value = json_decode($this->item->jcfields[40]->rawvalue);    

$article_id = $field_value->row0->field42;    

$app = Factory::getApplication();
$factory = $app->bootComponent('com_content')->getMVCFactory();
$table = $factory->createTable('Article', 'Table');    
$table->load($article_id);
$item_catid = $table->catid;

$rhelper = new RouteHelper;
$myroute = $rhelper->getRoute($article_id, 'com_content.article', '', $language = ‘fr’, $catid = $item_catid);

This will create a value like:

'index.php?option=com_content&view=article&id=120&catid=12&lang=fr&Itemid=118'

Now, here comes the bad news: 'Itemid' would be a menu ID and the problem with that is that you cannot query that based on article ID or category ID since many menu items can point to an article or category item, so the router is creating always the menu Itemid where the route was requested/created.

Thus, either you hardcode a menu ID in the URI or just leave that out from the URI.

So better if you create it half manually (without using the RouteHelper) with continuing the code from above:

$Uri = new Joomla\CMS\Uri\Uri;

$full_link = $Uri::base() . "index.php?option=com_content&view=article&id=" . $article_id . "&catid=" . $item_catid . "&lang=fr"; 

echo $full_link;

Everything is possible of course, but because of the basic concept of menu items in relations with category and article items, with the many-to-one relation, I do not see the easy way of querying menu items and choosing the right one from the many possible to create the right menu 'Itemid' always for this URI. But maybe if you run on this question more you will find one solution on this part of the question too. I do not see that it worth the time though. I would approch this part of the question from another angle...

3
  • As requested, I created a new request, as my problem is not solved since I will need to have the right link generated in the subform loop: joomla.stackexchange.com/questions/33061/…
    – Hdcms
    May 22 at 8:12
  • @Hdcms - I hope I’ll have a bit more time and will be able to check and follow that question too (if you will not self-solve that until).
    – Zollie
    May 23 at 11:38
  • I still haven't solved this problem but I've added a debug hoping it helps! joomla.stackexchange.com/questions/33061/… Regards
    – Hdcms
    May 30 at 12:11
0

I'll post what I did with 2 remarks:

  1. I am in an overload, there was already use Joomla\CMS\Factory; and rather use Joomla\Component\Content\Site\Helper\RouteHelper; than use Joomla\CMS\Helper\RouteHelper;

  2. I didn't put the new router because I still have some code that it will become obsolete in a few years; is it really superior?


$rhelper = new RouteHelper;
$myroute = $rhelper->getRoute($article_id, 'com_content.article', '', 
$language = 'en', $catid = $item_catid);

my code:

$field_value = json_decode($this->item->jcfields[40]->rawvalue);
$article_id = $field_value->row0->field42;  
$app = Factory::getApplication();
$factory = $app->bootComponent('com_content')->getMVCFactory();
$table = $factory->createTable('Article', 'Table');    
$table->load($article_id);
$item_catid = $table->catid;
if ($item_catid == 16) { 
    $item_id = "156";
} elseif ($item_catid == 19) { 
    $item_id = "157";   
} elseif ($item_catid ==20) { 
    $item_id = "158"; 
} elseif ($item_catid == 21) {
    $item_id = "159";
}       
?>
<a href="/index.php?option=com_content&view=article&id=<?php echo $article_id;?>&catid=<?php echo $item_catid;?>&Itemid=<?php echo $item_id;?>&lang=fr"><?php echo $this->item->jcfields[40]->value; ?></a>
2
  • Content/…/RouteHelper is also OK, no problem with that at all, it is the dedicated helper on site/content but irrelevant from the point of the originial question and you do not need to use any RouteHelper for this to work, it is only good for having examples on routing… if a component has already a RouteHelper we can use that of course. It always depends on where you put your code.
    – Zollie
    May 13 at 6:31
  • The problem with hardcoding the menu items bound to category IDs is - if it is not a hobby website - that maybe later you add new items and new categories, even new menus and this code could fail on those. At the end of the if/elseif block there should be a default (else) fallback menu item ID too if you want to use this code without later troubles. I would avoid using this kind of items’ binding in the first place…
    – Zollie
    May 16 at 16:17

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.