You are here

public function FieldValidationRuleSetAddForm::form in Field Validation 8

Gets the actual form array to be built.

Overrides FieldValidationRuleSetFormBase::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/FieldValidationRuleSetAddForm.php, line 15

Class

FieldValidationRuleSetAddForm
Controller for FieldValidationRuleSet addition forms.

Namespace

Drupal\field_validation\Form

Code

public function form(array $form, FormStateInterface $form_state, $entity_type = '') {
  $entity_types = \Drupal::entityTypeManager()
    ->getDefinitions();
  $entity_type_options = [
    '' => $this
      ->t('- Select -'),
  ];
  foreach ($entity_types as $key => $entitytype) {
    if ($entitytype instanceof \Drupal\Core\Entity\ContentEntityTypeInterface) {
      $entity_type_options[$key] = $entitytype
        ->getLabel();
    }
    if ($key == 'block_content') {

      // drupal_set_message($key);

      //drupal_set_message(var_export($entity_type, true));
    }
  }
  $form['entity_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Entity Type'),
    '#options' => $entity_type_options,
    '#default_value' => $entity_type,
    '#required' => TRUE,
    '#ajax' => [
      'callback' => '::updateBundle',
      'wrapper' => 'edit-bundle-wrapper',
    ],
  ];
  $default_entity_type = $form_state
    ->getValue('entity_type', $entity_type);

  //$default_entity_type = 'node';
  $form['bundle'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Bundle'),
    //'#default_value' => $form_state->getValue('bundle'),
    '#options' => $this
      ->findBundle($default_entity_type),
    '#required' => TRUE,
    '#prefix' => '<div id="edit-bundle-wrapper">',
    '#suffix' => '</div>',
    '#validated' => TRUE,
  ];
  return parent::form($form, $form_state);
}