/** * Tests _contextual_id_to_links(). */ function testContextualIdToLinks() { $tests = $this->_contextual_links_id_testcases(); foreach ($tests as $test) { $this->assertIdentical(_contextual_id_to_links($test['id']), $test['links']); } }
/** * Returns the requested rendered contextual links. * * Given a list of contextual links IDs, render them. Hence this must be * robust to handle arbitrary input. * * @see contextual_preprocess() * * @return \Symfony\Component\HttpFoundation\JsonResponse * The JSON response. */ public function render(Request $request) { $ids = $request->request->get('ids'); if (!isset($ids)) { throw new BadRequestHttpException(t('No contextual ids specified.')); } $rendered = array(); foreach ($ids as $id) { $element = array('#type' => 'contextual_links', '#contextual_links' => _contextual_id_to_links($id)); $rendered[$id] = $this->container->get('renderer')->renderRoot($element); } return new JsonResponse($rendered); }
/** * Resolves contextual links directly, without the need for javascript. * * @Then I click the contextual link :linkText in the :region region */ public function iClickTheContextualLinkInTheRegion($linkText, $region) { $account = user_load($this->getUserManager()->getCurrentUser()->uid); $links = array(); /** @var \Drupal\Core\Menu\ContextualLinkManager $contextual_links_manager */ $contextual_links_manager = \Drupal::service('plugin.manager.menu.contextual_link'); $session = $this->getSession(); $regionObj = $session->getPage()->find('region', $region); if (!$regionObj) { throw new \Exception(sprintf('No region "%s" found on the page %s.', $region, $session->getCurrentUrl())); } /** @var \Behat\Mink\Element\NodeElement $item */ foreach ($regionObj->findAll('xpath', '//*[@data-contextual-id]') as $item) { $contextual_id = $item->getAttribute('data-contextual-id'); foreach (_contextual_id_to_links($contextual_id) as $group_name => $link) { $route_parameters = $link['route_parameters']; foreach ($contextual_links_manager->getContextualLinkPluginsByGroup($group_name) as $plugin_id => $plugin_definition) { /** @var \Drupal\Core\Menu\ContextualLinkInterface $plugin */ $plugin = $contextual_links_manager->createInstance($plugin_id); $route_name = $plugin->getRouteName(); // Check access. if (!\Drupal::accessManager()->checkNamedRoute($route_name, $route_parameters, $account)) { continue; } /** @var \Drupal\Core\Url $url */ $url = Url::fromRoute($route_name, $route_parameters, $plugin->getOptions())->toRenderArray(); $links[$plugin->getTitle()] = $url['#url']->toString(); } } } if (isset($links[$linkText])) { $session->visit($this->locatePath($links[$linkText])); return; } throw new \Exception(t('Could not find a contextual link %link in the region %region', ['%link' => $linkText, '%region' => $region])); }