Large Taxonomy Entity Reference Gotcha

2022-01-04

Our Islandora site is built on top of an older digital exhibits site which included content types using paragraphs to build exhibits.

As often happens, we needed to correct a link on one of the paragraphed collection pages. However, when I clicked the link to edit the page I was met with an unexpected White Screen of Death (WSOD). 😬

Checking the webserver logs showed that PHP was running out of memory loading the edit page, specifically when running database queries. 🤔 Out of Memory errors usually mean one thing: Drupal is trying to load a lot of entities. The most common reason for Drupal to load entities on a single-item’s edit page is an entity reference field.

As suspected, the content type uses a taxonomy reference field. In this case the reference field was set to accept a term from any vocabulary despite having a vocabulary dedicated to this field. Normally being this wide open isn’t a big deal, it would simply mean opportunity for users to select terms from the wrong vocabulary.

The trouble here is that the edit form for was set to use a select list. This caused Drupal to try to load the title and vocabulary for every term in our repository—and we have a lot of taxonomy terms)—resulting in the out of memory error.

Fortunately, the fixes are simple. I opted to restrict the vocabularies the reference field could target. This reduced the options in the select list field to less than two-dozen rows from the several thousands of terms it was trying to load previously. 🎉 Alternatively, had we wanted to keep the reference field open to all the vocabularies, we could have adjusted the edit form to use an autocomplete field which would dynamically load options based user input.