You are here

public function AutoEntityLabelManager::setLabel in Automatic Entity Label 8.2

Same name and namespace in other branches
  1. 8.3 src/AutoEntityLabelManager.php \Drupal\auto_entitylabel\AutoEntityLabelManager::setLabel()
  2. 8 src/AutoEntityLabelManager.php \Drupal\auto_entitylabel\AutoEntityLabelManager::setLabel()

Sets the automatically generated entity label.

Return value

string The applied label. The entity is updated with this label.

Overrides AutoEntityLabelManagerInterface::setLabel

File

src/AutoEntityLabelManager.php, line 135

Class

AutoEntityLabelManager
Class for Auto Entity Label Manager.

Namespace

Drupal\auto_entitylabel

Code

public function setLabel() {
  if (!$this
    ->hasLabel()) {
    throw new \Exception('This entity has no label.');
  }
  $pattern = $this
    ->getPattern();
  if ($pattern) {
    $label = $this
      ->generateLabel($pattern, $this->entity);
  }
  else {
    $label = $this
      ->getAlternativeLabel();
  }
  $label = mb_substr($label, 0, 255);
  $label_name = $this
    ->getLabelName();
  $this->entity->{$label_name}
    ->setValue($label);
  $this->auto_label_applied = TRUE;
  return $label;
}