You are here

public function Kanban::build in Content Planner 8

Build.

Return value

array Returns a renderable array with the build of the Kanban board.

File

modules/content_kanban/src/Component/Kanban.php, line 190

Class

Kanban
The main Kanban class.

Namespace

Drupal\content_kanban\Component

Code

public function build() {
  $columns = [];

  // Get all Entity Type configs.
  $entityTypeConfigs = $this->kanbanService
    ->getEntityTypeConfigs($this->entityTypes);

  // Get User ID filter.
  $filter_uid = KanbanFilterForm::getUserIdFilter();

  // If the user cannot edit any content, hide the Filter form.
  if (!$this->currentUser
    ->hasPermission('manage any content with content kanban')) {
    $filter_uid = $this->currentUser
      ->id();
  }

  // Get content type filter.
  $filter_content_type = KanbanFilterForm::getContentTypeFilter();

  // Get State filter.
  $filter_state = KanbanFilterForm::getStateFilter();
  foreach ($this->states as $state_id => $state) {

    // If the State filter has been set, only get data which set by the filter.
    if ($filter_state && $filter_state != $state_id) {

      // Add empty Kanban column when the column is filtered.
      $emptyColumn = new KanbanColumn($this->workflowID, $state_id, $state, [], $entityTypeConfigs);
      $columns[] = $emptyColumn
        ->build();
      continue;
    }

    // Prepare filter for the Kanban service.
    $filters = [
      'moderation_state' => $state_id,
    ];

    // Add User filter, if given.
    if ($filter_uid) {
      $filters['uid'] = $filter_uid;
    }
    if ($filter_content_type) {
      $filters['content_type'] = $filter_content_type;
    }
    else {
      $filters['content_type'] = FALSE;
    }

    // Get Entity IDs.
    $multipleEntities = [];
    if ($entityIds = $this->kanbanService
      ->getEntityIdsFromContentModerationEntities($this->workflowID, $filters, $this->entityTypes)) {
      $multipleEntities = $this->kanbanService
        ->getEntitiesByEntityIds($entityIds, $filters);
    }
    $columnEntities = [];
    foreach ($multipleEntities as $entities) {
      $columnEntities = array_merge($columnEntities, $entities);
    }

    // Create Kanban object.
    $kanban_column = new KanbanColumn($this->workflowID, $state_id, $state, $columnEntities, $entityTypeConfigs);

    // Build render array for Kanban.
    $columns[] = $kanban_column
      ->build();
  }

  // Permissions.
  $permissions = [
    'create_entity' => $this
      ->getCreateEntityPermissions($entityTypeConfigs),
  ];

  // Build render array for Kanban.
  $build = [
    '#theme' => 'content_kanban',
    '#kanban_id' => $this->workflowID,
    '#kanban_label' => $this->workflow
      ->label(),
    '#filter_form' => $this
      ->buildFilterForm(),
    '#permissions' => $permissions,
    '#headers' => $this
      ->buildHeaders(),
    '#columns' => $columns,
    '#attached' => [
      'library' => [
        'content_kanban/kanban',
      ],
    ],
  ];
  return $build;
}