You are here

public function KanbanService::getEntitiesByEntityIds in Content Planner 8

Gets the entities by Type.

Parameters

array $entityIds: An array with the entity ids.

Return value

array Returns a array with the entities for the given entity ids.

File

modules/content_kanban/src/KanbanService.php, line 291

Class

KanbanService
Class KanbanService.

Namespace

Drupal\content_kanban

Code

public function getEntitiesByEntityIds(array $entityIds = [], $filters = []) {
  $result = [];

  // Basic table.
  if (!empty($entityIds)) {
    $query = [];

    // Build the query dynamically for all entities.
    foreach ($entityIds as $entityTypeName => $entityId) {
      try {
        $entityStorage = $this->entityTypeManager
          ->getStorage($entityTypeName);
        $entityKeys = $entityStorage
          ->getEntityType()
          ->getKeys();
        $query[$entityTypeName] = $this->database
          ->select($entityTypeName . '_field_data', 'nfd');
        $query[$entityTypeName]
          ->addField('nfd', $entityKeys['id']);
        $query[$entityTypeName]
          ->addField('nfd', $entityKeys['label']);
        $query[$entityTypeName]
          ->addField('nfd', 'created');
        $query[$entityTypeName]
          ->addField('nfd', 'status');
        $query[$entityTypeName]
          ->addField('nfd', 'type');
        if ($filters['content_type']) {
          $query[$entityTypeName]
            ->condition('nfd.type', $filters['content_type']);
        }

        // Join with users table to get the username who added the entity.
        $query[$entityTypeName]
          ->addField('ufd', 'name', 'username');
        $query[$entityTypeName]
          ->addField('nfd', $entityKeys['uid']);
        $query[$entityTypeName]
          ->condition('nfd.' . $entityKeys['id'], $entityIds[$entityTypeName], 'in');
        $query[$entityTypeName]
          ->innerJoin('users_field_data', 'ufd', 'nfd.' . $entityKeys['uid'] . ' = ufd.uid');

        // Filter by Starting Date.
        if (KanbanFilterForm::getDateRangeFilter()) {
          $searchFromTime = time() - 86400 * KanbanFilterForm::getDateRangeFilter();

          //@todo how about non mysql systems?
          $query[$entityTypeName]
            ->condition('nfd.created', $searchFromTime, '>=');
        }

        // Sort.
        if ($this->database
          ->schema()
          ->fieldExists($entityTypeName . '_field_data', 'publish_on') && $this
          ->contentCalendarModuleIsEnabled()) {
          $query[$entityTypeName]
            ->orderBy('nfd.publish_on', 'ASC');
        }
        else {
          $query[$entityTypeName]
            ->orderBy('nfd.created', 'ASC');
        }
        $result[$entityTypeName] = $query[$entityTypeName]
          ->execute()
          ->fetchAll();
      } catch (InvalidPluginDefinitionException $e) {
        watchdog_exception('content_kanban', $e);
      } catch (PluginNotFoundException $e) {
        watchdog_exception('content_kanban', $e);
      }
    }
  }
  if ($result) {
    return $result;
  }
  return [];
}