You are here

function auto_entitylabel_prepare_entityform in Automatic Entity Label 8.3

Same name and namespace in other branches
  1. 8.2 auto_entitylabel.module \auto_entitylabel_prepare_entityform()

Prepares the label replacement in the entity form.

Parameters

array $form: Form array.

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity which title will be replaced.

2 calls to auto_entitylabel_prepare_entityform()
auto_entitylabel_form_alter in ./auto_entitylabel.module
Implements hook_form_alter().
auto_entitylabel_inline_entity_form_entity_form_alter in ./auto_entitylabel.module
Implements hook_inline_entity_form_entity_form_alter().

File

./auto_entitylabel.module, line 103
Allows hiding of entity label fields and automatic label creation.

Code

function auto_entitylabel_prepare_entityform(array &$form, ContentEntityInterface $entity) {
  if (empty($form['#auto_entitylabel_processed'])) {
    $decorator = \Drupal::service('auto_entitylabel.entity_decorator');

    /** @var \Drupal\auto_entitylabel\AutoEntityLabelManager $entity */
    $entity = $decorator
      ->decorate($entity);
    $label = $entity
      ->getLabelName();
    $widget =& $form[$label]['widget'][0];
    switch ($entity
      ->getStatus()) {
      case AutoEntityLabelManager::ENABLED:

        // Hide the label field. It will be automatically generated in
        // hook_entity_presave().
        $widget['value']['#type'] = 'hidden';
        $widget['value']['#required'] = FALSE;
        if (empty($widget['value']['#default_value'])) {
          $widget['value']['#default_value'] = '%AutoEntityLabel%';
        }
        break;
      case AutoEntityLabelManager::OPTIONAL:

        // Allow label field to be empty. It will be automatically generated
        // in hook_entity_presave().
        $widget['value']['#required'] = FALSE;
        break;
      case AutoEntityLabelManager::PREFILLED:
        if (empty($widget['value']['#default_value'])) {
          $widget['value']['#default_value'] = $entity
            ->setLabel();
        }
        break;
    }
    $form['#auto_entitylabel_processed'] = TRUE;
  }
}