drupa8でhook_init()する方法

カテゴリ PHP コアバージョン 8.3x 関連モジュール PHP

 

コメント

ユーザー actbrain の写真

カスタムモジュールで対応

module_name
 +- src
 |   +- EventSubscriber
 |       +- Module_nameSubscriber.php
 +- module_name.info.yml
 +- module_name.services.yml

module_name.info.yml

name: Module name
type: module
description: 〜
core: 8.x

module_name.services.yml

services:
  module_name_event_subscriber:
    class: Drupal\module_name\EventSubscriber\Module_nameSubscriber
    tags:
      - {name: event_subscriber}

src/EventSubscriber/Module_nameSubscriber.php

<?php

namespace Drupal\min_init\EventSubscriber;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class Min_initSubscriber implements EventSubscriberInterface {

  public function checkForRedirection(GetResponseEvent $event) {
    $request_uri = $_SERVER['REQUEST_URI'];
    \Drupal::logger('min_init')->notice($request_uri);
    if (preg_match('!^(/node/[0-9]+/)(edit|clone/prepopulate).*?destination=node/[0-9]+$!', $request_uri, $m)) {
      $path = \Drupal\Core\Url::fromUserInput($m[1] . $m[2]);
      $response = new RedirectResponse($path->toString());
      $response->send();
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('checkForRedirection');
    return $events;
  }

}

ページ

OTHER FAQ

Drupal開発・運用の疑問/質問の答えはここに

無料ユーザー登録すると質問できます。

カテゴリ Core Ver.降順で並び替える 関連モジュール タイトル
コンテンツの作成 7.38 CKEditor 突然GoogleMap内の経路がずれてしまった
Google 7.38 robots.txt Google Search Console Teamから「CSS および JS ファイルに Googlebot がアクセスできません」
フォーム 7.38 Webform Webformでタクソノミーを参照したい
コンテンツの管理 7.38 Block Views node ノードページの編集タブをコンテキストメニューにしたい
フォーム 7.38 Views Views一覧の絞り込み検索フォームで複数フィールドを対象にしたい
フォーム 7.38 Webform Webformモジュールをアップデートしたら「このフィールドを入力してください」
コンテンツの作成 7.38 Webform Webformでテーブル形式にしたい
サイトの構築 7.41 Rules Paygent モジュールについて
フォーム 7.43 Webform フォーム関数が実行されるタイミングについて
ユーザ 7.44 Quickbar 会員の種別(役割)別にメニューを設けたい
ユーザ 7.44 Password Policy 無効なユーザーを整理したい
サイトの構築 7.50 Module Missing Message Fixer モジュールをアンインストールしたらエラーがでるようになった
メール 7.50 Smtp smtpでGmailアカウントを使いたい
権限 7.50 Cron Basic認証時のCron(wget)実行方法
Apache2.4 7.50 Apache2.4のアクセス制限
コンテンツの作成 7.50 Code per Node ページ毎にCSSやJSを設定したい
ユーザの管理 7.50 Paypal continued billing drupal7のPaypal継続課金モジュールについて
開発 mail 7.50 Mail Safety 開発/テスト用サイトのメール
CentOS 7.50 消せないファイルを消すには
コンテンツの作成 7.50 Inline Entity Form Drupal6のフィールドグループのようなモジュールはありませんか?

ページ