You are here

protected function ModifyEntityValues::getViewBundles in Views Bulk Edit 8.2

Helper method to get bundles displayed by the view.

Return value

array Array of entity bundles returned by the current view keyed by entity type IDs.

1 call to ModifyEntityValues::getViewBundles()
ModifyEntityValues::buildConfigurationForm in src/Plugin/Action/ModifyEntityValues.php

File

src/Plugin/Action/ModifyEntityValues.php, line 166

Class

ModifyEntityValues
Modify entity field values.

Namespace

Drupal\views_bulk_edit\Plugin\Action

Code

protected function getViewBundles() {

  // Get a list of all entity types and bundles of the view.
  $bundle_data = [];
  $bundle_info = $this->bundleInfo
    ->getAllBundleInfo();

  // If the list of selected results is available,
  // query db for selected bundles.
  if (!empty($this->context['list'])) {
    $query_data = [];
    foreach ($this->context['list'] as $item) {
      list(, , $entity_type_id, $id, ) = $item;
      $query_data[$entity_type_id][$id] = $id;
    }
    foreach ($query_data as $entity_type_id => $entity_ids) {
      $entityTypeDefinition = $this->entityTypeManager
        ->getDefinition($entity_type_id);
      if ($bundle_key = $entityTypeDefinition
        ->getKey('bundle')) {
        $id_key = $entityTypeDefinition
          ->getKey('id');
        $results = $this->database
          ->select($entityTypeDefinition
          ->getBaseTable(), 'base')
          ->fields('base', [
          $bundle_key,
        ])
          ->condition($id_key, $entity_ids, 'IN')
          ->execute()
          ->fetchCol();
        foreach ($results as $bundle_id) {
          if (!isset($bundle_data[$entity_type_id][$bundle_id])) {
            $bundle_data[$entity_type_id][$bundle_id] = $bundle_info[$entity_type_id][$bundle_id]['label'];
          }
        }
      }
      else {
        $bundle_data[$entity_type_id][$entity_type_id] = '';
      }
    }
  }
  else {

    // Initialize view and VBO view data service.
    $view = Views::getView($this->context['view_id']);
    $view
      ->setDisplay($this->context['display_id']);
    if (!empty($this->context['arguments'])) {
      $view
        ->setArguments($this->context['arguments']);
    }
    if (!empty($this->context['exposed_input'])) {
      $view
        ->setExposedInput($this->context['exposed_input']);
    }
    $view
      ->build();
    $this->viewDataService
      ->init($view, $view
      ->getDisplay(), $this->context['relationship_id']);

    // If administrator chose this method, get bundles from actual
    // view results.
    // NOTE: This can cause performance problems in case of large result sets!
    if (!empty($this->context['preconfiguration']['get_bundles_from_results'])) {
      $entities = [];
      if (empty($this->context['list'])) {
        $view->query
          ->setLimit(0);
        $view->query
          ->setOffset(0);
        $view->query
          ->execute($view);
        foreach ($view->result as $row) {
          $entities[] = $this->viewDataService
            ->getEntity($row);
        }
      }
      else {
        foreach ($this->context['list'] as $item) {
          $entities[] = $this->actionProcessor
            ->getEntity($item);
        }
      }
      if (!empty($entities)) {
        foreach ($entities as $entity) {
          $entity_type_id = $entity
            ->getEntityTypeId();
          $bundle = $entity
            ->bundle();
          if (!isset($bundle_data[$entity_type_id][$bundle])) {
            $bundle_data[$entity_type_id][$bundle] = $bundle_info[$entity_type_id][$bundle]['label'];
          }
        }
      }
    }
    else {

      // Try to get possible bundles from a bundle filter, fixed or exposed,
      // if exists (hopefully).
      foreach ($this->viewDataService
        ->getEntityTypeIds() as $entity_type_id) {
        $entityTypeDefinition = $this->entityTypeManager
          ->getDefinition($entity_type_id);
        $bundle_key = $entityTypeDefinition
          ->getKey('bundle');
        if (isset($view->filter[$bundle_key]) && !empty($view->filter[$bundle_key]->value)) {
          foreach ($view->filter[$bundle_key]->value as $bundle) {
            $bundle_data[$entity_type_id][$bundle] = $bundle_info[$entity_type_id][$bundle]['label'];
          }
        }
        elseif (empty($this->context['preconfiguration']['get_bundles_from_results'])) {
          if (isset($bundle_info[$entity_type_id])) {
            foreach ($bundle_info[$entity_type_id] as $bundle => $label) {
              $bundle_data[$entity_type_id][$bundle] = $bundle_info[$entity_type_id][$bundle]['label'];
            }
          }
        }
      }
    }
  }
  return $bundle_data;
}