1

I might be pushing the limits of what's possible with Craft's autogenerated title capability, but in any event I'd like to figure out the best path to arrive at my goal.

We have an internal parts inventory with unique IDs assigned in the format YYYYMMDD-NNN. I'd like to recreate that same autogenerated format as we move our data to Craft.

Logically, the concept is "today's date plus the sequential ID incrementing for the parts recorded one that day". Each day, we start back at '-001' (e.g., '20210305-001', '20210305-002',...).

I've tried the following code in Title Format autogeneration input, but either have the syntax messed up or am pushing beyond the capability of what's possible with autogenerating for a title field in craft.

{# Desired unique ID format = 'YYYYMMDD-NNN' #}
{% if not object.id %}
    {% set uniqueID = craft.entries.section('inventory')
    .order('postDate DESC')
    .first()
    .title %}
    {% set datePortion =  uniqueID | slice(0, 8) %} {# extract YYYYMMDD from string #}
    {% if datePortion == 'now'|date('Y-m-d') %}
        {% set count = uniqueID | slice(9, 3) %}
        {{ datePortion ~ '-' ~ count + 1 }} {# increment today's count for next part #}
    {% else %}
        {{ 'now'|date('Y-m-d') ~ '-001' }} {# first entry in a new day #}
    {% endif %}
{% else %}
    {{ object.title }}
{% endif %}

Hope someone has a more clever and elegant solution - thanks!

2 Answers 2

3

Unless I'm missing something, you should be able to use the seq Twig function (docs) and use something like:

{{ 'now'|date('Ymd') ~ '-' ~ seq('now'|date('Ymd'), 3) }}

This would generate the format you need YYYYMMDD-NNN and reset the NNN every new day.

Sequences are stored in your sequences database table.

2
  • Wow - this is brilliant! Never knew Twig had this capability under the hood. It's incrementing now, however, still a minor issue. I nested your code in the conditional {% if object.title %} {{ object.title }} {% else %} {{ 'now'|date('Ymd') ~ '-' ~ seq('now'|date('Ymd'), 3) }} {% endif %}. When I create a new entry, it increments (can see it at top). When I save the same entry, it increments yet again! Is my conditional wrong?
    – Ed Hebert
    Mar 9, 2021 at 13:48
  • I should also mention that I also tried {% if object.id %} {{ object.title }} {% else %} {{ 'now'|date('Ymd') ~ '-' ~ seq('now'|date('Ymd'), 3) }} {% endif %}. When creating the entry, the numerical title appears properly at the top of the entry page. However, when I save it, the entry is saved with the title "Untitled entry".
    – Ed Hebert
    Mar 9, 2021 at 14:00
2

Here's my solution:

{% if object.title %}
  {{- object.title -}}
{% else %}
  {%- set date = 'now'|date('Ymd') -%}
  {%- set count = craft.entries.section('inventory').search('title::' ~ date ~ '*' ).anyStatus().count() + 1 -%}
  {{- date ~ '-' ~ ("%03d"|format(count)) -}}
{% endif %}

First, we're checking to see if the entry already has a title. If so, use the existing title since we only ever want it generated once.

The real trick is passing the current date formatted like YYYYMMDD to a search query. For example, .search('title::20210308*') with search for all entries where the title field starts with 20210308. Then we use .count() to find out how many entries exist and add 1 to that number.

Finally the format filter adds leading zeros to the number (taken from this answer"Keep leading 0 in Twig").

I've added whitespace control dashes ({{- -}}, {%- -%}) to prevent extra whitespace from creeping is, but you could just as easily wrap the whole thing in {% apply spaceless %}...{% endapply %} tags.

Update:

I realized there's a scenario where this solution wouldn't work. If entries are created on the same day but some are deleted, the .count() could be incorrect. For example, 2 entries are created (-001 and -002), but the first is deleted. The next entry created on that day will be numbered -002 as well, resulting in a duplicate title.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.