Acquia Site Builder D10 study materials
Acquia Site Builder D10 study resources

Taxonomy Term Reference and Entity Reference Revisions

Acquia Site Builder D10 · Put your knowledge to work in context.

All case studies

Taxonomy Term Reference and Entity Reference Revisions

A news site uses a taxonomy vocabulary 'Categories' with 500 terms. Articles have an entity reference field to Categories. The site also uses a 'Related Articles' entity reference field to other Article nodes. Editors report that when they edit an article and change the category, the change is not reflected in views that list articles by category until they save the article again. Additionally, when they create a new article and reference another article that is unpublished, the reference is allowed, but the referenced article is not visible on the live site. The site has 10,000 articles. The team wants to ensure that category changes are immediately reflected in views and that unpublished articles cannot be referenced.

1. Why are category changes not immediately reflected in views, and how can this be fixed?

Read the worked answer

Views that list articles by category use the taxonomy term ID from the article's field. When the category is changed, the article's field value is updated, but the view's cache may not be invalidated immediately if the view is cached. Drupal's cache tags should invalidate the view when the article is saved. However, if the view is cached with a long lifetime or if the view is using a cache tag that is not specific to the article, the change may not appear. A common cause is that the view is using 'Tag based' caching with the correct tags, but the article's save operation does not invalidate the taxonomy term cache tag. To fix, ensure that the view's caching is set to 'Tag based' and that the 'Cache tags' are set to 'node_list' and 'taxonomy_term_list'. Also, check that the article's field is set to 'Index' in the view. Another possibility is that the view is using a relationship that does not update. The fix is to clear the cache or adjust the view's caching settings to use 'Tag based' with the appropriate tags. Additionally, ensure that the 'Category' field is not using a 'Static' cache context. After saving the article, the view should update.

2. How can the team prevent unpublished articles from being referenced in the 'Related Articles' field?

Read the worked answer

The 'Related Articles' field is an entity reference field to Article nodes. By default, entity reference fields allow referencing any node, regardless of published status. To restrict to published articles only, use a reference method that filters by published status. In the field settings, under 'Reference type', you can use a 'View' to filter the referenced entities. Create a view that lists published articles only, and set the entity reference field to use that view as the reference method. Alternatively, use the 'Entity Reference' module's 'Filter by an entity reference view' feature. Another approach is to use a 'Content entity' reference with a filter in the field's configuration: under 'Manage form display', for the entity reference field, set the 'Reference method' to 'Views' and select a view that filters on 'Published = Yes'. This ensures that only published articles appear in the autocomplete. Also, consider using the 'Entity Reference Validator' to enforce that only published articles can be referenced on save. The simplest is to use a view-based reference.

3. What performance considerations should be taken into account with 10,000 articles and 500 taxonomy terms when using entity reference fields?

Read the worked answer

With 10,000 articles and 500 terms, entity reference fields can cause performance issues if not configured properly. For the taxonomy term reference field, ensure that the field is using an autocomplete widget rather than a select list, as a select list with 500 terms would be unwieldy and slow. For the 'Related Articles' field, using an autocomplete widget with a view that filters published articles is efficient. However, the view should be optimized: limit the number of results (e.g., 50) and use an appropriate filter. Also, ensure that the entity reference field does not load all referenced entities on node load; use lazy loading or entity reference revisions if needed. For views that list articles by category, ensure that the view is using proper indexing and caching. Consider using the 'Taxonomy term' reference field with a 'Views' reference method to limit the terms shown. Additionally, enable caching for the autocomplete results. For the 'Related Articles' field, if many articles reference each other, consider using a custom module to cache the references. Overall, use autocomplete widgets, limit results, and enable caching.