View source
<?php
namespace Drupal\views\Entity;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\views\Plugin\DependentWithRemovalPluginInterface;
use Drupal\views\Views;
use Drupal\views\ViewEntityInterface;
class View extends ConfigEntityBase implements ViewEntityInterface {
protected $base_table = 'node';
protected $id = NULL;
protected $label;
protected $description = '';
protected $tag = '';
protected $display = [];
protected $base_field = 'nid';
protected $executable;
protected $module = 'views';
public function getExecutable() {
if (!isset($this->executable)) {
$this->executable = Views::executableFactory()
->get($this);
}
return $this->executable;
}
public function createDuplicate() {
$duplicate = parent::createDuplicate();
unset($duplicate->executable);
return $duplicate;
}
public function label() {
if (!($label = $this
->get('label'))) {
$label = $this
->id();
}
return $label;
}
public function addDisplay($plugin_id = 'page', $title = NULL, $id = NULL) {
if (empty($plugin_id)) {
return FALSE;
}
$plugin = Views::pluginManager('display')
->getDefinition($plugin_id);
if (empty($plugin)) {
$plugin['title'] = t('Broken');
}
if (empty($id)) {
$id = $this
->generateDisplayId($plugin_id);
if ($id !== 'default') {
preg_match("/[0-9]+/", $id, $count);
$count = $count[0];
}
else {
$count = '';
}
if (empty($title)) {
$title = $plugin['title'];
if ($count > 1) {
$title .= ' ' . $count;
}
}
}
$display_options = [
'display_plugin' => $plugin_id,
'id' => $id,
'display_title' => (string) $title,
'position' => $id === 'default' ? 0 : count($this->display),
'display_options' => [],
];
$this->display[$id] = $display_options;
return $id;
}
protected function generateDisplayId($plugin_id) {
if ($plugin_id == 'default') {
return 'default';
}
$id = $plugin_id . '_1';
$count = 1;
while (!empty($this->display[$id])) {
$id = $plugin_id . '_' . ++$count;
}
return $id;
}
public function &getDisplay($display_id) {
return $this->display[$display_id];
}
public function duplicateDisplayAsType($old_display_id, $new_display_type) {
$executable = $this
->getExecutable();
$display = $executable
->newDisplay($new_display_type);
$new_display_id = $display->display['id'];
$displays = $this
->get('display');
$display_duplicate = $displays[$old_display_id];
unset($display_duplicate['display_title']);
unset($display_duplicate['display_plugin']);
unset($display_duplicate['new_id']);
$displays[$new_display_id] = NestedArray::mergeDeep($displays[$new_display_id], $display_duplicate);
$displays[$new_display_id]['id'] = $new_display_id;
$this
->set('display', $displays);
$executable
->setDisplay($new_display_id);
$executable->display_handler
->filterByDefinedOptions($displays[$new_display_id]['display_options']);
$this
->set('display', $displays);
return $new_display_id;
}
public function calculateDependencies() {
parent::calculateDependencies();
$this
->addDependency('module', $this->module);
$executable = $this
->getExecutable();
$executable
->initDisplay();
$executable
->initStyle();
foreach ($executable->displayHandlers as $display) {
$this
->calculatePluginDependencies($display);
}
return $this;
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
$displays = $this
->get('display');
ksort($displays);
$this
->set('display', [
'default' => $displays['default'],
] + $displays);
if (!$this
->isSyncing() && !$this
->hasTrustedData()) {
$this
->addCacheMetadata();
}
}
protected function addCacheMetadata() {
$executable = $this
->getExecutable();
$current_display = $executable->current_display;
$displays = $this
->get('display');
foreach (array_keys($displays) as $display_id) {
$display =& $this
->getDisplay($display_id);
$executable
->setDisplay($display_id);
$cache_metadata = $executable
->getDisplay()
->calculateCacheMetadata();
$display['cache_metadata']['max-age'] = $cache_metadata
->getCacheMaxAge();
$display['cache_metadata']['contexts'] = $cache_metadata
->getCacheContexts();
$display['cache_metadata']['tags'] = $cache_metadata
->getCacheTags();
$display['cache_metadata']['contexts'] = Cache::mergeContexts($display['cache_metadata']['contexts'], [
'languages:' . LanguageInterface::TYPE_INTERFACE,
]);
sort($display['cache_metadata']['tags']);
sort($display['cache_metadata']['contexts']);
}
$executable
->setDisplay($current_display);
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
views_invalidate_cache();
$this
->invalidateCaches();
if (!isset($this->original) || $this
->status() != $this->original
->status()) {
\Drupal::service('router.builder')
->setRebuildNeeded();
}
}
public static function postLoad(EntityStorageInterface $storage, array &$entities) {
parent::postLoad($storage, $entities);
foreach ($entities as $entity) {
$entity
->mergeDefaultDisplaysOptions();
}
}
public static function preCreate(EntityStorageInterface $storage, array &$values) {
parent::preCreate($storage, $values);
$values += [
'display' => [
'default' => [
'display_plugin' => 'default',
'id' => 'default',
'display_title' => 'Default',
'position' => 0,
'display_options' => [],
],
],
];
}
public function postCreate(EntityStorageInterface $storage) {
parent::postCreate($storage);
$this
->mergeDefaultDisplaysOptions();
}
public static function preDelete(EntityStorageInterface $storage, array $entities) {
parent::preDelete($storage, $entities);
foreach ($entities as $entity) {
$executable = Views::executableFactory()
->get($entity);
foreach ($entity
->get('display') as $display_id => $display) {
$executable
->setDisplay($display_id);
$executable
->getDisplay()
->remove();
}
}
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
$tempstore = \Drupal::service('tempstore.shared')
->get('views');
foreach ($entities as $entity) {
$tempstore
->delete($entity
->id());
}
}
public function mergeDefaultDisplaysOptions() {
$displays = [];
foreach ($this
->get('display') as $key => $options) {
$options += [
'display_options' => [],
'display_plugin' => NULL,
'id' => NULL,
'display_title' => '',
'position' => NULL,
];
$displays[$key] = $options;
}
$this
->set('display', $displays);
}
public function isInstallable() {
$table_definition = \Drupal::service('views.views_data')
->get($this->base_table);
return $table_definition && isset($table_definition['table']['base']);
}
public function __sleep() {
$keys = parent::__sleep();
unset($keys[array_search('executable', $keys)]);
return $keys;
}
public function invalidateCaches() {
$tags = $this
->getCacheTags();
\Drupal::service('cache_tags.invalidator')
->invalidateTags($tags);
}
public function onDependencyRemoval(array $dependencies) {
$changed = FALSE;
if (isset($dependencies['module']) && in_array('views', $dependencies['module'])) {
return FALSE;
}
$views_data = Views::viewsData();
$base_table = $this
->get('base_table');
$base_table_data = $views_data
->get($base_table);
if (!empty($base_table_data['table']['provider']) && in_array($base_table_data['table']['provider'], $dependencies['module'])) {
return FALSE;
}
$current_display = $this
->getExecutable()->current_display;
$handler_types = Views::getHandlerTypes();
foreach ($this->display as $display_id => $display_plugin_base) {
$this
->getExecutable()
->setDisplay($display_id);
$display = $this
->getExecutable()
->getDisplay();
foreach (array_keys($handler_types) as $handler_type) {
$handlers = $display
->getHandlers($handler_type);
foreach ($handlers as $handler_id => $handler) {
if ($handler instanceof DependentWithRemovalPluginInterface) {
if ($handler
->onDependencyRemoval($dependencies)) {
unset($this->display[$display_id]['display_options'][$handler_types[$handler_type]['plural']][$handler_id]);
$changed = TRUE;
}
}
}
}
}
if ($changed) {
$this
->getExecutable()->current_display = NULL;
$this
->calculateDependencies();
$this
->disable();
}
$this
->getExecutable()
->setDisplay($current_display);
return $changed;
}
}