Issue #2343669 by mpdonadio: Remove _l() and _url()
parent
326db64678
commit
a7e819e13d
|
@ -487,33 +487,6 @@ function _format_date_callback(array $matches = NULL, $new_langcode = NULL) {
|
||||||
* @} End of "defgroup format".
|
* @} End of "defgroup format".
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
|
||||||
* Generates an internal or external URL.
|
|
||||||
*
|
|
||||||
* When creating links in modules, consider whether l() could be a better
|
|
||||||
* alternative than url().
|
|
||||||
*
|
|
||||||
* @see \Drupal\Core\Url::fromUri()
|
|
||||||
* @see \Drupal\Core\Url::fromRoute()
|
|
||||||
* @see \Drupal\Core\Url::toString()
|
|
||||||
*
|
|
||||||
* @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.0.
|
|
||||||
* Use \Drupal\Core\Url::fromRoute() for internal paths served by Drupal
|
|
||||||
* controllers or \Drupal\Core\Url::fromUri() for external paths or
|
|
||||||
* non-controller or sub-domain URIs such as core/install.php. Note that
|
|
||||||
* \Drupal\Core\Url::fromUri() expects a valid URI including the scheme. URIs
|
|
||||||
* from the same sub-domain that are not handled by Drupal controllers should
|
|
||||||
* use the base: scheme. For example:
|
|
||||||
* @code
|
|
||||||
* $installer_url = \Drupal\Core\Url::fromUri('base:core/install.php')->toString();
|
|
||||||
* $external_url = \Drupal\Core\Url::fromUri('http://example.com', ['query' => ['foo' => 'bar']])->toString();
|
|
||||||
* $internal_url = \Drupal\Core\Url::fromRoute('system.admin')->toString();
|
|
||||||
* @endcode
|
|
||||||
*/
|
|
||||||
function _url($path = NULL, array $options = array()) {
|
|
||||||
return \Drupal::urlGenerator()->generateFromPath($path, $options);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Formats an attribute string for an HTTP header.
|
* Formats an attribute string for an HTTP header.
|
||||||
*
|
*
|
||||||
|
@ -534,152 +507,6 @@ function drupal_http_header_attributes(array $attributes = array()) {
|
||||||
return $attributes ? ' ' . implode('; ', $attributes) : '';
|
return $attributes ? ' ' . implode('; ', $attributes) : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Formats an internal or external URL link as an HTML anchor tag.
|
|
||||||
*
|
|
||||||
* This function correctly handles aliased paths and adds an 'active' class
|
|
||||||
* attribute to links that point to the current page (for theming), so all
|
|
||||||
* internal links output by modules should be generated by this function if
|
|
||||||
* possible.
|
|
||||||
*
|
|
||||||
* However, for links enclosed in translatable text you should use t() and
|
|
||||||
* embed the HTML anchor tag directly in the translated string. For example:
|
|
||||||
* @code
|
|
||||||
* t('Visit the <a href="@url">settings</a> page', array('@url' => \Drupal::url('system.admin')));
|
|
||||||
* @endcode
|
|
||||||
* This keeps the context of the link title ('settings' in the example) for
|
|
||||||
* translators.
|
|
||||||
*
|
|
||||||
* This function does not support generating links from internal routes. For
|
|
||||||
* that use \Drupal\Core\Utility\LinkGenerator::generate(), which is exposed via
|
|
||||||
* the 'link_generator' service. It requires an internal route name and does not
|
|
||||||
* support external URLs. Using Drupal 7 style system paths should be avoided if
|
|
||||||
* possible but l() should still be used when rendering links to external URLs.
|
|
||||||
*
|
|
||||||
* @param string|array $text
|
|
||||||
* The link text for the anchor tag as a translated string or render array.
|
|
||||||
* @param string $path
|
|
||||||
* The internal path or external URL being linked to, such as "node/34" or
|
|
||||||
* "http://example.com/foo". After the url() function is called to construct
|
|
||||||
* the URL from $path and $options, the resulting URL is passed through
|
|
||||||
* \Drupal\Component\Utility\String::checkPlain() before it is inserted into
|
|
||||||
* the HTML anchor tag, to ensure well-formed HTML. See url() for more
|
|
||||||
* information and notes.
|
|
||||||
* @param array $options
|
|
||||||
* An associative array of additional options. Defaults to an empty array. It
|
|
||||||
* may contain the following elements.
|
|
||||||
* - 'attributes': An associative array of HTML attributes to apply to the
|
|
||||||
* anchor tag. If element 'class' is included, it must be an array; 'title'
|
|
||||||
* must be a string; other elements are more flexible, as they just need
|
|
||||||
* to work as an argument for the constructor of the class
|
|
||||||
* Drupal\Core\Template\Attribute($options['attributes']).
|
|
||||||
* - 'html' (default FALSE): Whether $text is HTML or just plain-text. For
|
|
||||||
* example, to make an image tag into a link, this must be set to TRUE, or
|
|
||||||
* you will see the escaped HTML image tag. $text is not sanitized if
|
|
||||||
* 'html' is TRUE. The calling function must ensure that $text is already
|
|
||||||
* safe.
|
|
||||||
* - 'language': An optional language object. If the path being linked to is
|
|
||||||
* internal to the site, $options['language'] is used to determine whether
|
|
||||||
* the link is "active", or pointing to the current page (the language as
|
|
||||||
* well as the path must match). This element is also used by url().
|
|
||||||
* - 'set_active_class': Whether l() should compare the $path, language and
|
|
||||||
* query options to the current URL to determine whether the link is
|
|
||||||
* "active". Defaults to FALSE. If TRUE, an "active" class will be applied
|
|
||||||
* to the link. It is important to use this sparingly since it is usually
|
|
||||||
* unnecessary and requires extra processing.
|
|
||||||
* 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().
|
|
||||||
* - Additional $options elements used by the url() function.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
* An HTML string containing a link to the given path.
|
|
||||||
*
|
|
||||||
* @see _url()
|
|
||||||
* @see system_page_attachments()
|
|
||||||
* @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.0.
|
|
||||||
* Use \Drupal::l($text, $url) where $url is an instance of
|
|
||||||
* \Drupal\Core\Url. To build a \Drupal\Core\Url object for internal paths
|
|
||||||
* served by Drupal controllers use \Drupal\Core\Url::fromRoute(). For
|
|
||||||
* external paths or non-controller or sub-domain URIs such as
|
|
||||||
* core/install.php use \Drupal\Core\Url::fromUri(). Note that
|
|
||||||
* \Drupal\Core\Url::fromUri() expects a valid URI including the scheme. URIs
|
|
||||||
* from the same sub-domain that are not handled by Drupal controllers should
|
|
||||||
* be prepended with base:. For example:
|
|
||||||
* @code
|
|
||||||
* $installer_url = \Drupal\Core\Url::fromUri('base:core/install.php')->toString();
|
|
||||||
* $installer_link = \Drupal::l($text, $installer_url);
|
|
||||||
* $external_url = \Drupal\Core\Url::fromUri('http://example.com', ['query' => ['foo' => 'bar']])->toString();
|
|
||||||
* $external_link = \Drupal::l($text, $external_url);
|
|
||||||
* $internal_url = \Drupal\Core\Url::fromRoute('system.admin')->toString();
|
|
||||||
* $internal_link = \Drupal::l($text, $internal_url);
|
|
||||||
* @endcode
|
|
||||||
*/
|
|
||||||
function _l($text, $path, array $options = array()) {
|
|
||||||
// Start building a structured representation of our link to be altered later.
|
|
||||||
$variables = array(
|
|
||||||
'text' => is_array($text) ? drupal_render($text) : $text,
|
|
||||||
'path' => $path,
|
|
||||||
'options' => $options,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Merge in default options.
|
|
||||||
$variables['options'] += array(
|
|
||||||
'attributes' => array(),
|
|
||||||
'query' => array(),
|
|
||||||
'html' => FALSE,
|
|
||||||
'language' => NULL,
|
|
||||||
'set_active_class' => 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'])) {
|
|
||||||
// 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 (!isset($variables['options']['attributes']['data-drupal-link-system-path'])) {
|
|
||||||
$variables['options']['attributes']['data-drupal-link-system-path'] = \Drupal::service('path.alias_manager')->getPathByAlias($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.
|
|
||||||
\Drupal::moduleHandler()->alter('link', $variables);
|
|
||||||
|
|
||||||
// Move attributes out of options. url() doesn't need them.
|
|
||||||
$attributes = new Attribute($variables['options']['attributes']);
|
|
||||||
unset($variables['options']['attributes']);
|
|
||||||
|
|
||||||
// The result of url() 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($variables['path'], $variables['options']));
|
|
||||||
|
|
||||||
// Sanitize the link text if necessary.
|
|
||||||
$text = $variables['options']['html'] ? $variables['text'] : String::checkPlain($variables['text']);
|
|
||||||
return SafeMarkup::set('<a href="' . $url . '"' . $attributes . '>' . $text . '</a>');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts to set the PHP maximum execution time.
|
* Attempts to set the PHP maximum execution time.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue