You are here

public function BusinessRuleForm::form in Business Rules 8

Same name and namespace in other branches
  1. 2.x src/Form/BusinessRuleForm.php \Drupal\business_rules\Form\BusinessRuleForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

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

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

File

src/Form/BusinessRuleForm.php, line 83

Class

BusinessRuleForm
Class BusinessRuleForm.

Namespace

Drupal\business_rules\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /** @var \Drupal\business_rules\Entity\BusinessRule $business_rule */
  $business_rule = $this->entity;
  if ($this->step === 1 && $business_rule
    ->isNew()) {
    $form['reacts_on'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Reacts on event'),
      '#description' => $this
        ->t('Whenever the event occurs, rule evaluation is triggered.'),
      '#options' => BusinessRule::getEventTypes(),
      '#default_value' => $business_rule
        ->getReactsOn(),
      '#required' => TRUE,
    ];
  }
  if ($this->step > 1 || !$business_rule
    ->isNew()) {
    $reactsOn = $business_rule
      ->getReactsOn() ? $business_rule
      ->getReactsOn() : $form_state
      ->getValue('reacts_on');
    $definition = $this->reactsOnManager
      ->getDefinition($reactsOn);
    $reflection = new \ReflectionClass($definition['class']);
    $custom_rule = $reflection
      ->newInstance($definition, $definition['id'], $definition);
    $form['label_reacts_on'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Reacts on event'),
      '#markup' => $definition['label'],
      '#description' => $definition['description'],
    ];
    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label'),
      '#maxlength' => 255,
      '#default_value' => $business_rule
        ->label(),
      '#description' => $this
        ->t("Label for the Rule."),
      '#required' => TRUE,
    ];
    $form['id'] = [
      '#type' => 'machine_name',
      '#default_value' => $business_rule
        ->id(),
      '#machine_name' => [
        'exists' => '\\Drupal\\business_rules\\Entity\\BusinessRule::load',
      ],
      '#disabled' => !$business_rule
        ->isNew(),
    ];
    $form['description'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Description'),
      '#description' => $this
        ->t('A good description for Business Rule.'),
      '#required' => TRUE,
      '#default_value' => $business_rule
        ->getDescription(),
    ];
    $form['tags'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Tags'),
      '#default_value' => implode(', ', $business_rule
        ->getTags()),
      '#description' => $this
        ->t('List of comma-separated tags.'),
      '#required' => FALSE,
      '#autocomplete_route_name' => 'business_rules.autocomplete_tags',
      '#autocomplete_route_parameters' => [],
    ];
    $form['status'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enabled'),
      '#default_value' => $business_rule
        ->isNew() ? 1 : $business_rule
        ->isEnabled(),
    ];
    $form['entity'] = $this
      ->getEntityInformationForm($definition);

    // Only show items if rule is already saved.
    if (!$business_rule
      ->isNew()) {
      $form['items_container'] = [
        '#type' => 'details',
        '#description' => $this
          ->t('The items are evaluated on the presented order. Drag them to change the order.'),
        '#title' => $this
          ->t('Items'),
        '#open' => TRUE,
      ];
      $form['items_container']['items'] = $this
        ->formItems($form, $form_state);
      if (!$business_rule
        ->isNew()) {
        $form['flowchart'] = [
          '#type' => 'details',
          '#title' => $this
            ->t('Flowchart'),
          '#open' => TRUE,
        ];
        $form['flowchart']['graph'] = $this->chart
          ->getGraph($business_rule);
        $form['#attached']['library'][] = 'business_rules/mxClient';
      }
    }

    // Process the form array by the Plugin.
    $custom_rule
      ->processForm($form, $form_state);
  }
  $form['#validate'][] = '::validateForm';
  return $form;
}