LoginSignup
1
2

More than 5 years have passed since last update.

Drupal8 入門の入門 (ページのヘッダ情報の追加・変更)

Last updated at Posted at 2016-05-25

ヘッダ情報をattachするタイミングがシステムやモジュールによって異なる。

ページのヘッダ情報を追加・更新する(共通)

mytheme.themeで、hook_page_attachments_alter()をオーバーライドして追加・変更する

例:

mytheme.theme.php
function mytheme_page_attachments_alter(&$attachments)
{
    // 例:Meta Descriptionを追加する
    if (なんらかの条件) {
        $attachments['#attached']['html_head'][] = [
            [
                '#tag' => 'meta',
                '#attributes' => [
                    'name' => 'description',
                    'content' => 'ページの概要です。',
                ],
            ],
            'description',
        ];
    }

    // 例:システムがデフォルトで設定する'Generator'を除去する
    $system_head_meta = [
        'system_meta_generator',
    ];
    $attachments['#attached']['html_head'] = array_filter(
        $attachments['#attached']['html_head'],
        function ($item) use ($system_head_meta)
        {
            // 判定処理(省略)
        }
    );

    /*
     * その他、ページによってlibraryを追加したりなど
     * $variables['#attached']['library'][] = 'mytheme/some_library';
     *
     * 尚、似たようなhookでも、hook_page_attachments(array &$attachments)はmodule内でhookできない?
     * Drupal/Core/Render/theme.api.php
     *
     * https://www.drupal.org/node/2274843
     * https://www.drupal.org/node/2216195
     * http://drupal.stackexchange.com/questions/147365/hook-page-attachments-not-called-from-mytheme-theme-file
     */
}

Nodeページのヘッダ情報を変更する

mymodule.moduleで、hook_entity_view_alter()をオーバーライドしてhookする理由は下記ソース内に記載。

このhookはmoduleでしかオーバーライドできないので注意!

例:

mymodule.module.php
function mymodule_entity_view_alter(array &$build, Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display)
{
    /*
     * https://www.drupal.org/node/2406533
     * taxonomy_termは、hook_page_attachments_alter()でヘッダ情報をセット
     * nodeは、hook_entity_view_alter()でヘッダ情報をセット
     *
     * core/modules/node/src/Controller/NodeViewController.php
     * Drupal/Core/Entity/entity.api.php
     *
     * core/modules/taxonomy/taxonomy.module
     *
     * entity ID : node, taxonomy_term, block_content, etc
     */
    if ($entity->getEntityType()->id() === 'node') {
        if (!isset($build['#attached']['html_head_link'])) {
            return;
        }

        // 下記の'rel'を除去する!
        $system_head_link = [
            'delete-form',
            'edit-form',
            'version-history',
            'shortlink',
        ];

        $build['#attached']['html_head_link'] = array_filter(
            $build['#attached']['html_head_link'],
            function ($item) use ($system_head_link) {
                // 判定処理(省略)
            }
        );
    }
}

Taxonomy_termページのヘッダ情報を変更する

mytheme.themeで、hook_page_attachments_alter()をオーバーライドしてhookする

例:

mytheme.theme.php
function mytheme_page_attachments_alter(&$attachments)
{
    if (\Drupal::routeMatch()->getRouteName() === 'entity.taxonomy_term.canonical') {
        // 下記の'rel'を除去する!
        $system_head_link = [
            'delete-form',
            'edit-form',
            'version-history',
            'shortlink',
        ];

        $attachments['#attached']['html_head_link'] = array_filter(
            $attachments['#attached']['html_head_link'],
            function ($item) use ($system_head_link) {
                // 判定処理(省略)
            }
        );
    }
}

Pagerを利用しているViewsページのヘッダにnext/prevリンクタグを挿入する

$pager->getCurrentPage()に注意。

参考

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2