Firstly: the reason for the white page errors was that I was putting the code into the wrong file. Instead of adding it to blog_item.php it should be in blog.php. It worked once I'd moved the code.
Secondly, I then realised I didn't need the code in the blog at all (Joomla handles the display of featured/not featured/both in blogs just fine), I needed it in the actual full article view (reason at the bottom).
So, if anyone else wants to know how to do this - i.e. indicate whether an article is actively, currently featured, not pending or expired - this is what I did:
In phpMyAdmin, I created a view with SQL, combining #__content and #__content_frontpage as well as adding a new column:
CREATE OR REPLACE VIEW #_content_with_featured AS
SELECT #_content.id AS f_id,#_content.title,#_content.featured,#_content_frontpage.featured_up,#_content_frontpage.featured_down,
(CASE
WHEN #_content.featured = '0' THEN 'no'
WHEN #_content.featured = '1' AND #_content_frontpage.featured_up > NOW() THEN 'no'
WHEN #_content.featured = '1' AND #_content_frontpage.featured_down < now() THEN 'no'
WHEN #_content.featured = '1' AND #_content_frontpage.featured_down IS NULL AND #_content_frontpage.featured_up < NOW() THEN 'yes'
WHEN #_content.featured = '1' AND #_content_frontpage.featured_up IS NULL AND #_content_frontpage.featured_down > NOW() THEN 'yes'
WHEN #_content.featured = '1' AND #_content_frontpage.featured_up IS NULL AND #_content_frontpage.featured_down IS NULL THEN 'yes'
END)
AS f_current
FROM #_content
LEFT JOIN #_content_frontpage ON #_content.id = #_content_frontpage.content_id
Note:
- In your database query you need to use the table prefix instead of
the # as you may have more than one table prefix in your database.
- Using 'OR REPLACE' in the first line allows you to tweak and re-run
the query, which overwrites the previous view, in case you want to
add or remove further columns.
- Using a view means that any additions/deletions/modifications to the
original two tables are dynamically updated. This would not happen
if you created a new table instead. Additionally, if you name the
view with the same prefix as your database tables, this enables you
to query the view in Joomla.
- In Joomla, you don't need to use the table prefix as Joomla knows
the prefix as it's in your Global Configuration.
I changed this code in an article override html/com_content/article/default.php on line 54:
<div class="page-header">
to:
<div class="page-header cf-<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('f_current'))
->from($db->quoteName('#__content_with_featured'))
->where($db->quoteName('f_id') . " = " . $articleId);
$db->setQuery($query);
$result = $db->loadResult();
echo $result;
?>">
which produces a new line of code that looks like this:
<div class="page-header cf-no">
or
<div class="page-header cf-yes">
The reason for doing this? My client has two menu items for events using the same category. One for future events - which are currently featured - and one for past events with reports added to the original articles but whilst still technically 'featured', they are 'expired'. Because they are all the same category, when clicking through to a full article display, they all have the same category root. So, while the blog items' URLs might be /diary/future and /diary/reports, all the articles go to /diary/future/[article]. I wanted article category modules (one listing future events and the other listing event reports) to be displayed in the sidebar on the article page depending on whether the article being viewed was a future event or a report. As all the articles are classed as featured, even if they are pending or expired, then I couldn't use the 'featured' field from #__content to control the display. I needed it based on the 'featured_up' and 'featured_down' dates as well.
I added both modules to the /events/future menu item and added a class of 'events' to the menu item. The 'future' module had a module class of 'future' added and the 'reports' module had a class of 'reports' added. Then the display (or not) of either sidebar module on the article pages is controlled with a little bit of JavaScript, which also hides the modules on the parent blog items:
if (document.querySelector(".events")) {
let blogModOne = document.querySelector(".rightsidebar");
if (document.querySelector(".blog-items")) {
blogModOne.classList.add("d-none");
}
else if ((document.querySelector(".item-page")) && (document.querySelector(".featured-0"))) {
let modOne = document.querySelector(".sidebarright.future");
modOne.classList.add("d-none");
}
else if ((document.querySelector(".item-page")) && (document.querySelector(".featured-1"))) {
let modTwo = document.querySelector(".sidebarright.reports");
modTwo.classList.add("d-none");
}
}