bobdenotter / sitemap

Provide a sitemap (.xml) for your Bolt 4 site
MIT License
5 stars 4 forks source link

Including taxonomies causes a variable reference error #11

Closed andysh-uk closed 2 years ago

andysh-uk commented 2 years ago

I've just installed v1.2.1 on Bolt 5.1.4, and with the default template, there is a PHP error (variable "record" does not exist) when including taxonomies.

# Configuration file for the Sitemap extension.

show_listings: false
limit: 10000
templates:
  xml: "@sitemap/sitemap.xml.twig"
  xsl: "@sitemap/sitemap.xsl"
taxonomies: ["categories", "tags"]

Commenting out the "taxonomies" line above gets it working.

in vendor/bobdenotter/sitemap/templates/sitemap.xml.twig (line 56)

            <changefreq>weekly</changefreq>
            <lastmod>{{ (record.modifiedAt is defined ? record.modifiedAt : 'now')|date('Y-m-d\\TH:i:sP') }}</lastmod>
            {% block priority -%}
                <priority>{{ url == '/' ? 1 : priority }}</priority>
            {% endblock priority -%}
            {% if record.definition.locales.all|length > 1 %}
                {% for locale in record.definition.locales %}
                    <xhtml:link rel="alternate" hreflang="{{ locale }}" href="{{ record|link(true, locale) }}" />
                {% endfor %}
            {% endif %}
            {% if record|image is not empty -%}

Line 56 is:

{% if record.definition.locales.all|length > 1 %}

I suspect this just needs a similar "record is defined" check like the lastmod line previously. I'll look to submit a PR in the next couple of days unless someone beats me to it.

andysh-uk commented 2 years ago

As I thought, the following changes worked (I've made these in my own template, but will send a proper PR for the default template):

In templates\sitemap.xml.twig, line 56 becomes:

{% if (record.definition is defined and record.definition.locales.all|length > 1) %}

and line 61 becomes:

{% if (record.definition is defined and record.definition.locales.all|length > 1) %}