You are here

public function AutoEntityLabelManager::setLabel in Automatic Entity Label 8

Same name and namespace in other branches
  1. 8.3 src/AutoEntityLabelManager.php \Drupal\auto_entitylabel\AutoEntityLabelManager::setLabel()
  2. 8.2 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 130

Class

AutoEntityLabelManager
AutoEntityLabelManager class.

Namespace

Drupal\auto_entitylabel

Code

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