You are here

public function BusinessRulesItemsController::itemsTable in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Controller/BusinessRulesItemsController.php \Drupal\business_rules\Controller\BusinessRulesItemsController::itemsTable()

The items table.

Parameters

string $business_rule: The Business Rule id.

string $item_type: The item type.

string $method: The method nojs|ajax.

Return value

array|AjaxResponse Render array or AjaxResponse.

1 string reference to 'BusinessRulesItemsController::itemsTable'
business_rules.routing.yml in ./business_rules.routing.yml
business_rules.routing.yml

File

src/Controller/BusinessRulesItemsController.php, line 162

Class

BusinessRulesItemsController
Class BusinessRulesItemsController.

Namespace

Drupal\business_rules\Controller

Code

public function itemsTable($business_rule, $item_type, $method) {
  $this
    ->init($item_type);

  /** @var \Drupal\business_rules\Entity\BusinessRule $rule */
  $rule = BusinessRule::load($business_rule);
  $this->items = $rule
    ->filterContextAvailableItems($this->items);
  $this->savedItems = $rule
    ->getItems();
  $table['#title'] = $this
    ->t('Add @label_plural on %rule', [
    '%rule' => $rule
      ->label(),
    '@label_plural' => $this->labelPlural,
  ]);
  $table['#attached']['library'][] = 'system/drupal.system.modules';
  $table['add_new'] = [
    '#type' => 'markup',
    '#markup' => $this->newItemButton
      ->toString(),
    '#prefix' => '<ul class="action-links"><li>',
    '#suffix' => '</li></ul>',
  ];
  $table['filters'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'table-filter',
        'js-show',
      ],
    ],
  ];
  $table['filters']['text'] = [
    '#type' => 'search',
    '#title' => $this
      ->t('Search'),
    '#size' => 30,
    '#placeholder' => $this
      ->t('Search for a @label key', [
      '@label' => $this->label,
    ]),
    '#attributes' => [
      'class' => [
        'table-filter-text',
      ],
      'data-table' => '.searchable-list',
      'autocomplete' => 'off',
      'title' => $this
        ->t('Enter a part of the @label key to filter by.', [
        '@label' => $this->label,
      ]),
    ],
  ];
  $table['help'] = [
    '#type' => 'markup',
    '#markup' => $this
      ->t('If the Business Rule has Target Entity Type and/or Target Bundle, only items with the same Entity/Bundle as the rule or with no context dependency are visible on this form.'),
  ];
  $header = [
    'label' => $this->label,
    'id' => $this
      ->t('Machine Name'),
    'type' => $this
      ->t('Type'),
    'description' => $this
      ->t('Description'),
    'operations' => $this
      ->t('Operations'),
    'filter' => [
      'data' => [
        '#markup' => 'filter',
      ],
      'style' => 'display: none',
    ],
  ];
  $rows = [];
  foreach ($this->items as $item) {
    if (!in_array($item
      ->id(), array_keys($this->savedItems))) {
      $listBuilder = $this->entityTypeManager
        ->getListBuilder($item
        ->getEntityTypeId());
      $operations = $listBuilder
        ->buildOperations($item);
      foreach ($operations['#links'] as $i => $link) {
        $operations['#links'][$i]['url']
          ->setRouteParameter('destination', $this->util
          ->getPreviousUri()
          ->toString());
      }
      $search_string = $item
        ->label() . ' ' . $item
        ->id() . ' ' . $item
        ->getTypeLabel() . ' ' . $item
        ->getDescription();
      $link = Link::createFromRoute($item
        ->label(), 'business_rules.items.add', [
        'business_rule' => $business_rule,
        'item_id' => $item
          ->Id(),
        'item_type' => $item_type,
      ]);
      $rows[$item
        ->id()] = [
        'label' => [
          'data' => $link,
        ],
        'id' => [
          'data' => [
            '#markup' => $item
              ->id(),
          ],
        ],
        'type' => [
          'data' => [
            '#markup' => $item
              ->getTypeLabel(),
          ],
        ],
        'description' => [
          'data' => [
            '#markup' => $item
              ->getDescription(),
          ],
        ],
        'operations' => [
          'data' => $operations,
        ],
        'filter' => [
          'data' => [
            [
              '#markup' => '<span class="table-filter-text-source">' . $search_string . '</span>',
            ],
          ],
          'style' => [
            'display: none',
          ],
        ],
      ];
    }
  }
  $table['business_rule_items'] = [
    '#type' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#attributes' => [
      'class' => [
        'searchable-list',
      ],
    ],
  ];
  if ($method == 'ajax') {
    $table['#attached']['library'][] = 'core/drupal.dialog.ajax';
    $options = [
      'width' => '75%',
    ];
    $title = $this
      ->t('Add @item', [
      '@item' => $this->label,
    ]);
    $response = new AjaxResponse();
    $response
      ->addCommand(new OpenModalDialogCommand($title, $table, $options));
    return $response;
  }
  else {
    return $table;
  }
}