/** * {@inheritdoc} */ public function build() { $build = array(); $route_name = \Drupal::service('path.matcher')->isFrontPage() ? '<front>' : '<current>'; $links = $this->workspaceManager->getWorkspaceSwitchLinks(Url::fromRoute($route_name)); if (isset($links)) { $build = array('#theme' => 'links__workspace_block', '#links' => $links, '#attributes' => array('class' => array('workspace-switcher')), '#set_active_class' => TRUE, '#cache' => ['contexts' => $this->entityTypeManager->getDefinition('workspace')->getListCacheContexts(), 'tags' => $this->entityTypeManager->getDefinition('workspace')->getListCacheTags()]); } return $build; }
/** * {@inheritdoc} */ public function addMultiple(array $entities) { $workspace_id = $this->workspaceId ?: $this->workspaceManager->getActiveWorkspace()->id(); $values = array(); foreach ($entities as $entity) { $key = $this->buildKey($entity); $value = $this->buildValue($entity); $values[$key] = $value; $this->cache[$workspace_id][$key] = $value; } $this->keyValueStore($workspace_id)->setMultiple($values); }
/** * {@inheritdoc} */ protected function loadLinks($menu_name, MenuTreeParameters $parameters) { $links = parent::loadLinks($menu_name, $parameters); $map = []; // Collect all menu_link_content IDs from the links. foreach ($links as $i => $link) { if ($link['provider'] != 'menu_link_content') { continue; } $metadata = unserialize($link['metadata']); $map[$metadata['entity_id']] = $i; } // Load all menu_link_content entities and remove links for the those that // don't belong to the active workspace. $active_workspace_id = $this->workspaceManager->getActiveWorkspace()->id(); $entities = $this->entityTypeManager->getStorage('menu_link_content')->loadMultiple(array_keys($map)); foreach ($map as $entity_id => $link_id) { if (!isset($entities[$entity_id]) || $active_workspace_id != $entities[$entity_id]->workspace->target_id) { unset($links[$link_id]); } } return $links; }
/** * Tests the getWorkspaceSwitchLinks() method. */ public function testGetWorkspaceSwitchLinks() { $second_machine_name = $this->values[1]['machine_name']; $url = Url::fromRoute($this->path); $expected_links = [1 => ['url' => $url, 'title' => $this->defaultMachineName, 'query' => ['workspace' => 1], 'attributes' => ['class' => ['session-active']]], 2 => ['url' => $url, 'title' => $second_machine_name, 'query' => ['workspace' => 2]]]; foreach ($this->values as $key => $value) { $this->entities[$key]->expects($this->any())->method('id')->will($this->returnValue($value['id'])); $this->entities[$key]->expects($this->any())->method('label')->will($this->returnValue($value['label'])); } $this->negotiator = $this->getMock('\\Drupal\\multiversion\\Workspace\\SessionWorkspaceNegotiator'); $this->negotiator->expects($this->any())->method('getActiveWorkspace')->with($this->requestStack, $this->entityManager)->will($this->returnValue($this->defaultMachineName)); $storage = $this->getMock('\\Drupal\\Core\\Entity\\EntityStorageInterface'); $storage->expects($this->any())->method('loadMultiple')->with()->will($this->returnValue($this->entities)); $this->entityManager->expects($this->any())->method('getStorage')->with('workspace')->will($this->returnValue($storage)); $this->workspaceManager->expects($this->any())->method('loadMultiple')->with()->will($this->returnValue(array($this->entities))); $workspace_manager = new WorkspaceManager($this->requestStack, $this->entityManager, $this->cacheRender); $workspace_manager->addNegotiator($this->workspaceNegotiator, 1); $workspace_manager->setActiveWorkspace($this->entities[0]); $negotiator = new SessionWorkspaceNegotiator(); $negotiator->setWorkspaceManager($workspace_manager); $links = $negotiator->getWorkspaceSwitchLinks($this->request, $url); $this->assertSame($expected_links, $links); }
/** * Helper method to decorate a cache ID. * * @param string $cid * @return string */ protected function decorate($cid) { return "{$cid}:" . $this->workspaceManager->getActiveWorkspace()->id(); }
/** * @param string $uuid * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface */ protected function keyValueStore($uuid) { $workspace_id = $this->workspaceId ?: $this->workspaceManager->getActiveWorkspace()->id(); return $this->keyValueFactory->get("entity.index.rev.tree.{$workspace_id}.{$uuid}"); }
/** * {@inheritdoc} */ public function setActiveWorkspaceId($id) { $workspace = $this->workspaceManager->load($id); return $this->workspaceManager->setActiveWorkspace($workspace); }
/** * @return \Drupal\key_value\KeyValueStore\KeyValueStoreSortedSetInterface */ protected function sortedSetStore() { $workspace_id = $this->workspaceId ?: $this->workspaceManager->getActiveWorkspace()->id(); return $this->sortedSetFactory->get($this->collectionPrefix . $workspace_id); }
/** * {@inheritdoc} */ public function getContext() { return 'ws.' . $this->workspaceManager->getActiveWorkspace()->id(); }