You are here

public function ViewsBulkOperationsViewData::getEntityDefault in Views Bulk Operations (VBO) 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Service/ViewsBulkOperationsViewData.php \Drupal\views_bulk_operations\Service\ViewsBulkOperationsViewData::getEntityDefault()
  2. 8 src/Service/ViewsBulkOperationsViewData.php \Drupal\views_bulk_operations\Service\ViewsBulkOperationsViewData::getEntityDefault()
  3. 8.2 src/Service/ViewsBulkOperationsViewData.php \Drupal\views_bulk_operations\Service\ViewsBulkOperationsViewData::getEntityDefault()

The default entity getter function.

Must work well with standard Drupal core entity views.

Parameters

\Drupal\views\ResultRow $row: Views result row.

string $relationship_id: Id of the view relationship.

\Drupal\views\ViewExecutable $view: The current view object.

Return value

\Drupal\Core\Entity\FieldableEntityInterface The translated entity.

Overrides ViewsBulkOperationsViewDataInterface::getEntityDefault

File

src/Service/ViewsBulkOperationsViewData.php, line 233

Class

ViewsBulkOperationsViewData
Gets Views data needed by VBO.

Namespace

Drupal\views_bulk_operations\Service

Code

public function getEntityDefault(ResultRow $row, $relationship_id, ViewExecutable $view) {
  if ($relationship_id == 'none') {
    if (!empty($row->_entity)) {
      $entity = $row->_entity;
    }
  }
  elseif (isset($row->_relationship_entities[$relationship_id])) {
    $entity = $row->_relationship_entities[$relationship_id];
  }
  else {
    throw new \Exception('Unexpected view result row structure.');
  }
  if (empty($entity)) {
    return;
  }
  if ($entity instanceof TranslatableInterface && $entity
    ->isTranslatable()) {

    // Try to find a field alias for the langcode.
    // Assumption: translatable entities always
    // have a langcode key.
    $language_field = '';
    $langcode_key = $entity
      ->getEntityType()
      ->getKey('langcode');
    $base_table = $view->storage
      ->get('base_table');
    foreach ($view->query->fields as $field) {
      if ($field['field'] === $langcode_key && (empty($field['base_table']) || $field['base_table'] === $base_table)) {
        $language_field = $field['alias'];
        break;
      }
    }
    if (!$language_field) {
      $language_field = $langcode_key;
    }
    if (isset($row->{$language_field})) {
      return $entity
        ->getTranslation($row->{$language_field});
    }
  }
  return $entity;
}