class ViewsData
Same name in this branch
- 10 core/modules/content_moderation/src/ViewsData.php \Drupal\content_moderation\ViewsData
Same name in other branches
- 9 core/modules/content_moderation/src/ViewsData.php \Drupal\content_moderation\ViewsData
- 9 core/modules/views/src/ViewsData.php \Drupal\views\ViewsData
- 8.9.x core/modules/content_moderation/src/ViewsData.php \Drupal\content_moderation\ViewsData
- 8.9.x core/modules/views/src/ViewsData.php \Drupal\views\ViewsData
- 11.x core/modules/content_moderation/src/ViewsData.php \Drupal\content_moderation\ViewsData
- 11.x core/modules/views/src/ViewsData.php \Drupal\views\ViewsData
Class to manage and lazy load cached views data.
If a table is requested and cannot be loaded from cache, all data is then requested from cache. A table-specific cache entry will then be created for the requested table based on this cached data. Table data is only rebuilt when no cache entry for all table data can be retrieved.
Hierarchy
- class \Drupal\views\ViewsData
Expanded class hierarchy of ViewsData
8 files declare their use of ViewsData
- HandlerBase.php in core/
modules/ views/ src/ Plugin/ views/ HandlerBase.php - ResultTest.php in core/
modules/ views/ tests/ src/ Unit/ Plugin/ area/ ResultTest.php - SqlTest.php in core/
modules/ views/ tests/ src/ Unit/ Plugin/ query/ SqlTest.php - ViewsDataTest.php in core/
modules/ views/ tests/ src/ Unit/ ViewsDataTest.php - ViewsEntityRow.php in core/
modules/ views/ src/ Plugin/ Derivative/ ViewsEntityRow.php
3 string references to 'ViewsData'
- views.services.yml in core/
modules/ views/ views.services.yml - core/modules/views/views.services.yml
- ViewsDataTest::setupMockedModuleHandler in core/
modules/ views/ tests/ src/ Unit/ ViewsDataTest.php - Mocks the basic module handler used for the test.
- ViewsDataTest::testFullAndTableGetCache in core/
modules/ views/ tests/ src/ Unit/ ViewsDataTest.php - Tests the cache of the full and single table data.
1 service uses ViewsData
- views.views_data in core/
modules/ views/ views.services.yml - Drupal\views\ViewsData
File
-
core/
modules/ views/ src/ ViewsData.php, line 20
Namespace
Drupal\viewsView source
class ViewsData {
/**
* The base cache ID to use.
*
* @var string
*/
protected $baseCid = 'views_data';
/**
* The cache backend to use.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cacheBackend;
/**
* Table data storage.
*
* This is used for explicitly requested tables.
*
* @var array
*/
protected $storage = [];
/**
* All table storage data loaded from cache.
*
* This is used when all data has been loaded from the cache to prevent
* further cache get calls when rebuilding all data or for single tables.
*
* @var array
*/
protected $allStorage = [];
/**
* Whether the data has been fully loaded in this request.
*
* @var bool
*/
protected $fullyLoaded = FALSE;
/**
* The current language code.
*
* @var string
*/
protected $langcode;
/**
* Stores a module manager to invoke hooks.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Constructs this ViewsData object.
*
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* The cache backend to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface|\Drupal\Core\Config\ConfigFactoryInterface $module_handler
* The module handler class to use for invoking hooks.
* @param \Drupal\Core\Language\LanguageManagerInterface|\Drupal\Core\Extension\ModuleHandlerInterface $language_manager
* The language manager.
*/
public function __construct(CacheBackendInterface $cache_backend, ModuleHandlerInterface|ConfigFactoryInterface $module_handler, LanguageManagerInterface|ModuleHandlerInterface $language_manager) {
$this->cacheBackend = $cache_backend;
if ($module_handler instanceof ConfigFactoryInterface) {
$this->moduleHandler = $language_manager;
$this->languageManager = func_get_arg(3);
$this->langcode = $this->languageManager
->getCurrentLanguage()
->getId();
@trigger_error('Calling ' . __CLASS__ . '::_construct() with the $config argument is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. See https://www.drupal.org/node/2541974', E_USER_DEPRECATED);
}
else {
$this->moduleHandler = $module_handler;
$this->languageManager = $language_manager;
$this->langcode = $this->languageManager
->getCurrentLanguage()
->getId();
}
}
/**
* Gets all table data.
*
* @return array
* An array of table data.
*/
public function getAll() {
if (!$this->fullyLoaded) {
$this->allStorage = $this->getData();
}
// Set storage from allStorage outside of the fullyLoaded check to prevent
// cache calls on requests that have requested all data to get a single
// tables data. Make sure $this->storage is populated in this case.
$this->storage = $this->allStorage;
return $this->allStorage;
}
/**
* Gets data for a particular table.
*
* @param string $key
* The key of the cache entry to retrieve.
*
* @return array
* An array of table data.
*/
public function get($key) {
if (!$key) {
throw new \InvalidArgumentException('A valid cache entry key is required. Use getAll() to get all table data.');
}
if (!isset($this->storage[$key])) {
// Prepare a cache ID for get and set.
$cid = $this->baseCid . ':' . $key;
$from_cache = FALSE;
if ($data = $this->cacheGet($cid)) {
$this->storage[$key] = $data->data;
$from_cache = TRUE;
}
elseif (!$this->fullyLoaded) {
$this->allStorage = $this->getData();
}
if (!$from_cache) {
if (!isset($this->allStorage[$key])) {
// Write an empty cache entry if no information for that table
// exists to avoid repeated cache get calls for this table and
// prevent loading all tables unnecessarily.
$this->storage[$key] = [];
$this->allStorage[$key] = [];
}
else {
$this->storage[$key] = $this->allStorage[$key];
}
// Create a cache entry for the requested table.
$this->cacheSet($cid, $this->allStorage[$key]);
}
}
return $this->storage[$key];
}
/**
* Gets data from the cache backend.
*
* @param string $cid
* The cache ID to return.
*
* @return mixed
* The cached data.
*/
protected function cacheGet($cid) {
return $this->cacheBackend
->get($this->prepareCid($cid));
}
/**
* Sets data to the cache backend.
*
* @param string $cid
* The cache ID to set.
* @param mixed $data
* The data that will be cached.
*/
protected function cacheSet($cid, $data) {
return $this->cacheBackend
->set($this->prepareCid($cid), $data, Cache::PERMANENT, [
'views_data',
'config:core.extension',
]);
}
/**
* Prepares the cache ID by appending a language code.
*
* @param string $cid
* The cache ID to prepare.
*
* @return string
* The prepared cache ID.
*/
protected function prepareCid($cid) {
return $cid . ':' . $this->langcode;
}
/**
* Gets all data invoked by hook_views_data().
*
* This is requested from the cache before being rebuilt.
*
* @return array
* An array of all data.
*/
protected function getData() {
$this->fullyLoaded = TRUE;
if ($data = $this->cacheGet($this->baseCid)) {
return $data->data;
}
else {
$data = [];
$this->moduleHandler
->invokeAllWith('views_data', function (callable $hook, string $module) use (&$data) {
$views_data = $hook();
// Set the provider key for each base table.
foreach ($views_data as &$table) {
if (isset($table['table']) && !isset($table['table']['provider'])) {
$table['table']['provider'] = $module;
}
}
$data = NestedArray::mergeDeep($data, $views_data);
});
$this->moduleHandler
->alter('views_data', $data);
$this->processEntityTypes($data);
// Keep a record with all data.
$this->cacheSet($this->baseCid, $data);
return $data;
}
}
/**
* Links tables with 'entity type' to respective generic entity-type tables.
*
* @param array $data
* The array of data to alter entity data for, passed by reference.
*/
protected function processEntityTypes(array &$data) {
foreach ($data as $table_name => $table_info) {
// Add in a join from the entity-table if an entity-type is given.
if (!empty($table_info['table']['entity type'])) {
$entity_table = 'views_entity_' . $table_info['table']['entity type'];
$data[$entity_table]['table']['join'][$table_name] = [
'left_table' => $table_name,
];
$data[$entity_table]['table']['entity type'] = $table_info['table']['entity type'];
// Copy over the default table group if we have none yet.
if (!empty($table_info['table']['group']) && empty($data[$entity_table]['table']['group'])) {
$data[$entity_table]['table']['group'] = $table_info['table']['group'];
}
}
}
}
/**
* Fetches a list of all base tables available.
*
* @return array
* An array of base table data keyed by table name. Each item contains the
* following keys:
* - title: The title label for the base table.
* - help: The help text for the base table.
* - weight: The weight of the base table.
*/
public function fetchBaseTables() {
$tables = [];
foreach ($this->getAll() as $table => $info) {
if (!empty($info['table']['base'])) {
$tables[$table] = [
'title' => $info['table']['base']['title'],
'help' => !empty($info['table']['base']['help']) ? $info['table']['base']['help'] : '',
'weight' => !empty($info['table']['base']['weight']) ? $info['table']['base']['weight'] : 0,
];
}
}
// Sorts by the 'weight' and then by 'title' element.
uasort($tables, function ($a, $b) {
if ($a['weight'] != $b['weight']) {
return $a['weight'] <=> $b['weight'];
}
return $a['title'] <=> $b['title'];
});
return $tables;
}
/**
* Clears the class storage and cache.
*/
public function clear() {
$this->storage = [];
$this->allStorage = [];
$this->fullyLoaded = FALSE;
Cache::invalidateTags([
'views_data',
]);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
ViewsData::$allStorage | protected | property | All table storage data loaded from cache. |
ViewsData::$baseCid | protected | property | The base cache ID to use. |
ViewsData::$cacheBackend | protected | property | The cache backend to use. |
ViewsData::$fullyLoaded | protected | property | Whether the data has been fully loaded in this request. |
ViewsData::$langcode | protected | property | The current language code. |
ViewsData::$languageManager | protected | property | The language manager. |
ViewsData::$moduleHandler | protected | property | Stores a module manager to invoke hooks. |
ViewsData::$storage | protected | property | Table data storage. |
ViewsData::cacheGet | protected | function | Gets data from the cache backend. |
ViewsData::cacheSet | protected | function | Sets data to the cache backend. |
ViewsData::clear | public | function | Clears the class storage and cache. |
ViewsData::fetchBaseTables | public | function | Fetches a list of all base tables available. |
ViewsData::get | public | function | Gets data for a particular table. |
ViewsData::getAll | public | function | Gets all table data. |
ViewsData::getData | protected | function | Gets all data invoked by hook_views_data(). |
ViewsData::prepareCid | protected | function | Prepares the cache ID by appending a language code. |
ViewsData::processEntityTypes | protected | function | Links tables with 'entity type' to respective generic entity-type tables. |
ViewsData::__construct | public | function | Constructs this ViewsData object. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.