class AutoEntityLabelManager in Automatic Entity Label 8.2
Same name and namespace in other branches
- 8.3 src/AutoEntityLabelManager.php \Drupal\auto_entitylabel\AutoEntityLabelManager
- 8 src/AutoEntityLabelManager.php \Drupal\auto_entitylabel\AutoEntityLabelManager
Class for Auto Entity Label Manager.
Hierarchy
- class \Drupal\auto_entitylabel\AutoEntityLabelManager implements AutoEntityLabelManagerInterface uses StringTranslationTrait
Expanded class hierarchy of AutoEntityLabelManager
2 files declare their use of AutoEntityLabelManager
- AutoEntityLabelForm.php in src/
Form/ AutoEntityLabelForm.php - auto_entitylabel.module in ./
auto_entitylabel.module - Allows hiding of entity label fields and automatic label creation.
1 string reference to 'AutoEntityLabelManager'
1 service uses AutoEntityLabelManager
File
- src/
AutoEntityLabelManager.php, line 15
Namespace
Drupal\auto_entitylabelView 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;
/**
* Automatic label is prefilled.
*/
const PREFILLED = 3;
/**
* The content entity.
*
* @var \Drupal\Core\Entity\ContentEntityInterface
*/
protected $entity;
/**
* The type of the entity.
*
* @var string
*/
// @codingStandardsIgnoreLine
protected $entity_type;
/**
* The bundle of the entity.
*
* @var string
*/
// @codingStandardsIgnoreLine
protected $entity_bundle;
/**
* Indicates if the automatic label has been applied.
*
* @var bool
*/
// @codingStandardsIgnoreLine
protected $auto_label_applied = FALSE;
/**
* Config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
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
->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;
}
/**
* {@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);
}
/**
* {@inheritdoc}
*/
public function getStatus() {
return $this
->getConfig('status');
}
/**
* {@inheritdoc}
*/
public function getPattern() {
$pattern = $this
->getConfig('pattern') ?: '';
$pattern = trim($pattern);
return $pattern;
}
/**
* 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 object $entity
* Content entity.
*
* @return string
* A label string
*/
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;
}
/**
* Returns automatic label configuration of the content entity bundle.
*
* @param string $value
* The configuration value to get.
*
* @return \Drupal\Core\Config\ImmutableConfig
* Return Config.
*/
protected function getConfig($value) {
if (!isset($this->config)) {
$this->config = $this->configFactory
->get('auto_entitylabel.settings.' . $this->entity_type . '.' . $this->entity_bundle);
}
return $this->config
->get($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 object $entity
* Content entity to pa ss through to the PHP script.
*
* @return string
* String to use as label.
*/
protected function evalLabel($code, $entity) {
ob_start();
// @codingStandardsIgnoreLine
print eval('?>' . $code);
$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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AutoEntityLabelManager:: |
protected | property | ||
AutoEntityLabelManager:: |
protected | property | Automatic label configuration. | |
AutoEntityLabelManager:: |
protected | property | Config factory. | |
AutoEntityLabelManager:: |
protected | property | The content entity. | |
AutoEntityLabelManager:: |
protected | property | The entity type manager. | |
AutoEntityLabelManager:: |
protected | property | ||
AutoEntityLabelManager:: |
protected | property | ||
AutoEntityLabelManager:: |
protected | property | Token service. | |
AutoEntityLabelManager:: |
public | function |
Returns whether the automatic label has to be set. Overrides AutoEntityLabelManagerInterface:: |
|
AutoEntityLabelManager:: |
public static | function | Check if given entity bundle has a visible label on the entity form. | |
AutoEntityLabelManager:: |
public static | function | Constructs the list of options for the given bundle. | |
AutoEntityLabelManager:: |
constant | Automatic label is disabled. | ||
AutoEntityLabelManager:: |
constant | Automatic label is enabled. Will always be generated. | ||
AutoEntityLabelManager:: |
protected | function | Evaluates php code and passes the entity to it. | |
AutoEntityLabelManager:: |
protected | function | Generates the label according to the settings. | |
AutoEntityLabelManager:: |
protected | function | Gets an alternative entity label. | |
AutoEntityLabelManager:: |
protected | function | Gets the entity bundle label or the entity label. | |
AutoEntityLabelManager:: |
protected | function | Returns automatic label configuration of the content entity bundle. | |
AutoEntityLabelManager:: |
public | function | Gets the field name of the entity label. | |
AutoEntityLabelManager:: |
public | function |
Returns pattern for the label. Overrides AutoEntityLabelManagerInterface:: |
|
AutoEntityLabelManager:: |
public | function |
Returns status of automatic entity label. Overrides AutoEntityLabelManagerInterface:: |
|
AutoEntityLabelManager:: |
public | function |
Determines if the entity bundle has auto entity label enabled. Overrides AutoEntityLabelManagerInterface:: |
|
AutoEntityLabelManager:: |
public | function | Checks if the entity has a label. | |
AutoEntityLabelManager:: |
public | function |
Determines if the entity bundle has an optional automatic label. Overrides AutoEntityLabelManagerInterface:: |
|
AutoEntityLabelManager:: |
constant | Automatic label is optional. Will only be generated if no label was given. | ||
AutoEntityLabelManager:: |
constant | Automatic label is prefilled. | ||
AutoEntityLabelManager:: |
public | function |
Sets the automatically generated entity label. Overrides AutoEntityLabelManagerInterface:: |
|
AutoEntityLabelManager:: |
public | function | Constructs an AutoEntityLabelManager object. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |