TIPS
TIPS. Front判定
php
$is_front = \Drupal::service('path.matcher')->isFrontPage();
- \core\includes\theme.inc
TIPS. Path(Route)を取得する方法いろいろ。
アクセスURL例:http://mydrupal.com/node/6
\Drupal::routeMatch()->getRouteName();
結果:> 'entity.node.canonical'
$route = \Drupal::routeMatch()->getRouteObject();
$path = $route->getPath();
結果:> '/node/{node}'
\Drupal::service('path.current')->getPath();
結果:> '/node/6'
アクセスURL例:http://mydrupal.com/popular
\Drupal::routeMatch()->getRouteName();
結果:> 'view.content_popular.page_1'
$route = \Drupal::routeMatch()->getRouteObject();
$path = $route->getPath();
結果:> '/popular'
\Drupal::service('path.current')->getPath();
結果:> '/popular'
TIPS. Uriを生成する方法いろいろ。
例) フロントのURL。
$front_url = \Drupal\Core\Url::fromRoute('<front>');
例) URIから生成。
$url = \Drupal\Core\Url::fromUri('base:articles/author/'.$node->getOwnerId());
- Drupalの生成したページなら
\Drupal\Core\Url::fromRoute($route_name)
を使うべきらしい。 - \Drupal::Url()は9.0で削除されるらしいから使わないほうがよさそう
- 参照:\core\lib\Drupal\Core\Url.php
例) theme_preprocess_xxx()で用意してtwigでは出力するだけの場合
theme
$authors_article_url = \Drupal\Core\Url::fromRoute('view.author_contents_page.page_1',
array('author_id' => $node->getOwnerId()));
$variables['authors_article_link_item'] = array(
'text' => 'この記者の他の記事',
'url' => $authors_article_url
);
twig
<p><a href="{{ authors_article_link_item.url }}">{{ authors_article_link_item.text }}</a></p>
TIPS. パラメータを取得する
- URLを「/articles/author/%author_id」と定義したView
- 「/articles/author/5」 でアクセスしても定義したパラメータ名(author_id)で取得可能
$author_id = \Drupal::routeMatch()->getParameter('author_id');
TIPS. NodeからUser entityを取り出し、User entityに追加したカスタムフィールドを取り出す
theme
$author = $node->getOwner();
$variables['user_detail'] = array(
'custom_field' => $author->get('field_my_custom_field')->value
);
- \core\lib\Drupal\Core\Entity\ContentEntityBase.php
- \core\lib\Drupal\Core\Field\FieldItemBase.php
TIPS. Twigでリンク生成。path()とurl()
twig
<p><a href="{{ path('view.author_contents_page.page_1', {'author_id': node.ownerid}) }}">この記者の他の記事</a></p>
html
<p><a href="/articles/author/5">この記者の他の記事</a></p>
twig
<p><a href="{{ url('view.author_contents_page.page_1', {'author_id': node.ownerid}) }}">この記者の他の記事</a></p>
html
<p><a href="http://mydrupal.com/articles/author/5">この記者の他の記事</a></p>
- Creating Links Within Twig Templates Using path() and url()
- 参照:\core\lib\Drupal\Core\Template\TwigExtension.php
TIPS. ページのタイトルを取得する
theme
$request = \Drupal::request();
$route_match = \Drupal::routeMatch();
$title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
TIPS. ヘッダの情報を変更する
例:linkタグprevを追加する
theme
pager_find_page()は1ページ目が「0」始まりなので注意。
if (\Drupal::request()->query->has('page') && pager_find_page() > 0) {
global $base_url;
$path = \Drupal::service('path.current')->getPath() . '?page=' . (pager_find_page() - 1);
$attachments['#attached']['html_head'][] = [
[
'#tag' => 'link',
'#attributes' => [
'rel' => 'prev',
'href' => $base_url . $path,
],
],
'link_prev',
];
}
- 参照:- Trying to add an html head tag in Drupal 8, beta 2
- \core\lib\Drupal\Core\Render\theme.api.php
- \core\lib\Drupal\Core\Render\HtmlResponseAttachmentsProcessor.php
TIPS. 各種Entityの読み出し
theme
// ユーザー
$uid = \Drupal::routeMatch()->getParameter('author_id');
$author = \Drupal\user\Entity\User::load($uid);
// 各フィールドの参照は以下のどちらでも可能
$author_public_name = $author->toArray()['field_public_name'][0]['value'];
$author_public_name = $author->get('field_public_name')->value;
theme
// タクソノミー
$taxonomy_term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($tid);
$name = $taxonomy_term->toArray()['name'][0]['value'];
参考
- Theming in Drupal 8 with Twig (Part 2)
- Twig best practices - preprocess functions and templates
- Functions - In Twig Templates
-
Drupalのテーマ作成などで、よく使うAPI(関数)まとめ
- Drupal7までの物が多い。。。
その他未整理メモ
- variablesの「#」付きのキーは自分のプロパティ、「#」無しのキーは子要素(?)
パンくずは「system」モジュールであり、「menu」モジュールではないため、管理画面からカスタマイズすることはできない?