You are here

public static function InlineEntityFormComplex::requiredField in Inline Entity Form 8

IEF widget #element_validate callback: Required field validation.

File

src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php, line 940

Class

InlineEntityFormComplex
Complex inline widget.

Namespace

Drupal\inline_entity_form\Plugin\Field\FieldWidget

Code

public static function requiredField($element, FormStateInterface $form_state, $form) {
  $ief_id = $element['#ief_id'];
  $children = $form_state
    ->get([
    'inline_entity_form',
    $ief_id,
    'entities',
  ]);
  $has_children = !empty($children);
  $form = $form_state
    ->get([
    'inline_entity_form',
    $ief_id,
    'form',
  ]);
  $form_open = !empty($form);

  // If the add new / add existing form is open, its validation / submission
  // will do the job instead (either by preventing the parent form submission
  // or by adding a new referenced entity).
  if (!$has_children && !$form_open) {

    /** @var \Drupal\Core\Field\FieldDefinitionInterface $instance */
    $instance = $form_state
      ->get([
      'inline_entity_form',
      $ief_id,
      'instance',
    ]);
    $form_state
      ->setError($element, t('@name field is required.', [
      '@name' => $instance
        ->getLabel(),
    ]));
  }
}