label method sometimes returns null

Created on 9 May 2025, 5 days ago

Because of the conditions used in Order::label, the result can sometimes be null. In my case, it was because I was using a workflow with several states (the required 'draft' and other validation states) before the order number is generated.
In other words: when using a custom order validation process, the following condition may not always be met:
elseif ($this->getState()->getId() === 'draft' && !$this->isNew()) {
Obviously a null label can generate errors in subsequent function calls (such as transliteration in the entity_print module, in my case).
I suggest either replacing
$label = $this->t('Draft @id', ['@id' => $this->id()]);
with
$label = $this->t('@state @id', ['@state' => $this->getState()->getLabel(), '@id' => $this->id()]);
or defaulting to a null string before dispatching the event
$label = $label ?? '';

🐛 Bug report
Status

Active

Version

3.0

Component

Order

Created by

🇫🇷France linebreak

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @linebreak
  • 🇮🇱Israel jsacksick

    The thing is I'm not sure this is the right fix as the label method has the following signature:

      /**
       * Gets the label of the entity.
       *
       * @return string|\Drupal\Core\StringTranslation\TranslatableMarkup|null
       *   The label of the entity, or NULL if there is no label defined.
       */
      public function label();
    

    The label method can return NULL, so modules dealing with labels should account for that, so to me this is a won't fix.

Production build 0.71.5 2024