diff --git a/auto_entitylabel.module b/auto_entitylabel.module index 2d2b8d9..c9a62ed 100644 --- a/auto_entitylabel.module +++ b/auto_entitylabel.module @@ -122,7 +122,7 @@ function auto_entitylabel_prepare_entityform(array &$form, ContentEntityInterfac $widget['value']['#type'] = 'hidden'; $widget['value']['#required'] = FALSE; if (empty($widget['value']['#default_value'])) { - $widget['value']['#default_value'] = '%AutoEntityLabel%'; + $widget['value']['#default_value'] = AutoEntityLabelManager::TEMP_LABEL; } break; @@ -186,7 +186,7 @@ function auto_entitylabel_entity_insert(EntityInterface $entity) { if ($decorated_entity->hasLabel() && ( $decorated_entity->hasAutoLabel() - || empty($entity->label()) && $decorated_entity->hasOptionalAutoLabel() + || $decorated_entity->labelIsEmpty() && $decorated_entity->hasOptionalAutoLabel() ) ) { if ($entity->getEntityType()->isRevisionable()) { diff --git a/config/schema/auto_entitylabel.schema.yml b/config/schema/auto_entitylabel.schema.yml index 36f4a63..a7750ab 100644 --- a/config/schema/auto_entitylabel.schema.yml +++ b/config/schema/auto_entitylabel.schema.yml @@ -8,6 +8,9 @@ auto_entitylabel.settings.*: pattern: type: string label: 'Pattern for the label' + enforce_resaving: + type: boolean + label: 'Generate by re-saving' escape: type: boolean label: 'Remove special characters' diff --git a/src/AutoEntityLabelManager.php b/src/AutoEntityLabelManager.php index f646eb6..6fbdd4e 100644 --- a/src/AutoEntityLabelManager.php +++ b/src/AutoEntityLabelManager.php @@ -2,14 +2,14 @@ namespace Drupal\auto_entitylabel; +use Drupal\Component\Utility\Html; +use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Render\BubbleableMetadata; use Drupal\Core\StringTranslation\StringTranslationTrait; -use Drupal\Core\Entity\ContentEntityInterface; -use Drupal\Core\Config\ConfigFactoryInterface; -use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Utility\Token; -use Drupal\Component\Utility\Html; /** * Class for Auto Entity Label Manager. @@ -17,6 +17,11 @@ use Drupal\Component\Utility\Html; class AutoEntityLabelManager implements AutoEntityLabelManagerInterface { use StringTranslationTrait; + /** + * The temporary label for prevent database errors. + */ + const TEMP_LABEL = '%AutoEntityLabel%'; + /** * Automatic label is disabled. */ @@ -154,6 +159,13 @@ class AutoEntityLabelManager implements AutoEntityLabelManagerInterface { return $definition->hasKey('label'); } + /** + * {@inheritdoc} + */ + public function needEnforceResaving() { + return $this->entity->isNew() && $this->getConfig('enforce_resaving'); + } + /** * {@inheritdoc} */ @@ -163,16 +175,22 @@ class AutoEntityLabelManager implements AutoEntityLabelManagerInterface { throw new \Exception('This entity has no label.'); } - $pattern = $this->getPattern(); - - if ($pattern) { - $label = $this->generateLabel($pattern, $this->entity); + if ($this->needEnforceResaving()) { + $label = static::TEMP_LABEL; } else { - $label = $this->getAlternativeLabel(); + $pattern = $this->getPattern(); + + if ($pattern) { + $label = $this->generateLabel($pattern, $this->entity); + } + else { + $label = $this->getAlternativeLabel(); + } + + $label = mb_substr($label, 0, 255); } - $label = mb_substr($label, 0, 255); $label_name = $this->getLabelName(); $this->entity->$label_name->setValue($label); @@ -181,6 +199,13 @@ class AutoEntityLabelManager implements AutoEntityLabelManagerInterface { return $label; } + /** + * {@inheritdoc} + */ + public function labelIsEmpty() { + return strlen((string) $this->entity->label()) == 0 || $this->entity->label() == AutoEntityLabelManager::TEMP_LABEL; + } + /** * {@inheritdoc} */ @@ -201,7 +226,7 @@ class AutoEntityLabelManager implements AutoEntityLabelManagerInterface { public function autoLabelNeeded() { $not_applied = empty($this->autoLabelApplied); $required = $this->hasAutoLabel(); - $optional = $this->hasOptionalAutoLabel() && empty($this->entity->label()); + $optional = $this->hasOptionalAutoLabel() && $this->labelIsEmpty(); return $not_applied && ($required || $optional); } diff --git a/src/AutoEntityLabelManagerInterface.php b/src/AutoEntityLabelManagerInterface.php index c7e8428..cda51da 100644 --- a/src/AutoEntityLabelManagerInterface.php +++ b/src/AutoEntityLabelManagerInterface.php @@ -7,6 +7,14 @@ namespace Drupal\auto_entitylabel; */ interface AutoEntityLabelManagerInterface { + /** + * Determines if the entity need to be resaved for the label to be generated. + * + * @return bool + * True if the entity need to be resaved. + */ + public function needEnforceResaving(); + /** * Sets the automatically generated entity label. * @@ -15,6 +23,14 @@ interface AutoEntityLabelManagerInterface { */ public function setLabel(); + /** + * Determines if the entity label is empty. + * + * @return bool + * True if the entity label is empty. + */ + public function labelIsEmpty(); + /** * Determines if the entity bundle has auto entity label enabled. * diff --git a/src/Form/AutoEntityLabelForm.php b/src/Form/AutoEntityLabelForm.php index e797ddd..5cf79e0 100644 --- a/src/Form/AutoEntityLabelForm.php +++ b/src/Form/AutoEntityLabelForm.php @@ -239,6 +239,13 @@ class AutoEntityLabelForm extends ConfigFormBase { $form['auto_entitylabel']['pattern']['#description'] .= ' ' . $this->t('To get a list of available tokens install Token module.', [':drupal-token' => 'https://www.drupal.org/project/token']); } + $form['auto_entitylabel']['enforce_resaving'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Generate by re-saving'), + '#description' => $this->t('The label will be generated by re-saving the entity. This will allow the entity ID to be used in tokens.'), + '#default_value' => $config->get('enforce_resaving'), + ]; + $form['auto_entitylabel']['escape'] = [ '#type' => 'checkbox', '#title' => $this->t('Remove special characters.'), @@ -289,6 +296,7 @@ class AutoEntityLabelForm extends ConfigFormBase { [ 'status', 'pattern', + 'enforce_resaving', 'escape', 'preserve_titles', 'save',