You are here

public static function NameFieldSettingsTrait::validateMinimumComponents in Name Field 8

Helper function to validate minimum components.

Parameters

array $element: Element being validated.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

File

src/Traits/NameFieldSettingsTrait.php, line 342

Class

NameFieldSettingsTrait
Name settings trait.

Namespace

Drupal\name\Traits

Code

public static function validateMinimumComponents(array $element, FormStateInterface $form_state) {
  $minimum_components = $form_state
    ->getValue([
    'settings',
    'minimum_components',
  ]);
  $diff = array_intersect(array_keys(array_filter($minimum_components)), [
    'given',
    'family',
  ]);
  if (count($diff) == 0) {
    $components = array_intersect_key(_name_translations(), array_flip([
      'given',
      'family',
    ]));
    $form_state
      ->setError($element, t('%label must have one of the following components: %components', [
      '%label' => t('Minimum components'),
      '%components' => implode(', ', $components),
    ]));
  }
  $components = $form_state
    ->getValue([
    'settings',
    'components',
  ]);
  $minimum_components = $form_state
    ->getValue([
    'settings',
    'minimum_components',
  ]);
  $diff = array_diff_key(array_filter($minimum_components), array_filter($components));
  if (count($diff)) {
    $components = array_intersect_key(_name_translations(), $diff);
    $form_state
      ->setError($element, t('%components can not be selected for %label when they are not selected for %label2.', [
      '%label' => t('Minimum components'),
      '%label2' => t('Components'),
      '%components' => implode(', ', $components),
    ]));
  }
}