Skip to content
Advertisement

Use the Joomla Language override to create custom tags

I have this piece of code that already runs on the pages I need this done. (The entire Joomla 3.9 site is a Frankenstein of custom code, components, etc. )

   $placeholder_country = JText::_('COM_ACME_PLACEHOLDER_COUNTRY_'.strtoupper(str_replace('-','_',$var['country'])));
    if($placeholder_country === 'COM_ACME_PLACEHOLDER_COUNTRY_'.strtoupper(str_replace('-','_',$var['country'])) || !$placeholder_country) $placeholder_country = '';

    $article->text = JString::str_ireplace("{%placeholder_country%}", $placeholder_country, $article->text);

I tried adding the following to see if I can get this to set the title the same way.

     $placeholder_country = JText::_('titlenick_'.strtoupper(str_replace('-','_',$var['country'])));
    if($placeholder_country === 'titlenick_'.strtoupper(str_replace('-','_',$var['country'])) || !$placeholder_country) $placeholder_country = '';

    $document->setTitle = JString::str_ireplace("{%placeholder_country%}", $placeholder_country, $article->text);

Meaning, I go into the Joomla Language overrides section, add the Language Constant, for example, “TITLENICK_PERU”

In the “text” Id enter the page title I want. “Cheap Peru vacations” for example.

EDIT:

Attempting to solve using the below answer from Anibal:

After said insight was given, progress was made.

I’ve confirmed this works

  $placeholder_nicktitle = JText::_('titlenick_'.strtoupper(str_replace('-','_',$var['country'])));
if($placeholder_nicktitle === 'titlenick_'.strtoupper(str_replace('-','_',$var['country'])) || !$placeholder_nicktitle) $placeholder_nicktitle = '';

$article->text = JString::str_ireplace("{%placeholder_nicktitle%}", $placeholder_nicktitle, $article->text);

Placing {%placeholder_nicktitle%} in the Joomla article caused the override to trigger and displays the text I typed in the override.

Now the question is how do I get this to work so that whatever text I type in the override “text” field will end up being the page title?

I imagine this needs to change

$article->text = JString::str_ireplace("{%placeholder_nicktitle%}", $placeholder_nicktitle, $article->text);

It was a long shot, but I tried replacing the above line with this ( and it threw a 500 error)

$document =& JFactory::getDocument();
$document->setTitle("JString::str_ireplace(". 
{%placeholder_nicktitle%}", $placeholder_nicktitle, $article- 
 >text);");

Side note: Right now the page gets it’s title from the Joomla article title itself “{%country_name%} cheap vacations” is what we have in there for example. Custom coded I believe. I tried adding {%placeholder_nicktitle%} in the Joomla article title and that didn’t work for some reason

Advertisement

Answer

There are several points to observe in your piece of code. So, this is more a code review than an answer to a question.

The first part of the code generates a LABEL, and then the second part uses the Joomla Language system to generate the translated text.

First, I would confirm if the PHP code generates the expected label: COM_ACME_PLACEHOLDER_COUNTRY_PERU or TITLENICK_PERU.

Then, the easy part, confirm that if you write: JString::str_ireplace("{%placeholder_country%}", $placeholder_country, $article->text); when the label is correctly generated; it gets replaced as you define in the Language Overrides section.

In case of doubt, the Joomla function JString::str_ireplace is just a wrapper of the PHP function so that you can check the original documentation here: https://www.php.net/str_ireplace

To sum up, the problem seems to be caused by the added complexity of the two parts. If you divide them, you can solve each part separately.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement