Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  *
  * For anonymous users, the "active" class will be calculated on the server,
  * because most sites serve each anonymous user the same cached page anyway.
  * For authenticated users, the "active" class will be calculated on the
  * client (through JavaScript), only data- attributes are added to links to
  * prevent breaking the render cache. The JavaScript is added in
  * system_page_attachments().
  *
  * @see system_page_attachments()
  */
 public function generate($text, Url $url, $collect_cacheability_metadata = FALSE)
 {
     // Performance: avoid Url::toString() needing to retrieve the URL generator
     // service from the container.
     $url->setUrlGenerator($this->urlGenerator);
     // Start building a structured representation of our link to be altered later.
     $variables = array('text' => is_array($text) ? drupal_render($text) : $text, 'url' => $url, 'options' => $url->getOptions());
     // Merge in default options.
     $variables['options'] += array('attributes' => array(), 'query' => array(), 'language' => NULL, 'set_active_class' => FALSE, 'absolute' => FALSE);
     // Add a hreflang attribute if we know the language of this link's url and
     // hreflang has not already been set.
     if (!empty($variables['options']['language']) && !isset($variables['options']['attributes']['hreflang'])) {
         $variables['options']['attributes']['hreflang'] = $variables['options']['language']->getId();
     }
     // Set the "active" class if the 'set_active_class' option is not empty.
     if (!empty($variables['options']['set_active_class']) && !$url->isExternal()) {
         // Add a "data-drupal-link-query" attribute to let the
         // drupal.active-link library know the query in a standardized manner.
         if (!empty($variables['options']['query'])) {
             $query = $variables['options']['query'];
             ksort($query);
             $variables['options']['attributes']['data-drupal-link-query'] = Json::encode($query);
         }
         // Add a "data-drupal-link-system-path" attribute to let the
         // drupal.active-link library know the path in a standardized manner.
         if ($url->isRouted() && !isset($variables['options']['attributes']['data-drupal-link-system-path'])) {
             // @todo System path is deprecated - use the route name and parameters.
             $system_path = $url->getInternalPath();
             // Special case for the front page.
             $variables['options']['attributes']['data-drupal-link-system-path'] = $system_path == '' ? '<front>' : $system_path;
         }
     }
     // Remove all HTML and PHP tags from a tooltip, calling expensive strip_tags()
     // only when a quick strpos() gives suspicion tags are present.
     if (isset($variables['options']['attributes']['title']) && strpos($variables['options']['attributes']['title'], '<') !== FALSE) {
         $variables['options']['attributes']['title'] = strip_tags($variables['options']['attributes']['title']);
     }
     // Allow other modules to modify the structure of the link.
     $this->moduleHandler->alter('link', $variables);
     // Move attributes out of options since generateFromRoute() doesn't need
     // them. Include a placeholder for the href.
     $attributes = array('href' => '') + $variables['options']['attributes'];
     unset($variables['options']['attributes']);
     $url->setOptions($variables['options']);
     if (!$collect_cacheability_metadata) {
         $url_string = $url->toString($collect_cacheability_metadata);
     } else {
         $generated_url = $url->toString($collect_cacheability_metadata);
         $url_string = $generated_url->getGeneratedUrl();
         $generated_link = GeneratedLink::createFromObject($generated_url);
     }
     // The result of the URL generator is a plain-text URL to use as the href
     // attribute, and it is escaped by \Drupal\Core\Template\Attribute.
     $attributes['href'] = $url_string;
     $result = SafeMarkup::format('<a@attributes>@text</a>', array('@attributes' => new Attribute($attributes), '@text' => $variables['text']));
     return $collect_cacheability_metadata ? $generated_link->setGeneratedLink($result) : $result;
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  *
  * For anonymous users, the "active" class will be calculated on the server,
  * because most sites serve each anonymous user the same cached page anyway.
  * For authenticated users, the "active" class will be calculated on the
  * client (through JavaScript), only data- attributes are added to links to
  * prevent breaking the render cache. The JavaScript is added in
  * system_page_attachments().
  *
  * @see system_page_attachments()
  */
 public function generate($text, Url $url)
 {
     // Performance: avoid Url::toString() needing to retrieve the URL generator
     // service from the container.
     $url->setUrlGenerator($this->urlGenerator);
     // Start building a structured representation of our link to be altered later.
     $variables = array('text' => is_array($text) ? drupal_render($text) : $text, 'url' => $url, 'options' => $url->getOptions());
     // Merge in default options.
     $variables['options'] += array('attributes' => array(), 'query' => array(), 'language' => NULL, 'set_active_class' => FALSE, 'absolute' => FALSE);
     // Add a hreflang attribute if we know the language of this link's url and
     // hreflang has not already been set.
     if (!empty($variables['options']['language']) && !isset($variables['options']['attributes']['hreflang'])) {
         $variables['options']['attributes']['hreflang'] = $variables['options']['language']->getId();
     }
     // Set the "active" class if the 'set_active_class' option is not empty.
     if (!empty($variables['options']['set_active_class']) && !$url->isExternal()) {
         // Add a "data-drupal-link-query" attribute to let the
         // drupal.active-link library know the query in a standardized manner.
         if (!empty($variables['options']['query'])) {
             $query = $variables['options']['query'];
             ksort($query);
             $variables['options']['attributes']['data-drupal-link-query'] = Json::encode($query);
         }
         // Add a "data-drupal-link-system-path" attribute to let the
         // drupal.active-link library know the path in a standardized manner.
         if ($url->isRouted() && !isset($variables['options']['attributes']['data-drupal-link-system-path'])) {
             // @todo System path is deprecated - use the route name and parameters.
             $system_path = $url->getInternalPath();
             // Special case for the front page.
             $variables['options']['attributes']['data-drupal-link-system-path'] = $system_path == '' ? '<front>' : $system_path;
         }
     }
     // Remove all HTML and PHP tags from a tooltip, calling expensive strip_tags()
     // only when a quick strpos() gives suspicion tags are present.
     if (isset($variables['options']['attributes']['title']) && strpos($variables['options']['attributes']['title'], '<') !== FALSE) {
         $variables['options']['attributes']['title'] = strip_tags($variables['options']['attributes']['title']);
     }
     // Allow other modules to modify the structure of the link.
     $this->moduleHandler->alter('link', $variables);
     // Move attributes out of options. generateFromRoute(() doesn't need them.
     $attributes = new Attribute($variables['options']['attributes']);
     unset($variables['options']['attributes']);
     $url->setOptions($variables['options']);
     // The result of the url generator is a plain-text URL. Because we are using
     // it here in an HTML argument context, we need to encode it properly.
     $url = String::checkPlain($url->toString());
     // Make sure the link text is sanitized.
     $safe_text = SafeMarkup::escape($variables['text']);
     return SafeMarkup::set('<a href="' . $url . '"' . $attributes . '>' . $safe_text . '</a>');
 }
Exemplo n.º 3
0
 /**
  * Checks whether a give path matches the ng-lightbox path rules.
  * This function checks both internal paths and aliased paths.
  *
  * @param \Drupal\Core\Url $url
  *   The Url object.
  *
  * @return bool
  *   TRUE if it matches the given rules.
  */
 public function isNgLightboxEnabledPath(Url $url)
 {
     // No lightbox on external Urls.
     if ($url->isExternal()) {
         return FALSE;
     }
     // If we don't want to enable the Lightbox on admin pages.
     if ($this->config->get('skip_admin_paths') && $this->adminContext->isAdminRoute()) {
         return FALSE;
     }
     // @TODO, decide whether we want to try and support paths or to adopt routes
     // like core is trying to force us into.
     $path = strtolower($url->toString());
     // We filter out empty paths because some modules (such as Media) use
     // theme_link() to generate links with empty paths.
     if (empty($path)) {
         return FALSE;
     }
     // Remove the base path.
     if ($base_path = \Drupal::request()->getBasePath()) {
         $path = substr($path, strlen($base_path));
     }
     // Check the cache, see if we've handled this before.
     if (isset($this->matches[$path])) {
         return $this->matches[$path];
     }
     // Normalise the patterns as well so they match the normalised paths.
     $patterns = strtolower($this->config->get('patterns'));
     // Check for internal paths first which is much quicker than the alias lookup.
     if ($this->pathMatcher->matchPath($path, $patterns)) {
         $this->matches[$path] = TRUE;
     } else {
         // Now check for aliases paths.
         $aliased_path = strtolower($this->aliasManager->getAliasByPath($path));
         if ($path != $aliased_path && $this->pathMatcher->matchPath($aliased_path, $patterns)) {
             $this->matches[$path] = TRUE;
         } else {
             // No match.
             $this->matches[$path] = FALSE;
         }
     }
     return $this->matches[$path];
 }
Exemplo n.º 4
0
 /**
  * Tests the isExternal() method.
  *
  * @depends testCreateFromPath
  *
  * @covers ::isExternal()
  */
 public function testIsExternal(Url $url)
 {
     $this->assertTrue($url->isExternal());
 }
Exemplo n.º 5
0
 /**
  * {@inheritdoc}
  *
  * For anonymous users, the "active" class will be calculated on the server,
  * because most sites serve each anonymous user the same cached page anyway.
  * For authenticated users, the "active" class will be calculated on the
  * client (through JavaScript), only data- attributes are added to links to
  * prevent breaking the render cache. The JavaScript is added in
  * system_page_attachments().
  *
  * @see system_page_attachments()
  */
 public function generate($text, Url $url)
 {
     // Performance: avoid Url::toString() needing to retrieve the URL generator
     // service from the container.
     $url->setUrlGenerator($this->urlGenerator);
     if (is_array($text)) {
         $text = $this->renderer->render($text);
     }
     // Start building a structured representation of our link to be altered later.
     $variables = array('text' => $text, 'url' => $url, 'options' => $url->getOptions());
     // Merge in default options.
     $variables['options'] += array('attributes' => array(), 'query' => array(), 'language' => NULL, 'set_active_class' => FALSE, 'absolute' => FALSE);
     // Add a hreflang attribute if we know the language of this link's url and
     // hreflang has not already been set.
     if (!empty($variables['options']['language']) && !isset($variables['options']['attributes']['hreflang'])) {
         $variables['options']['attributes']['hreflang'] = $variables['options']['language']->getId();
     }
     // Ensure that query values are strings.
     array_walk($variables['options']['query'], function (&$value) {
         if ($value instanceof MarkupInterface) {
             $value = (string) $value;
         }
     });
     // Set the "active" class if the 'set_active_class' option is not empty.
     if (!empty($variables['options']['set_active_class']) && !$url->isExternal()) {
         // Add a "data-drupal-link-query" attribute to let the
         // drupal.active-link library know the query in a standardized manner.
         if (!empty($variables['options']['query'])) {
             $query = $variables['options']['query'];
             ksort($query);
             $variables['options']['attributes']['data-drupal-link-query'] = Json::encode($query);
         }
         // Add a "data-drupal-link-system-path" attribute to let the
         // drupal.active-link library know the path in a standardized manner.
         if ($url->isRouted() && !isset($variables['options']['attributes']['data-drupal-link-system-path'])) {
             // @todo System path is deprecated - use the route name and parameters.
             $system_path = $url->getInternalPath();
             // Special case for the front page.
             $variables['options']['attributes']['data-drupal-link-system-path'] = $system_path == '' ? '<front>' : $system_path;
         }
     }
     // Remove all HTML and PHP tags from a tooltip, calling expensive strip_tags()
     // only when a quick strpos() gives suspicion tags are present.
     if (isset($variables['options']['attributes']['title']) && strpos($variables['options']['attributes']['title'], '<') !== FALSE) {
         $variables['options']['attributes']['title'] = strip_tags($variables['options']['attributes']['title']);
     }
     // Allow other modules to modify the structure of the link.
     $this->moduleHandler->alter('link', $variables);
     $url = $variables['url'];
     // Move attributes out of options since generateFromRoute() doesn't need
     // them. Make sure the "href" comes first for testing purposes.
     $attributes = array('href' => '') + $variables['options']['attributes'];
     unset($variables['options']['attributes']);
     $url->setOptions($variables['options']);
     // External URLs can not have cacheable metadata.
     if ($url->isExternal()) {
         $generated_link = new GeneratedLink();
         $attributes['href'] = $url->toString(FALSE);
     } elseif ($url->isRouted() && $url->getRouteName() === '<nolink>') {
         $generated_link = new GeneratedNoLink();
         unset($attributes['href']);
     } else {
         $generated_url = $url->toString(TRUE);
         $generated_link = GeneratedLink::createFromObject($generated_url);
         // The result of the URL generator is a plain-text URL to use as the href
         // attribute, and it is escaped by \Drupal\Core\Template\Attribute.
         $attributes['href'] = $generated_url->getGeneratedUrl();
     }
     if (!$variables['text'] instanceof MarkupInterface) {
         $variables['text'] = Html::escape($variables['text']);
     }
     $attributes = new Attribute($attributes);
     // This is safe because Attribute does escaping and $variables['text'] is
     // either rendered or escaped.
     return $generated_link->setGeneratedLink('<' . $generated_link::TAG . $attributes . '>' . $variables['text'] . '</' . $generated_link::TAG . '>');
 }