You are here

protected function AutoEntityLabelManager::generateLabel in Automatic Entity Label 8.2

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

Generates the label according to the settings.

Parameters

string $pattern: Label pattern. May contain tokens.

object $entity: Content entity.

Return value

string A label string

1 call to AutoEntityLabelManager::generateLabel()
AutoEntityLabelManager::setLabel in src/AutoEntityLabelManager.php
Sets the automatically generated entity label.

File

src/AutoEntityLabelManager.php, line 256

Class

AutoEntityLabelManager
Class for Auto Entity Label Manager.

Namespace

Drupal\auto_entitylabel

Code

protected function generateLabel($pattern, $entity) {
  $entity_type = $entity
    ->getEntityType()
    ->id();
  $output = $this->token
    ->replace($pattern, [
    $entity_type => $entity,
  ], [
    'clear' => TRUE,
  ]);

  // Evaluate PHP.
  if ($this
    ->getConfig('php')) {
    $output = $this
      ->evalLabel($output, $this->entity);
  }

  // Decode HTML entities, returning them to their original UTF-8 characters.
  $output = Html::decodeEntities($output);

  // Strip tags and Remove special characters.
  $pattern = !empty($this
    ->getConfig('escape')) ? '/[^a-zA-Z0-9\\s]|[\\t\\n\\r\\0\\x0B]/' : '/[\\t\\n\\r\\0\\x0B]/';
  $output = preg_replace($pattern, ' ', strip_tags($output));

  // Invoke hook_auto_entitylabel_label_alter().
  $entity_clone = clone $entity;
  \Drupal::moduleHandler()
    ->alter('auto_entitylabel_label', $output, $entity_clone);

  // Trim stray whitespace from beginning and end. Also converts 2 or more
  // whitespace characters within label to a single space.
  $output = preg_replace('/\\s{2,}/', ' ', trim($output));
  return $output;
}