You are here

public function SiteSettingReplicateForm::machineNameExists in Site Settings and Labels 8

File

src/Form/SiteSettingReplicateForm.php, line 194

Class

SiteSettingReplicateForm
Class SiteSettingReplicateForm.

Namespace

Drupal\site_settings\Form

Code

public function machineNameExists($value, array $form, FormStateInterface $form_state) {
  $matches = 0;

  // Check if exists.
  if ($existing = $this->entityTypeManager
    ->getStorage('site_setting_entity_type')
    ->loadMultiple()) {
    if (in_array($value, $existing)) {
      $matches++;
    }
  }

  // Check if the new ones we are adding match.
  $values = $form_state
    ->getValues();
  foreach ($values['new_settings'] as $key => $setting) {
    if ($setting['machine_name'] == $value) {
      $matches++;
    }
  }

  // 1 match is allowed as that is self.
  if ($matches > 1) {
    return TRUE;
  }
  else {
    return FALSE;
  }
}