You are here

public function SiteSettingEntityForm::validateForm in Site Settings and Labels 8

Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level validation handler, otherwise an exception will be thrown.

Overrides ContentEntityForm::validateForm

File

src/Form/SiteSettingEntityForm.php, line 89

Class

SiteSettingEntityForm
Form controller for Site Setting edit forms.

Namespace

Drupal\site_settings\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  $entity = $this->entity;
  $entity_bundle = $entity
    ->bundle();
  $entity_type = $entity
    ->getEntityType()
    ->getBundleEntityType();

  // Get existing entities in this settings bundle.
  $query = $this->entityTypeManager
    ->getStorage('site_setting_entity')
    ->getQuery();
  $query
    ->condition('type', $entity_bundle);
  $existing = $query
    ->execute();
  $bundle = $this->entityTypeManager
    ->getStorage($entity_type)
    ->load($entity_bundle);
  if (!$bundle->multiple) {
    if (count($existing) > 0 && $entity
      ->id() != reset($existing)) {
      $form_state
        ->setErrorByName('name', $this
        ->t('There can only be one of this setting.'));
    }
  }
}