You are here

public function BusinessRulesUtil::getAddItemsForm in Business Rules 8

Same name and namespace in other branches
  1. 2.x src/Util/BusinessRulesUtil.php \Drupal\business_rules\Util\BusinessRulesUtil::getAddItemsForm()

Get an render array for add items form.

Parameters

\Drupal\business_rules\ItemInterface $item: The Business Rule Item.

array $items: The array of items to render inside the table form.

array $selected_items: The current selected items.

string $label: The item label.

string $label_plural: The item label in plural.

\Drupal\Core\Url $back_url: The return Url.

Return value

array The render array.

File

src/Util/BusinessRulesUtil.php, line 287

Class

BusinessRulesUtil
Class BusinessRulesUtil.

Namespace

Drupal\business_rules\Util

Code

public function getAddItemsForm(ItemInterface $item, array $items, array $selected_items, $label, $label_plural, Url $back_url) {
  $form['#title'] = $this
    ->t('Add @label_plural on %parent', [
    '%parent' => $item
      ->label(),
    '@label_plural' => $label_plural,
  ]);
  $form['#attached']['library'][] = 'system/drupal.system.modules';
  $form['filters'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'table-filter',
        'js-show',
      ],
    ],
  ];
  $form['filters']['text'] = [
    '#type' => 'search',
    '#title' => $this
      ->t('Search'),
    '#size' => 30,
    '#placeholder' => $this
      ->t('Search for a @label key', [
      '@label' => $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' => $label,
      ]),
    ],
  ];
  $header = [
    'label' => $label,
    'id' => $this
      ->t('Machine Name'),
    'type' => $this
      ->t('Type'),
    'description' => $this
      ->t('Description'),
    'filter' => [
      'data' => [
        '#markup' => 'filter',
      ],
      'style' => 'display: none',
    ],
  ];
  $rows = [];
  foreach ($items as $item) {
    $search_string = $item
      ->label() . ' ' . $item
      ->id() . ' ' . $item
      ->getTypeLabel() . ' ' . $item
      ->getDescription();
    $rows[$item
      ->id()] = [
      'label' => [
        'data' => [
          '#markup' => $item
            ->label(),
        ],
      ],
      'id' => [
        'data' => [
          '#markup' => $item
            ->id(),
        ],
      ],
      'type' => [
        'data' => [
          '#markup' => $item
            ->getTypeLabel(),
        ],
      ],
      'description' => [
        'data' => [
          '#markup' => $item
            ->getDescription(),
        ],
      ],
      'filter' => [
        'data' => [
          [
            '#markup' => '<span class="table-filter-text-source">' . $search_string . '</span>',
          ],
        ],
        'style' => [
          'display: none',
        ],
      ],
    ];
  }
  $form['items'] = [
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $rows,
    '#js_select' => FALSE,
    '#default_value' => $selected_items,
    '#attributes' => [
      'class' => [
        'searchable-list',
      ],
    ],
  ];
  $form['actions'] = [
    '#type' => 'actions',
    'submit' => [
      '#type' => 'submit',
      '#value' => $this
        ->t('Save'),
      '#button_type' => 'primary',
    ],
    'back' => [
      '#type' => 'link',
      '#title' => $this
        ->t('Back'),
      '#button_type' => 'danger',
      '#attributes' => [
        'class' => [
          'button',
          'button--danger',
        ],
      ],
      '#url' => $back_url,
    ],
  ];
  return $form;
}