How can I tell if a config entity hasn’t been saved yet?

  • 1
    Would be nice to have some more context provided to your question. Why you need it? Where you need it? – leymannx Aug 30 '19 at 8:21

this core test confirms it:

// Config is no longer new once saved.

(from core/tests/Drupal/Tests/Core/Config/ConfigTest.php)

  • Why did you post a question if you already knew the answer? – user72672 Aug 30 '19 at 9:52

You shall use isNew().

From the docs:

Usually an entity is new if no ID exists for it yet. However, entities may be enforced to be new with existing IDs too.

It returns TRUE when the entity is new and FALSE if the entity has been saved before.

if ($entity->isNew()) {
   // Entity is new.
else {
  // Entity has been saved before.

Check if it has an ID.

if ($entity->id()) {
   // Not a new entity.
else {
  // Hasn't yet been saved.
  • 2
    Look at the docs for isNew, sometimes entities are enforced to be new with an ID too, so this solution would not work in every situation. – user72672 Aug 30 '19 at 6:15

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