ViewsBulkOperationsViewData.php in Views Bulk Operations (VBO) 8
File
src/Service/ViewsBulkOperationsViewData.php
View source
<?php
namespace Drupal\views_bulk_operations\Service;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Views;
use Drupal\views\ResultRow;
use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\views_bulk_operations\ViewsBulkOperationsEvent;
class ViewsBulkOperationsViewData implements ViewsBulkOperationsViewDataInterface {
protected $eventDispatcher;
protected $moduleHandler;
protected $view;
protected $relationship;
protected $data;
protected $entityTypeIds;
protected $entityGetter;
public function __construct(EventDispatcherInterface $eventDispatcher, ModuleHandlerInterface $moduleHandler) {
$this->eventDispatcher = $eventDispatcher;
$this->moduleHandler = $moduleHandler;
}
public function init(ViewExecutable $view, DisplayPluginBase $display, $relationship) {
$this->view = $view;
$this->displayHandler = $display;
$this->relationship = $relationship;
$event = new ViewsBulkOperationsEvent($this
->getViewProvider(), $this
->getData(), $view);
$this->eventDispatcher
->dispatch(ViewsBulkOperationsEvent::NAME, $event);
$this->entityTypeIds = $event
->getEntityTypeIds();
$this->entityGetter = $event
->getEntityGetter();
}
public function getEntityTypeIds() {
return $this->entityTypeIds;
}
protected function getData() {
if (!$this->data) {
$viewsData = Views::viewsData();
if (!empty($this->relationship) && $this->relationship != 'none') {
$relationship = $this->displayHandler
->getOption('relationships')[$this->relationship];
$table_data = $viewsData
->get($relationship['table']);
$this->data = $viewsData
->get($table_data[$relationship['field']]['relationship']['base']);
}
else {
$this->data = $viewsData
->get($this->view->storage
->get('base_table'));
}
}
return $this->data;
}
public function getViewProvider() {
$views_data = $this
->getData();
if (isset($views_data['table']['provider'])) {
return $views_data['table']['provider'];
}
return FALSE;
}
public function getEntity(ResultRow $row) {
if (!empty($this->entityGetter['file'])) {
require_once $this->entityGetter['file'];
}
if (is_callable($this->entityGetter['callable'])) {
return call_user_func($this->entityGetter['callable'], $row, $this->relationship, $this->view);
}
else {
if (is_array($this->entityGetter['callable'])) {
if (is_object($this->entityGetter['callable'][0])) {
$info = get_class($this->entityGetter['callable'][0]);
}
else {
$info = $this->entityGetter['callable'][0];
}
$info .= '::' . $this->entityGetter['callable'][1];
}
else {
$info = $this->entityGetter['callable'];
}
throw new \Exception(sprintf("Entity getter method %s doesn't exist.", $info));
}
}
public function getTotalResults() {
if (isset($this->view->query)) {
$query = $this->view->query
->query();
}
if (!empty($query)) {
$total_results = $query
->countQuery()
->execute()
->fetchField();
}
else {
if (isset($this->view->query) && empty($this->view->result)) {
$this->moduleHandler
->invokeAll('views_pre_execute', [
$this->view,
]);
$this->view->query
->execute($this->view);
}
$total_results = $this->view->total_rows;
}
if ($total_results && ($offset = $this->view->pager
->getOffset())) {
$total_results -= $offset;
}
return $total_results;
}
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 ($entity
->isTranslatable()) {
$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 ($entity instanceof TranslatableInterface && isset($row->{$language_field})) {
return $entity
->getTranslation($row->{$language_field});
}
}
return $entity;
}
}