I needed to exclude certain headers from the Table of Contents for a project, namely ones that were rendered via an 'accordion' paragraph. I added an optional setting to specify an XPath for headers to be excluded (that would otherwise pass under the other settings).

Patch attached achieves the basic functionality. Not sure what would happen for a malformed path.

Comments

bgilhome created an issue. See original summary.

bgilhome’s picture

Issue summary: View changes
bgilhome’s picture

StatusFileSize
new5.62 KB
bgilhome’s picture

StatusFileSize
new5.63 KB

Need to use strict=TRUE for the in_array check on the $heading DOMElement. Updated patch atatched.

vladimiraus’s picture

Status: Needs review » Needs work

Looks like we need to add hook_update to add new option for existing installations.

smulvih2’s picture

Status: Needs work » Needs review
StatusFileSize
new5.66 KB

#4 worked for me. Updated patch to fix a formatting issue and add an example xpath to field description to give users a good default for ignoring anything with a class of toc-ignore.

@vladimiraus, don't think we need a hook_update() for this. I added this patch to an existing install and it worked great; no change to functionality unless I manually add an xpath.

smulvih2’s picture

Would be nice to get this included in a new release. I moved a site from drupal/tocify to this module because tocify relied on JS to build the TOC client-side and that had inherent accessibility issues. When doing a module comparison this module only lacked the ability to ignore certain DOM elements, which this patch fixes.

vladimiraus’s picture

Status: Needs review » Needs work

Thanks @smulvih2 for the contribution. Couple of things to fix:

  • hook_update for current installation to include header_exclude_xpath to all existing TOCs
  • change new \DOMXPath($dom); to new \DOMXPath($dom);
smulvih2’s picture

@vladimiraus, I don't think a hook_update() is needed here. Without a hook_update() the header_exclude_xpath line is added to existing TOC config when a TOC is saved. Otherwise if the header_exclude_xpath line is missing from TOC config it doesn't cause an issue. The options array is protected so not sure how to update this, please advise.

Also I tried removing the slash before DOMXPath($dom); which is what I think you were trying to say in your last comment, but this causes an error, so working fine as is.

smulvih2’s picture

Status: Needs work » Needs review
StatusFileSize
new6.4 KB

New patch with hook_update() to add header_exclude_xpath to all existing TOCs.

smulvih2’s picture

@VladimirAus any chance you can review this and get into a new release? I'm using this patch on a few projects and its working great.

vladimiraus’s picture

Thanks for your commit @smulvih2.
Give me few days...

liquidcms’s picture

As I am using this functionality without this patch; safe to say this has been committed and this should be marked fixed/closed.

Also, wouldn't //*[contains(@class,"toc-ignore")]//* have been a better default?

smulvih2’s picture

@liquidcms if you are using WxT this patch is included in the distro which is why it is working for you. Still waiting for this patch to be rolled into latest version of toc_api.

  • VladimirAus committed cdb884a on 8.x-1.x authored by smulvih2
    Issue #3118516 by bgilhome, smulvih2, VladimirAus: Add an 'exclude'...
vladimiraus’s picture

Status: Needs review » Fixed

Thank you. Commited.

joseph.olstad’s picture

@VladimirAus,
thank you!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.