Skip to content

Instantly share code, notes, and snippets.

@Seorusus
Created October 26, 2022 16:48
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/a465ba844c554298eeea8526976e59e1 to your computer and use it in GitHub Desktop.
Save Seorusus/a465ba844c554298eeea8526976e59e1 to your computer and use it in GitHub Desktop.
Валидация формы создания айтема tabs. Мин кол-во айтемов д.быть 2
/**
* 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