You are here

function commerce_pado_entity_view_display_form_validate in Commerce Product Add-on 8

Form validate callback form the view display edit form.

Checking if a product add-on field has been selected for the commerce_pado_add_to_cart field formatter.

1 string reference to 'commerce_pado_entity_view_display_form_validate'
commerce_pado_form_entity_view_display_edit_form_alter in ./commerce_pado.module
Implements hook_form_FORM_ID_alter().

File

./commerce_pado.module, line 76
Commerce Product Add On module file.

Code

function commerce_pado_entity_view_display_form_validate(&$form, FormStateInterface $form_state) {
  if ($form_state
    ->getTriggeringElement()['#array_parents'] == [
    'actions',
    'submit',
  ]) {

    //** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $entity
    $entity = $form_state
      ->getFormObject()
      ->getEntity();
    $content = $entity
      ->get('content');
    if (!empty($content['variations']) && $content['variations']['type'] === 'commerce_pado_add_to_cart') {
      if (empty($content['variations']['settings']['add_on_field'])) {
        $form_state
          ->setError($form['fields']['variations']['plugin']['type'], t('Please select a product add-on field.'));
      }
    }
  }
}