You are here

public function WorkbenchAccessSections::page in Workbench Access 8

Returns the section assignment page.

File

src/Controller/WorkbenchAccessSections.php, line 71

Class

WorkbenchAccessSections
Generates the sections list page.

Namespace

Drupal\workbench_access\Controller

Code

public function page(AccessSchemeInterface $access_scheme) {
  $rows = [];
  $tree = $access_scheme
    ->getAccessScheme()
    ->getTree();
  foreach ($tree as $id => $data) {

    // @TODO: Move to a theme function?
    // @TODO: format plural
    foreach ($data as $item_id => $item) {
      $editor_count = count($this->userSectionStorage
        ->getEditors($access_scheme, $item_id));
      $role_count = count($this->roleSectionStorage
        ->getRoles($access_scheme, $item_id));
      $row = [];
      $row[] = str_repeat('-', $item['depth']) . ' ' . $item['label'];
      $row[] = Link::fromTextAndUrl($this
        ->t('@count editors', [
        '@count' => $editor_count,
      ]), Url::fromRoute('entity.access_scheme.by_user', [
        'access_scheme' => $access_scheme
          ->id(),
        'id' => $item_id,
      ]));
      $row[] = Link::fromTextAndUrl($this
        ->t('@count roles', [
        '@count' => $role_count,
      ]), Url::fromRoute('entity.access_scheme.by_role', [
        'access_scheme' => $access_scheme
          ->id(),
        'id' => $item_id,
      ]));
      $rows[] = $row;
    }
  }
  return [
    '#type' => 'table',
    '#header' => [
      $access_scheme
        ->getPluralLabel(),
      $this
        ->t('Editors'),
      $this
        ->t('Roles'),
    ],
    '#rows' => $rows,
    '#empty' => $this
      ->t('No sections are available.'),
  ];
}