localgovdrupal / localgov_subsites

Replaces LocalGov Campaigns with a new name and extended functionality to support subsites within LocalGov Drupal installations.
GNU General Public License v2.0
4 stars 2 forks source link

Subsite preview links are broken if the subsite overview is unpublished (causing WSOD) #118

Closed andybroomfield closed 1 year ago

andybroomfield commented 1 year ago

When a comms officer was trying to share a preview link to a subsite page that wasn't public yet, they encountered a problem for those clicking the link, as they where getting the WSOD technicalk difficulties message.

On closer inspection, the following PHP error was being triggered.

UnexpectedValueException: Object not found in SplObjectStorage->offsetGet() (line 59 of /mnt/www/html/brightonhovemain/docroot/modules/contrib/localgov_subsites/src/Plugin/Block/SubsitesNavigationBlock.php).

This occurs as the subsitenavigationblock is building all menu links using $entities = $mapper->loadAndAccessCheckEntitysForTreeNodes('node', $tree, $cache); and then tries to get the subsite id using $subsite_id = $entities[$ancestors[0]]->id();. But since the overview is unpublished, this will fail as its blank. Wrapping an empty check around this and setting subsite_id ?? NULL if blank does seem to resolve this issue, though it's not an ideal fix.

Steps to replicate

  1. Create a subsite overview, but don't publish
  2. Create a subsite page that uses the unpublished overview as the parent
  3. Generate a preview line for the subsite page
  4. Try to use the preview link in a private browsing window.

Expected result Subsite page is shown, albeit with only the page in the navigation menu

Actual resule WSOD, PHP error or technical difficulities message depending on setup.

andybroomfield commented 1 year ago

Additional, wrapping this in an empty block will also cause this error on the twig

The website encountered an unexpected error. Please try again later. AssertionError: $url must be a string or object of type \Drupal\Core\Url in assert() (line 246 of core/lib/Drupal/Core/Template/TwigExtension.php). Becuase the subsite overview link will have no url. so subsite-navigation.html.twig needs to be updated with

{% if item.url is not empty %}
{{ link(item.title, item.url) }}
{% endif %}
andybroomfield commented 1 year ago

Would there be an objection to me applying the first fix?

Wrapping an empty check around this and setting subsite_id ?? NULL if blank does seem to resolve this issue,