Simple PHP Expression Not Working As Expected In...

Search
Other Bug Reports questions
Forum

Simple PHP Expression Not Working As Expected In Conditional Content

Bruce Scherzinger's Avatar Bruce Scherzinger
I am trying to create a PHP condition based on the existence of an <hr class="system-pagebreak"> tag existing in the article in which it is contained.

The expression I am using is the following:
{show contentpagetypes="article" php="return strpos($article->fulltext,'system-pagebreak') !== false;" devices="!mobile"}
Upon saving, JCE renders the above as follows:
{show contentpagetypes="article" php="return strpos($article&gt;fulltext,'system-pagebreak') !== false;" devices="!mobile"}
The result is as if the php condition always returns false, but that should not be the case. The way all of our articles are formed, if the article contains at least one of this type of <hr> tag, it must also contain a system-readmore <hr> tag and all the system-pagebreak <hr> tags must follow it. That means we only have to look in $article->fulltext for the string.

What am I doing wrong?

Thanks in advance,
Bruce
Peter van Westen's Avatar Peter van Westen ADMIN
JCE is mesding up your code. See if you can change settings to solve it. Or ask them for support.
Please post a rating at the Joomla! Extensions Directory
Bruce Scherzinger's Avatar Bruce Scherzinger
Hey Peter,

I edited the code directly in the database, so the editor is no longer involved. It still does not work. Below is the entire Custom Module code. Its function is to relocate the Article Index for articles that use the 'system-pagebreak' <hr> class, which is what creates the index, into this module using Javascript. It does work without this php condition.
<script>
	(function() {
		document.addEventListener('DOMContentLoaded', function() {
			var ai = document.getElementsByClassName('article-index');
			var target = document.getElementById('mod_article_index');
			target.appendChild(ai[0]);
		});
	})();
</script>
{show contentpagetypes="article" devices="!mobile" php="return strpos($article->fulltext,'system-pagebreak') !== false;"}
<div style="padding-top: 10px;">
	{accordion title="Article Index" color="orange" open="false"}
	<div id="mod_article_index" style="margin: 0; padding: 0;">&nbsp;</div>
	{/accordions}
</div>
{/show}
Is there something else I can try?

Thanks,
Bruce
Bruce Scherzinger's Avatar Bruce Scherzinger
I also tried adding a computed column to the #__content table that uses the SQL function INSTR to look in the fulltext column for 'system-pagebreak'. The column accurately contains a 1 for all the articles that contain that keyword. So I changed the code to just return the value of this new column, but that didn't work.

As an alternative approach I tried creating a custom condition group that uses the Article Content Keywords method. This didn't work either, but I suspect that might be because it looks in the rendered page(?), not in the raw HTML code. So I also tried using the Article Meta Keywords method. I rather expected that one to work. I added some keywords to one of my articles to test it, but this didn't work either. Getting frustrated.
Peter van Westen's Avatar Peter van Westen ADMIN
There is no '<hr class="system-pagebreak">' inside the fulltext. Joomla doesn't save the '<hr class="system-pagebreak">' to the content in the database. It splits the content and save the stuff before it as the introtext and the stuff after it as the fulltext.

If you want to check if there is a 'readmore' in the content, simply check if the $article->fulltext is not empty.
Please post a rating at the Joomla! Extensions Directory
Bruce Scherzinger's Avatar Bruce Scherzinger
Yes there absolutely is because we always insert those after the system-readmore <hr> tag. As I said, I added a computed column to #__content and it's finding 'system-pagebreak' in all the right articles. I'm specifically looking for articles that have the <hr class=system-pagebreak'> tag. We use them to index our long articles.

Here's a link to a page that has quite a few of them.
hixnews.com/news/newsletters/2020-2029/2...24-volume-24-issue-2
Peter van Westen's Avatar Peter van Westen ADMIN
Sorry, I misread and misinterpreted. I was thinking of the '<hr id="system-readmore" />'.


I just tried placing this in the introtext of an aricle, which has a readmore (so a fulltext) and page breaks in it:
{show php="return strpos($article->fulltext,'system-pagebreak') !== false;"}
HAS PAGEBREAKS
{/show}

And it is working as expected.
Please post a rating at the Joomla! Extensions Directory
Bruce Scherzinger's Avatar Bruce Scherzinger
I don't doubt you at all, Peter. But I am wondering why it doesn't work for me. In fact, I tried those other non-php methods that didn't work either. I am wondering if there's some site configuration that could be interfering? Not sure if uninstalling/reinstalling the plugin would do anything. Still hoping for some suggestions.

Thanks,
Bruce
Bruce Scherzinger's Avatar Bruce Scherzinger
I just realized the difference between what you are doing and what I am doing. In my case, I have the conditional logic in a Custom HTML module that isn't embedded inside an article. So my PHP code will have to query the database itself to make this work, if that's even possible. I will have to see if I can figure out the code to determine the alias of the article that is creating the index being relocated to the module.

This makes sense now. It's not an issue with the plugin at all.

Thanks for the discussion, Peter. Sometimes that's what it takes to make me see things clearly.

Regards,
Bruce
Bruce Scherzinger's Avatar Bruce Scherzinger
Got it working. Here's the Custom PHP code I used:
$url = $_SERVER['REQUEST_URI'];
$parts = explode('/',$url);
$alias = $parts[count($parts)-1];
$db->setQuery('SELECT `fulltext` FROM #__content WHERE alias=\''.$alias.'\'');
$fulltext = $db->loadResult();
return str_contains($fulltext,'system-pagebreak');
Yay!

Cheers,
Bruce
Peter van Westen's Avatar Peter van Westen ADMIN
Well done 🙂

Or:
$db->setQuery('SELECT `fulltext` FROM #__content WHERE id='. $app->input->getInt('id'));
$fulltext = $db->loadResult();
return str_contains($fulltext,'system-pagebreak');

Which is a lot safer (prevents sql insertion)
Please post a rating at the Joomla! Extensions Directory
Bruce Scherzinger's Avatar Bruce Scherzinger
Yeah, that's much simpler as well. Thanks, Peter!

Bruce
Bruce Scherzinger's Avatar Bruce Scherzinger
By the way, Peter...The Conditions component is showing 0 uses for the condition set I created. Although it is true that it isn't used in any article directly, it is being used in a Custom HTML module. I am guessing your code may not be looking there for condition sets. Not super-critical, but what I hope is that the list would contain links to the content that does contain the sets.

Thanks again for your help with this concern. Much appreciated!

Cheers,
Bruce
Peter van Westen's Avatar Peter van Westen ADMIN
The usage only lists items that use it. So modules (via Advanced Module Manager), ReReplacer Pro items and Content Templater Pro items.

It does not include inline uses through Conditional Content. As that would require it to search through your entire database.
The Conditional Content syntax could even be used in php files (template overrides for instance). So that is all undoable to track.
Please post a rating at the Joomla! Extensions Directory
Bruce Scherzinger's Avatar Bruce Scherzinger
Ah, good to know. Thanks for clearing that up!

Bruce
You can only post on this forum if you log in