You are here

public static function DynamicEntityReferenceItem::storageSettingsFormValidate in Dynamic Entity Reference 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php \Drupal\dynamic_entity_reference\Plugin\Field\FieldType\DynamicEntityReferenceItem::storageSettingsFormValidate()

Form element validation for storage settings.

Parameters

array $element: The form element .

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

array $form: The complete form.

File

src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php, line 240

Class

DynamicEntityReferenceItem
Defines the 'dynamic_entity_reference' entity field type.

Namespace

Drupal\dynamic_entity_reference\Plugin\Field\FieldType

Code

public static function storageSettingsFormValidate(array &$element, FormStateInterface $form_state, array $form) {
  $labels = \Drupal::service('entity_type.repository')
    ->getEntityTypeLabels(TRUE);
  $options = array_filter(array_keys($labels[(string) t('Content', [], [
    'context' => 'Entity type group',
  ])]), function ($entity_type_id) {
    return static::entityHasIntegerId($entity_type_id);
  });
  $exclude_entity_types = $form_state
    ->getValue([
    'settings',
    'exclude_entity_types',
  ], 0);
  $entity_type_ids = $form_state
    ->getValue([
    'settings',
    'entity_type_ids',
  ], []);
  $diff = array_diff($options, $entity_type_ids);
  if (!$exclude_entity_types && empty($entity_type_ids) || $exclude_entity_types && empty($diff)) {
    $form_state
      ->setError($element, t('Select at least one entity type ID.'));
  }
}