You are here

function auto_entitylabel_entity_insert in Automatic Entity Label 8.3

Same name and namespace in other branches
  1. 8.2 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 174
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() || empty($entity
      ->label()) && $decorated_entity
      ->hasOptionalAutoLabel())) {
      if ($entity
        ->getEntityType()
        ->isRevisionable()) {
        $entity
          ->setNewRevision(FALSE);
      }
      $entity
        ->save();
    }
  }
}