0

The following code use to work for generating variables for open graph data, but when I updated to Joomla 4 I now get an error because JRequest is deprecated.

$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
if ($option=="com_content" && $view=="article") {
    $ids = explode(':',JRequest::getString('id'));
    $article_id = $ids[0];
    $article =& JTable::getInstance("content");
    $article->load($article_id);
    $theArticle = $article->get("title");
    $theImages = $article->get("images");   
    $pictures = json_decode($theImages); // Split the parameters apart
    $timage= "http://mywebsite.../".$pictures->{'image_intro'};
}
$doc =& JFactory::getDocument();
$doc->addCustomTag( '
<meta name="twitter:title" content="'.mb_strimwidth(strip_tags($theArticle),0,225, " ...").'"=""/>
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="yoursite">
<meta name="twitter:creator" content="yourcreator">
<meta name="twitter:url" content="'.JURI::current().'">
<meta name="twitter:description" content="'.mb_strimwidth(strip_tags($doc->getMetaData( 'description' )),0,225, " ...").'"=""/>
<meta name="twitter:image" content="'.$timage.'">

<meta property="og:title" content="'.$this->escape($this->item->title).'"/>
<meta property="og:type" content="article"/>
<meta property="og:email" content="";/>
<meta property="og:url" content="'.str_replace('" ','&quot;',juri::current()).'"="">
<meta property="og:image" content="'.$timage.'"/>
<meta property="og:site_name" content=""/>
<meta property="og:description" content="'.strip_tags($this->item->introtext).'"/>
');

How can I rewrite this to work with Joomla 4?

2 Answers 2

1

In this instance you would replace JRequest with application input.

$JInput = Factory::getApplication()->input;
$view = $JInput->get('view','','string');
echo $view;

Sorry for my short answer but I'm traveling. this should point you in the right direction. Welcome to the JSE.

1

JRequest had been deprecated since Joomla 3, so the documentation available for Joomla 3 is still valid for Joomla 4.

If you want to convert the old classes to the namespaces you can use this script or use the list as cheatsheet.

For a comprehensive list of deprecated classes, removed classes and other stuff that has changed between J3.10 and J4 you should read the list of potential backward compatibility issues.

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.