You are here

public function ItemForm::form in Business Rules 2.x

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

Gets the actual form array to be built.

Overrides EntityForm::form

See also

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

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

2 calls to ItemForm::form()
ConditionForm::form in src/Form/ConditionForm.php
Gets the actual form array to be built.
VariableForm::form in src/Form/VariableForm.php
Gets the actual form array to be built.
2 methods override ItemForm::form()
ConditionForm::form in src/Form/ConditionForm.php
Gets the actual form array to be built.
VariableForm::form in src/Form/VariableForm.php
Gets the actual form array to be built.

File

src/Form/ItemForm.php, line 80

Class

ItemForm
Base class for Business rules item.

Namespace

Drupal\business_rules\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $itemManager = $this
    ->getItemManager();
  $form_state
    ->set('business_rules_step', $this->step);

  /** @var \Drupal\business_rules\ItemInterface $item */
  $item = $this->entity;
  $class = get_class($item);
  if ($this->step === 1 && $item
    ->isNew()) {
    $options = $item
      ->getTypes();
    $form['type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Type'),
      '#required' => TRUE,
      '#options' => $options,
      '#weight' => 0,
    ];
  }
  if ($this->step > 1 || !$item
    ->isNew()) {
    $type = $item
      ->getType() ? $item
      ->getType() : $form_state
      ->getValue('type');
    $definition = $itemManager
      ->getDefinition($type);
    $reflection = new \ReflectionClass($definition['class']);
    $custom_item = $reflection
      ->newInstance($definition, $definition['id'], $definition);
    $form['label_type'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Type'),
      '#markup' => $definition['label'],
      '#description' => $definition['description'],
      '#weight' => 10,
    ];
    $form['type'] = [
      '#type' => 'value',
      '#value' => $type,
    ];
    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label'),
      '#maxlength' => 255,
      '#default_value' => $item
        ->label(),
      '#description' => $this
        ->t("Label for the Item."),
      '#required' => TRUE,
      '#weight' => 20,
    ];
    $form['id'] = [
      '#type' => 'machine_name',
      '#default_value' => $item
        ->id(),
      '#machine_name' => [
        'exists' => "\\{$class}::load",
      ],
      '#disabled' => !$item
        ->isNew(),
      '#weight' => 30,
    ];
    $form['description'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Description'),
      '#description' => $this
        ->t('A good description of this Item.'),
      '#required' => TRUE,
      '#default_value' => $item
        ->getDescription(),
      '#weight' => 40,
    ];
    $form['tags'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Tags'),
      '#default_value' => $item
        ->getTags() ? implode(', ', $item
        ->getTags()) : '',
      '#description' => $this
        ->t('List of comma-separated tags.'),
      '#required' => FALSE,
      '#weight' => 41,
      '#autocomplete_route_name' => 'business_rules.autocomplete_tags',
      '#autocomplete_route_parameters' => [],
    ];
    $form['settings'] = $this
      ->getEntityInformationForm($definition);
    $form['settings']['#weight'] = 50;

    // Get the plugin definition form.
    $form['settings'] += $custom_item
      ->getSettingsForm($form, $form_state, $item);

    // Additional item form fields.
    $form['additional_fields'] = [];
    $form['additional_fields']['#weight'] = 60;

    // Show available tokens replacements.
    $form['tokens']['#markup'] = $this
      ->getTokensLink();
    $form['tokens']['#weight'] = 900;
    if ($this->util->moduleHandler
      ->moduleExists('token')) {
      $form['#attached']['library'][] = 'token/token';
      $form['#attached']['library'][] = 'token/jquery.treeTable';
    }

    // Show the available variables.
    $form['variables'] = $this->util
      ->getVariablesDetailsBox($item);
    $form['variables']['#weight'] = 1000;
    $form['business_rules_box'] = $this->util
      ->getUsedByBusinessRulesDetailsBox($item);
    $form['business_rules_box']['#weight'] = 1100;
    $form['conditions_box'] = $this->util
      ->getUsedByConditionsDetailsBox($item);
    $form['conditions_box']['#weight'] = 1110;
    $form['actions_box'] = $this->util
      ->getUsedByActionsDetailsBox($item);
    $form['actions_box']['#weight'] = 1120;
    if (!$item
      ->isNew() && !$item instanceof Variable && ($item instanceof Action && is_array($item
      ->getSettings('items')) && count($item
      ->getSettings('items')) || $item instanceof Condition)) {
      $flowchart = $this->chart
        ->getGraph($item);
      if (count($flowchart)) {
        $form['flowchart'] = [
          '#type' => 'details',
          '#title' => $this
            ->t('Flowchart'),
          '#weight' => 1200,
          '#open' => FALSE,
        ];
        $form['flowchart']['graph'] = $flowchart;
        $form['#attached']['library'][] = 'business_rules/mxClient';
      }
    }
  }
  $form['#attached']['library'][] = 'business_rules/style';
  return $form;
}