You are here

class AutoEntityLabelManager in Automatic Entity Label 8

Same name and namespace in other branches
  1. 8.3 src/AutoEntityLabelManager.php \Drupal\auto_entitylabel\AutoEntityLabelManager
  2. 8.2 src/AutoEntityLabelManager.php \Drupal\auto_entitylabel\AutoEntityLabelManager

AutoEntityLabelManager class.

Hierarchy

Expanded class hierarchy of AutoEntityLabelManager

1 file declares its use of AutoEntityLabelManager
AutoEntityLabelForm.php in src/Form/AutoEntityLabelForm.php
1 string reference to 'AutoEntityLabelManager'
auto_entitylabel.services.yml in ./auto_entitylabel.services.yml
auto_entitylabel.services.yml
1 service uses AutoEntityLabelManager
auto_entitylabel.manager in ./auto_entitylabel.services.yml
Drupal\auto_entitylabel\AutoEntityLabelManager

File

src/AutoEntityLabelManager.php, line 14

Namespace

Drupal\auto_entitylabel
View source
class AutoEntityLabelManager implements AutoEntityLabelManagerInterface {
  use StringTranslationTrait;

  /**
   * Automatic label is disabled.
   */
  const DISABLED = 0;

  /**
   * Automatic label is enabled. Will always be generated.
   */
  const ENABLED = 1;

  /**
   * Automatic label is optional. Will only be generated if no label was given.
   */
  const OPTIONAL = 2;

  /**
   * The content entity.
   *
   * @var \Drupal\Core\Entity\ContentEntityInterface
   */
  protected $entity;

  /**
   * The type of the entity.
   *
   * @var string
   *
   * @codingStandardsIgnoreStart
   */
  protected $entity_type;

  /**
   * The bundle of the entity.
   *
   * @var string
   */
  protected $entity_bundle;

  /**
   * Indicates if the automatic label has been applied.
   *
   * @var bool
   */
  protected $auto_label_applied = FALSE;

  /**
   * Config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   *
   * @codingStandardsIgnoreEnd
   */
  protected $configFactory;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Token service.
   *
   * @var \Drupal\Core\Utility\Token
   */
  protected $token;

  /**
   * Automatic label configuration.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * Constructs an AutoEntityLabelManager object.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity to add the automatic label to.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   Configuration factory.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity type manager.
   * @param \Drupal\Core\Utility\Token $token
   *   Token manager.
   */
  public function __construct(ContentEntityInterface $entity, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, Token $token) {
    $this->entity = $entity;
    $this->entity_type = $entity
      ->getEntityType()
      ->id();
    $this->entity_bundle = $entity
      ->bundle();
    $this->bundle_entity_type = $entity_type_manager
      ->getDefinition($this->entity_type)
      ->getBundleEntityType();
    $this->configFactory = $config_factory;
    $this->entityTypeManager = $entity_type_manager;
    $this->token = $token;
  }

  /**
   * Checks if the entity has a label.
   *
   * @return bool
   *   True if the entity has a label property.
   */
  public function hasLabel() {

    /** @var \Drupal\Core\Entity\EntityTypeInterface $definition */
    $definition = $this->entityTypeManager
      ->getDefinition($this->entity
      ->getEntityTypeId());
    return $definition
      ->hasKey('label');
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function hasAutoLabel() {
    return $this
      ->getConfig('status') == self::ENABLED;
  }

  /**
   * {@inheritdoc}
   */
  public function hasOptionalAutoLabel() {
    return $this
      ->getConfig('status') == self::OPTIONAL;
  }

  /**
   * {@inheritdoc}
   */
  public function autoLabelNeeded() {
    $not_applied = empty($this->auto_label_applied);
    $required = $this
      ->hasAutoLabel();
    $optional = $this
      ->hasOptionalAutoLabel() && empty($this->entity
      ->label());
    return $not_applied && ($required || $optional);
  }

  /**
   * Gets the field name of the entity label.
   *
   * @return string
   *   The entity label field name. Empty if the entity has no label.
   */
  public function getLabelName() {
    $label_field = '';
    if ($this
      ->hasLabel()) {
      $definition = $this->entityTypeManager
        ->getDefinition($this->entity
        ->getEntityTypeId());
      $label_field = $definition
        ->getKey('label');
    }
    return $label_field;
  }

  /**
   * Gets the entity bundle label or the entity label.
   *
   * @return string
   *   The bundle label.
   */
  protected function getBundleLabel() {
    $entity_type = $this->entity
      ->getEntityTypeId();
    $bundle = $this->entity
      ->bundle();

    // Use the the human readable name of the bundle type. If this entity has no
    // bundle, we use the name of the content entity type.
    if ($bundle != $entity_type) {
      $bundle_entity_type = $this->entityTypeManager
        ->getDefinition($entity_type)
        ->getBundleEntityType();
      $label = $this->entityTypeManager
        ->getStorage($bundle_entity_type)
        ->load($bundle)
        ->label();
    }
    else {
      $label = $this->entityTypeManager
        ->getDefinition($entity_type)
        ->getLabel();
    }
    return $label;
  }

  /**
   * Generates the label according to the settings.
   *
   * @param string $pattern
   *   Label pattern. May contain tokens.
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   Content entity.
   *
   * @return string
   *   A label string
   *
   * @codingStandardsIgnoreStart
   */
  protected function generateLabel($pattern, $entity) {

    // @codingStandardsIgnoreEnd
    $entity_type = $entity
      ->getEntityType()
      ->id();
    $output = $this->token
      ->replace($pattern, [
      $entity_type => $entity,
    ], [
      'sanitize' => FALSE,
      'clear' => TRUE,
    ]);

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

    // Strip tags.
    $output = preg_replace('/[\\t\\n\\r\\0\\x0B]/', '', strip_tags($output));
    return $output;
  }

  /**
   * Returns automatic label configuration of the content entity bundle.
   *
   * @param string $value
   *   The configuration value to get.
   *
   * @return \Drupal\Core\Config\ImmutableConfig
   *   Return the config.
   */
  protected function getConfig($value) {
    if (!isset($this->config)) {
      $this->config = $this->configFactory
        ->get('auto_entitylabel.settings');
    }
    $key = $this->bundle_entity_type . '_' . $this->entity_bundle;
    return $this->config
      ->get($key . '_' . $value);
  }

  /**
   * Gets an alternative entity label.
   *
   * @return string
   *   Translated label string.
   */
  protected function getAlternativeLabel() {
    $content_type = $this
      ->getBundleLabel();
    if ($this->entity
      ->id()) {
      $label = $this
        ->t('@type @id', [
        '@type' => $content_type,
        '@id' => $this->entity
          ->id(),
      ]);
    }
    else {
      $label = $content_type;
    }
    return $label;
  }

  /**
   * Evaluates php code and passes the entity to it.
   *
   * @param string $code
   *   PHP code to evaluate.
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   Content entity to pa ss through to the PHP script.
   *
   * @return string
   *   String to use as label.
   *
   * @codingStandardsIgnoreStart
   */
  protected function evalLabel($code, $entity) {
    ob_start();
    print eval('?>' . $code);

    // @codingStandardsIgnoreEnd
    $output = ob_get_contents();
    ob_end_clean();
    return $output;
  }

  /**
   * Constructs the list of options for the given bundle.
   *
   * @codingStandardsIgnoreStart
   */
  public static function auto_entitylabel_options($entity_type, $bundle_name) {

    // @codingStandardsIgnoreEnd
    $options = [
      'auto_entitylabel_disabled' => t('Disabled'),
    ];
    if (self::auto_entitylabel_entity_label_visible($entity_type)) {
      $options += [
        'auto_entitylabel_enabled' => t('Automatically generate the label and hide the label field'),
        'auto_entitylabel_optional' => t('Automatically generate the label if the label field is left empty'),
      ];
    }
    else {
      $options += [
        'auto_entitylabel_enabled' => t('Automatically generate the label'),
      ];
    }
    return $options;
  }

  /**
   * Check if given entity bundle has a visible label on the entity form.
   *
   * @param string $entity_type
   *   The entity type.
   *
   * @return bool
   *   TRUE if the label is rendered in the entity form, FALSE otherwise.
   *
   * @todo
   *   Find a generic way of determining the result of this function. This
   *   will probably require access to more information about entity forms
   *   (entity api module?).
   *
   * @codingStandardsIgnoreStart
   */
  public static function auto_entitylabel_entity_label_visible($entity_type) {

    // @codingStandardsIgnoreEnd
    $hidden = [
      'profile2' => TRUE,
    ];
    return empty($hidden[$entity_type]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AutoEntityLabelManager::$auto_label_applied protected property Indicates if the automatic label has been applied.
AutoEntityLabelManager::$config protected property Automatic label configuration.
AutoEntityLabelManager::$configFactory protected property Config factory.
AutoEntityLabelManager::$entity protected property The content entity.
AutoEntityLabelManager::$entityTypeManager protected property The entity type manager.
AutoEntityLabelManager::$entity_bundle protected property The bundle of the entity.
AutoEntityLabelManager::$entity_type protected property The type of the entity.
AutoEntityLabelManager::$token protected property Token service.
AutoEntityLabelManager::autoLabelNeeded public function Returns whether the automatic label has to be set. Overrides AutoEntityLabelManagerInterface::autoLabelNeeded
AutoEntityLabelManager::auto_entitylabel_entity_label_visible public static function Check if given entity bundle has a visible label on the entity form.
AutoEntityLabelManager::auto_entitylabel_options public static function Constructs the list of options for the given bundle.
AutoEntityLabelManager::DISABLED constant Automatic label is disabled.
AutoEntityLabelManager::ENABLED constant Automatic label is enabled. Will always be generated.
AutoEntityLabelManager::evalLabel protected function Evaluates php code and passes the entity to it.
AutoEntityLabelManager::generateLabel protected function Generates the label according to the settings.
AutoEntityLabelManager::getAlternativeLabel protected function Gets an alternative entity label.
AutoEntityLabelManager::getBundleLabel protected function Gets the entity bundle label or the entity label.
AutoEntityLabelManager::getConfig protected function Returns automatic label configuration of the content entity bundle.
AutoEntityLabelManager::getLabelName public function Gets the field name of the entity label.
AutoEntityLabelManager::hasAutoLabel public function Determines if the entity bundle has auto entity label enabled. Overrides AutoEntityLabelManagerInterface::hasAutoLabel
AutoEntityLabelManager::hasLabel public function Checks if the entity has a label.
AutoEntityLabelManager::hasOptionalAutoLabel public function Determines if the entity bundle has an optional automatic label. Overrides AutoEntityLabelManagerInterface::hasOptionalAutoLabel
AutoEntityLabelManager::OPTIONAL constant Automatic label is optional. Will only be generated if no label was given.
AutoEntityLabelManager::setLabel public function Sets the automatically generated entity label. Overrides AutoEntityLabelManagerInterface::setLabel
AutoEntityLabelManager::__construct public function Constructs an AutoEntityLabelManager object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.