You are here

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

Same name and namespace in other branches
  1. 8 src/Plugin/BusinessRulesAction/ActionSet.php \Drupal\business_rules\Plugin\BusinessRulesAction\ActionSet::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.

\Drupal\business_rules\ItemInterface $action: The current action.

Return value

array The render array.

1 call to ActionSet::formItems()
ActionSet::getSettingsForm in src/Plugin/BusinessRulesAction/ActionSet.php
Return the form array.

File

src/Plugin/BusinessRulesAction/ActionSet.php, line 149

Class

ActionSet
Class ActionSet.

Namespace

Drupal\business_rules\Plugin\BusinessRulesAction

Code

public function formItems(array $form, FormStateInterface $form_state, ItemInterface $action) {
  $user_input = $form_state
    ->getUserInput();
  $label = t('Item');
  $raw_items = $action
    ->getSettings('items');
  $items = [];
  if (is_array($raw_items)) {
    foreach ($raw_items as $key => $item) {
      $itemObj = new BusinessRulesItemObject($key, $item['type'], $item['weight']);
      $items[$key] = $itemObj;
    }
    uasort($items, function ($a, $b) {
      return $a
        ->getWeight() < $b
        ->getWeight() ? -1 : 1;
    });
  }
  $header = [
    'item_type' => t('Type'),
    'label' => $label,
    'weight' => t('Weight'),
    'id' => t('Machine name'),
    'subtype' => t('Subtype'),
    'description' => t('Description'),
    'operations' => t('Operations'),
    'type' => [
      'data' => '',
      'width' => '0px',
    ],
  ];
  $table['items'] = [
    '#type' => 'table',
    '#header' => $header,
    '#attributes' => [
      'id' => 'business_rules-items',
    ],
    '#empty' => t('There are currently no actions in this action set. Add one by selecting an option below.'),
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'items-order-weight',
      ],
    ],
  ];
  if (is_array($items)) {
    foreach ($items as $value) {
      $item = Action::load($value
        ->getId());
      $item_weight = !empty($item) ? $value
        ->getWeight() : '';
      $route_remove_item = 'business_rules.action_set.items.remove';
      if (!empty($item)) {
        $key = $item
          ->id();
        $listBuilder = $this->entityTypeManager
          ->getListBuilder($item
          ->getEntityTypeId());
        $operations = $listBuilder
          ->buildOperations($item);
        $operations['#links']['remove'] = [
          'title' => t('Remove'),
          'url' => Url::fromRoute($route_remove_item, [
            'action_id' => $action
              ->id(),
            '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' => t('Weight for item'),
          '#title_display' => 'invisible',
          '#delta' => 100,
          '#default_value' => $item_weight,
          '#attributes' => [
            'class' => [
              'items-order-weight',
            ],
          ],
        ];
        $table['items'][$key] = [
          '#attributes' => [
            'class' => 'draggable',
            'id' => $item
              ->id(),
          ],
          '#weight' => isset($user_input['effects']) ? $user_input['effects'][$key]['weight'] : NULL,
          'item_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,
          'type' => [
            '#type' => 'value',
            '#value' => $value
              ->getType(),
          ],
        ];
      }
    }
  }
  $add_action = Link::createFromRoute(t('Add Action'), 'business_rules.action_set.items.table', [
    'action_id' => $action
      ->id(),
    'method' => 'nojs',
  ], [
    'attributes' => [
      'class' => [
        'use-ajax',
      ],
    ],
  ]);
  $table['add'][] = [
    'data' => [
      'add' => [
        '#type' => 'markup',
        '#markup' => $add_action
          ->toString(),
        '#prefix' => '<div id="business_rule-add_buttons">',
        '#suffix' => '</div>',
      ],
    ],
  ];
  return $table;
}