Created
October 26, 2022 16:48
-
-
Save Seorusus/a465ba844c554298eeea8526976e59e1 to your computer and use it in GitHub Desktop.
Валидация формы создания айтема tabs. Мин кол-во айтемов д.быть 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Implements hook_field_widget_paragraphs_form_alter(). | |
*/ | |
function basf_site_field_widget_paragraphs_browser_form_alter( | |
&$element, | |
FormStateInterface $formState, | |
): void | |
{ | |
/** @var \Drupal\Core\Entity\EntityForm $formObject */ | |
$formObject = $formState->getFormObject(); | |
if ($formObject instanceof EntityForm) { | |
$entityType = $formObject->getEntity()->bundle(); | |
} | |
if (isset($element['#paragraph_type'])) { | |
switch ($element['#paragraph_type']) { | |
case 'organism_tabs': | |
switch ($entityType) { | |
case 'overview': | |
$element['#element_validate'][] = 'BasfSiteValidateTabs'; | |
break; | |
} | |
break; | |
} | |
} | |
} | |
/** | |
* Validate Tabs paragraph. | |
* | |
* @param array $referenceForm | |
* Reference form array. | |
* | |
* @param \Drupal\Core\Form\FormStateInterface $formState | |
* Form state object. | |
*/ | |
function BasfSiteValidateTabs(array &$referenceForm, FormStateInterface $formState): void { | |
if (count(array_filter(array_keys($referenceForm['subform']['field_tab_item']['widget']), 'is_int')) < 2) { | |
$str = 'Error'; | |
$formState->setError($referenceForm, $str); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment