You are here

function auto_entitylabel_entity_insert in Automatic Entity Label 8.2

Same name and namespace in other branches
  1. 8.3 auto_entitylabel.module \auto_entitylabel_entity_insert()
  2. 7 auto_entitylabel.module \auto_entitylabel_entity_insert()

Implements hook_entity_insert().

File

./auto_entitylabel.module, line 152
Allows hiding of entity label fields and automatic label creation.

Code

function auto_entitylabel_entity_insert(EntityInterface $entity) {
  if ($entity instanceof ContentEntityInterface) {
    $decorator = \Drupal::service('auto_entitylabel.entity_decorator');

    /** @var \Drupal\auto_entitylabel\AutoEntityLabelManager $decorated_entity */
    $decorated_entity = $decorator
      ->decorate($entity);
    if ($decorated_entity
      ->hasLabel() && ($decorated_entity
      ->hasAutoLabel() || $decorated_entity
      ->hasOptionalAutoLabel())) {

      // Store the old label.
      $old_label = $entity
        ->label();

      // Update the entity label.
      $decorated_entity
        ->setLabel();

      // Save it only if the title has changed.
      if ($entity
        ->label() != $old_label) {
        if ($entity
          ->getEntityType()
          ->isRevisionable()) {
          $entity
            ->setNewRevision(FALSE);
        }
        $entity
          ->save();
      }
    }
  }
}