php Drupal 8链接,或者我的l()函数在哪里

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Drupal 8链接,或者我的l()函数在哪里相关的知识,希望对你有一定的参考价值。

<?php

// Autoload Url and Link classes.
use Drupal\Core\Url;
use Drupal\Core\Link;

/**
 * External link
 */
$url = Url::fromUri('https://colorfield.be');
$link = Link::fromTextAndUrl(t('Colorfield'), $url);
$link = $link->toRenderable();
$link['#attributes'] = array('class' => array('external'));
$output = render($link); 

/**
 * Internal route
 */
$url = Url::fromRoute('contact.site_page'); // a route provided in .routing.yml
$link = Link::fromTextAndUrl(t("Contact"), $url);
$link = $link->toRenderable();
$link['#attributes'] = array('class' => array('internal'));
$output = render($link);

/**
 * Entities, e.g. node
 * @see http://stackoverflow.com/questions/35397009/creating-a-link-from-node-id-in-drupal-8
 */
$options = ['absolute' => TRUE];
$url = Url::fromRoute('entity.node.canonical', ['node' => 1], $options);
//$url = $url->toString();
$link = Link::fromTextAndUrl("Node title", $url);
$link = $link->toRenderable();
$link['#attributes'] = array('class' => array('internal'));
$output = render($link);

/**
 * Internal path
 */
$path = '/my/path'; // prefixed with / 
$url = Url::fromUri('internal:'.$path);
$link = Link::fromTextAndUrl($label, $url);
$link = $link->toRenderable();
$link['#attributes'] = array('class' => array('internal'));
$output = render($link); 

/**
 * Anchor (current page)
 */
$url = Url::fromRoute('<current>', array(), array('fragment' => $name));
$link = Link::fromTextAndUrl($label, $url);
$link =  $link->toRenderable(); 
$output = render($link); 

以上是关于php Drupal 8链接,或者我的l()函数在哪里的主要内容,如果未能解决你的问题,请参考以下文章

php Drupal 8生成链接内部

图像/链接Drupal

如何从外部PHP文件运行Drupal 8函数

php Drupal 8中构造函数的参数错误太少

php 在drupal 8中找到名字: - Drupal 8

当我将Drupal 7更新为Drupal 8时,我的模块会发生什么?