Let's say I have a newly created node:

$node = \Drupal\node\Entity\Node::create([
  'title' => 'test',
  'type' => 'article',
]);

This node type does not have a configured pathauto pattern for it.

When I check to see if the path is empty or not, I get false, despite there being no path set for the article:

// Returns false
$node->get('path')->isEmpty()

This is because of this code in PathautoItem:

  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
    // Make sure that the pathauto state flag does not get lost if just that is
    // changed.
    return !$this->alias && !$this->get('pathauto')->hasValue();
  }

  /**
   * {@inheritdoc}
   */
  public function applyDefaultValue($notify = TRUE) {
    parent::applyDefaultValue($notify);
    // Created fields default creating a new alias.
    $this->setValue(array('pathauto' => PathautoState::CREATE), $notify);
    return $this;
  }

So because the Pauthauto "state" field is always set to a non-empty value, even for entities that are not using pathauto, you can't use the field API's isEmpty() reliably.

I think this is a bug, but I don't know enough about pathauto's architecture to comment on it much further.

In the mean time, I'm using $entity->path->alias and checking if that's empty or not.

Comments

bkosborne created an issue. See original summary.

bkosborne’s picture

Issue summary: View changes