You are here

public function ConditionForm::formItems in Business Rules 2.x

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

Provide the form fields for add Business Rule's Items.

Parameters

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

string $items_type: The items type: success|fail.

Return value

array The render array.

1 call to ConditionForm::formItems()
ConditionForm::form in src/Form/ConditionForm.php
Gets the actual form array to be built.

File

src/Form/ConditionForm.php, line 139

Class

ConditionForm
Class ConditionForm.

Namespace

Drupal\business_rules\Form

Code

public function formItems(array $form, FormStateInterface $form_state, $items_type) {

  /** @var \Drupal\business_rules\Entity\Condition $condition */
  $condition = $this->entity;
  $user_input = $form_state
    ->getUserInput();
  $label = $this
    ->t('Item');
  $label_plural = $this
    ->t('Items');
  if ($items_type == 'success') {
    $items = $condition
      ->getSuccessItems();
  }
  elseif ($items_type == 'fail') {
    $items = $condition
      ->getFailItems();
  }
  uasort($items, function ($a, $b) {
    return $a
      ->getWeight() < $b
      ->getWeight() ? -1 : 1;
  });
  $header = [
    'type' => $this
      ->t('Type'),
    'label' => $label,
    'weight' => $this
      ->t('Weight'),
    'id' => $this
      ->t('Machine name'),
    'subtype' => $this
      ->t('Subtype'),
    'description' => $this
      ->t('Description'),
    'operations' => $this
      ->t('Operations'),
    'business_rule_item_type' => [
      'data' => '',
      'width' => '0px',
    ],
  ];
  $table[$items_type] = [
    '#type' => 'table',
    '#header' => $header,
    '#attributes' => [
      'id' => 'business_rules-items-' . $items_type,
    ],
    '#empty' => $this
      ->t('There are currently no @label in this condition. Add one by selecting an option below.', [
      '@label' => $label_plural,
    ]),
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => $items_type . '-items-order-weight',
      ],
    ],
  ];
  if (is_array($items)) {
    foreach ($items as $value) {
      if ($value
        ->getType() == 'condition') {
        $item = Condition::load($value
          ->getId());
      }
      elseif ($value
        ->getType() == 'action') {
        $item = Action::load($value
          ->getId());
      }
      $item_weight = !empty($item) ? $value
        ->getWeight() : '';
      $route_remove_item = 'business_rules.condition.items.remove';
      if (!empty($item)) {
        $key = $item
          ->id();
        $listBuilder = $this->entityTypeManager
          ->getListBuilder($item
          ->getEntityTypeId());
        $operations = $listBuilder
          ->buildOperations($item);
        $operations['#links']['remove'] = [
          'title' => $this
            ->t('Remove'),
          'url' => Url::fromRoute($route_remove_item, [
            'condition_id' => $condition
              ->id(),
            'condition_item_type' => $items_type,
            'item_type' => $value
              ->getType(),
            'item_id' => $item
              ->id(),
            'method' => 'nojs',
          ], [
            'attributes' => [
              'class' => [
                'use-ajax',
              ],
            ],
          ]),
          'weight' => 1,
        ];
        uasort($operations['#links'], function ($a, $b) {
          return $a['weight'] < $b['weight'] ? -1 : 1;
        });
        foreach ($operations['#links'] as $i => $link) {
          $uri = $this->util
            ->getCurrentUri()
            ->toString();
          $operations['#links'][$i]['url']
            ->setRouteParameter('destination', $uri);
        }
        $item_weight = $this
          ->generateItemWeight('item', $item_weight);
        $weight = [
          '#type' => 'weight',
          '#title' => $this
            ->t('Weight for item'),
          '#title_display' => 'invisible',
          '#delta' => 100,
          '#default_value' => $item_weight,
          '#attributes' => [
            'class' => [
              $items_type . '-items-order-weight',
            ],
          ],
        ];
        $table[$items_type][$key] = [
          '#attributes' => [
            'class' => 'draggable',
            'id' => $items_type . '-' . $item
              ->id(),
          ],
          '#weight' => isset($user_input['effects']) ? $user_input['effects'][$key]['weight'] : NULL,
          'type' => [
            '#markup' => $item
              ->getBusinessRuleItemTranslatedType(),
          ],
          'item' => [
            '#tree' => FALSE,
            'label' => [
              '#markup' => $item
                ->label(),
            ],
          ],
          'weight' => $weight,
          'id' => [
            '#markup' => $item
              ->id(),
          ],
          'subtype' => [
            '#markup' => $item
              ->getTypeLabel(),
          ],
          'description' => [
            '#markup' => $item
              ->getDescription(),
          ],
          'operations' => $operations,
          'business_rule_item_type' => [
            '#type' => 'value',
            '#value' => $value
              ->getType(),
          ],
        ];
      }
    }
  }
  $add_condition = Link::createFromRoute($this
    ->t('Add Condition'), 'business_rules.condition.items.table', [
    'condition_id' => $condition
      ->id(),
    'item_type' => 'condition',
    'condition_item_type' => $items_type,
    'method' => 'nojs',
  ], [
    'attributes' => [
      'class' => [
        'use-ajax',
      ],
    ],
  ]);
  $add_action = Link::createFromRoute($this
    ->t('Add Action'), 'business_rules.condition.items.table', [
    'condition_id' => $condition
      ->id(),
    'item_type' => 'action',
    'condition_item_type' => $items_type,
    'method' => 'nojs',
  ], [
    'attributes' => [
      'class' => [
        'use-ajax',
      ],
    ],
  ]);
  $table['add'][] = [
    'data' => [
      'add' => [
        '#type' => 'markup',
        '#markup' => $add_condition
          ->toString() . ' | ' . $add_action
          ->toString(),
        '#prefix' => '<div id="' . $items_type . '-business_rule-add_buttons">',
        '#suffix' => '</div>',
      ],
    ],
  ];
  return $table;
}