Chris@0: cacheBackend = $cache_backend; Chris@0: $this->moduleHandler = $module_handler; Chris@0: $this->languageManager = $language_manager; Chris@0: Chris@0: $this->langcode = $this->languageManager->getCurrentLanguage()->getId(); Chris@0: $this->skipCache = $config->get('views.settings')->get('skip_cache'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets all table data. Chris@0: * Chris@0: * @see https://www.drupal.org/node/2723553 Chris@0: * Chris@0: * @return array Chris@0: * An array of table data. Chris@0: */ Chris@0: public function getAll() { Chris@0: if (!$this->fullyLoaded) { Chris@0: $this->allStorage = $this->getData(); Chris@0: } Chris@0: Chris@0: // Set storage from allStorage outside of the fullyLoaded check to prevent Chris@0: // cache calls on requests that have requested all data to get a single Chris@0: // tables data. Make sure $this->storage is populated in this case. Chris@0: $this->storage = $this->allStorage; Chris@0: return $this->allStorage; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets data for a particular table, or all tables. Chris@0: * Chris@0: * @param string|null $key Chris@0: * The key of the cache entry to retrieve. Defaults to NULL, this will Chris@17: * return all table data. NULL $key deprecated in Drupal 8.2.x and will be Chris@17: * removed in 9.0.0. Use getAll() instead. Chris@0: * Chris@0: * @see https://www.drupal.org/node/2723553 Chris@0: * Chris@0: * @return array Chris@0: * An array of table data. Chris@0: */ Chris@0: public function get($key = NULL) { Chris@0: if (!$key) { Chris@0: return $this->getAll(); Chris@0: } Chris@0: if (!isset($this->storage[$key])) { Chris@0: // Prepare a cache ID for get and set. Chris@0: $cid = $this->baseCid . ':' . $key; Chris@0: $from_cache = FALSE; Chris@0: Chris@0: if ($data = $this->cacheGet($cid)) { Chris@0: $this->storage[$key] = $data->data; Chris@0: $from_cache = TRUE; Chris@0: } Chris@0: // If there is no cached entry and data is not already fully loaded, Chris@0: // rebuild. This will stop requests for invalid tables calling getData. Chris@0: elseif (!$this->fullyLoaded) { Chris@0: $this->allStorage = $this->getData(); Chris@0: } Chris@0: Chris@0: if (!$from_cache) { Chris@0: if (!isset($this->allStorage[$key])) { Chris@0: // Write an empty cache entry if no information for that table Chris@0: // exists to avoid repeated cache get calls for this table and Chris@0: // prevent loading all tables unnecessarily. Chris@0: $this->storage[$key] = []; Chris@0: $this->allStorage[$key] = []; Chris@0: } Chris@0: else { Chris@0: $this->storage[$key] = $this->allStorage[$key]; Chris@0: } Chris@0: Chris@0: // Create a cache entry for the requested table. Chris@0: $this->cacheSet($cid, $this->allStorage[$key]); Chris@0: } Chris@0: } Chris@0: return $this->storage[$key]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets data from the cache backend. Chris@0: * Chris@0: * @param string $cid Chris@0: * The cache ID to return. Chris@0: * Chris@0: * @return mixed Chris@0: * The cached data, if any. This will immediately return FALSE if the Chris@0: * $skipCache property is TRUE. Chris@0: */ Chris@0: protected function cacheGet($cid) { Chris@0: if ($this->skipCache) { Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: return $this->cacheBackend->get($this->prepareCid($cid)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets data to the cache backend. Chris@0: * Chris@0: * @param string $cid Chris@0: * The cache ID to set. Chris@0: * @param mixed $data Chris@0: * The data that will be cached. Chris@0: */ Chris@0: protected function cacheSet($cid, $data) { Chris@0: return $this->cacheBackend->set($this->prepareCid($cid), $data, Cache::PERMANENT, ['views_data', 'config:core.extension']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares the cache ID by appending a language code. Chris@0: * Chris@0: * @param string $cid Chris@0: * The cache ID to prepare. Chris@0: * Chris@0: * @return string Chris@0: * The prepared cache ID. Chris@0: */ Chris@0: protected function prepareCid($cid) { Chris@0: return $cid . ':' . $this->langcode; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets all data invoked by hook_views_data(). Chris@0: * Chris@0: * This is requested from the cache before being rebuilt. Chris@0: * Chris@0: * @return array Chris@0: * An array of all data. Chris@0: */ Chris@0: protected function getData() { Chris@0: $this->fullyLoaded = TRUE; Chris@0: Chris@0: if ($data = $this->cacheGet($this->baseCid)) { Chris@0: return $data->data; Chris@0: } Chris@0: else { Chris@0: $modules = $this->moduleHandler->getImplementations('views_data'); Chris@0: $data = []; Chris@0: foreach ($modules as $module) { Chris@0: $views_data = $this->moduleHandler->invoke($module, 'views_data'); Chris@0: // Set the provider key for each base table. Chris@0: foreach ($views_data as &$table) { Chris@0: if (isset($table['table']) && !isset($table['table']['provider'])) { Chris@0: $table['table']['provider'] = $module; Chris@0: } Chris@0: } Chris@0: $data = NestedArray::mergeDeep($data, $views_data); Chris@0: } Chris@0: $this->moduleHandler->alter('views_data', $data); Chris@0: Chris@0: $this->processEntityTypes($data); Chris@0: Chris@0: // Keep a record with all data. Chris@0: $this->cacheSet($this->baseCid, $data); Chris@0: Chris@0: return $data; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Links tables with 'entity type' to respective generic entity-type tables. Chris@0: * Chris@0: * @param array $data Chris@0: * The array of data to alter entity data for, passed by reference. Chris@0: */ Chris@0: protected function processEntityTypes(array &$data) { Chris@0: foreach ($data as $table_name => $table_info) { Chris@0: // Add in a join from the entity-table if an entity-type is given. Chris@0: if (!empty($table_info['table']['entity type'])) { Chris@0: $entity_table = 'views_entity_' . $table_info['table']['entity type']; Chris@0: Chris@0: $data[$entity_table]['table']['join'][$table_name] = [ Chris@0: 'left_table' => $table_name, Chris@0: ]; Chris@0: $data[$entity_table]['table']['entity type'] = $table_info['table']['entity type']; Chris@0: // Copy over the default table group if we have none yet. Chris@0: if (!empty($table_info['table']['group']) && empty($data[$entity_table]['table']['group'])) { Chris@0: $data[$entity_table]['table']['group'] = $table_info['table']['group']; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Fetches a list of all base tables available. Chris@0: * Chris@0: * @return array Chris@0: * An array of base table data keyed by table name. Each item contains the Chris@0: * following keys: Chris@0: * - title: The title label for the base table. Chris@0: * - help: The help text for the base table. Chris@0: * - weight: The weight of the base table. Chris@0: */ Chris@0: public function fetchBaseTables() { Chris@0: $tables = []; Chris@0: Chris@0: foreach ($this->get() as $table => $info) { Chris@0: if (!empty($info['table']['base'])) { Chris@0: $tables[$table] = [ Chris@0: 'title' => $info['table']['base']['title'], Chris@0: 'help' => !empty($info['table']['base']['help']) ? $info['table']['base']['help'] : '', Chris@0: 'weight' => !empty($info['table']['base']['weight']) ? $info['table']['base']['weight'] : 0, Chris@0: ]; Chris@0: } Chris@0: } Chris@0: Chris@0: // Sorts by the 'weight' and then by 'title' element. Chris@0: uasort($tables, function ($a, $b) { Chris@0: if ($a['weight'] != $b['weight']) { Chris@0: return $a['weight'] < $b['weight'] ? -1 : 1; Chris@0: } Chris@0: if ($a['title'] != $b['title']) { Chris@0: return $a['title'] < $b['title'] ? -1 : 1; Chris@0: } Chris@0: return 0; Chris@0: }); Chris@0: Chris@0: return $tables; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Clears the class storage and cache. Chris@0: */ Chris@0: public function clear() { Chris@0: $this->storage = []; Chris@0: $this->allStorage = []; Chris@0: $this->fullyLoaded = FALSE; Chris@0: Cache::invalidateTags(['views_data']); Chris@0: } Chris@0: Chris@0: }