Skip to content

Instantly share code, notes, and snippets.

@Seorusus
Created November 3, 2022 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Seorusus/8d5920a884da74f037c31a9cfdc4daa1 to your computer and use it in GitHub Desktop.
Save Seorusus/8d5920a884da74f037c31a9cfdc4daa1 to your computer and use it in GitHub Desktop.
Валидация поля. Если количество элементов менее 2. Получение количества IDs
/**
* Validate Carousel gallery paragraph.
*
* @param array $element
* Reference form array.
*
* @param \Drupal\Core\Form\FormStateInterface $formState
* Form state object.
*/
function basf_site_validate_carousel_gallery(
array &$element,
FormStateInterface $formState
): void {
$subItems = $element["subform"]["field_gallery_slides"]["widget"]["selection"];
// Gets count of slides.
$str = implode(",", array_keys($subItems));
$subItems = preg_replace('/[^0-9.]+/', '', $str);
if (count(str_split($subItems)) < 2) {
$error = t('At least 2 media sliders is required.');
$formState->setError($element, $error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment