HTTP処理に使用する同等の機能は何ですか?


17

Drupal 7のHTTP処理ページにリストされている機能を見ると、次の機能はDrupal 8にはもう存在しないことに気付きました(リンクはDrupal 7のドキュメントページへのリンクです。機能がありません。)

代わりにDrupal 8で使用する関数/メソッドは何ですか?


1
これは、Drupalの7とのDrupal 8の違いについての質問のセリエAの一部である問題である
kiamlaluno

回答:


16

これらは、Drupal 8.6.xコードで使用する必要がある関数/メソッド/クラスです。

  • drupal_access_denied()AccessDeniedHttpExceptionクラスから置き換えられました。アクセス拒否エラーを返す必要があるページコールバックは、次のようなコードを使用する必要があります。

    // system_batch_page()
    public function batchPage(Request $request) {
      require_once $this->root . '/core/includes/batch.inc';
      $output = _batch_page($request);
      if ($output === FALSE) {
        throw new AccessDeniedHttpException();
      }
      elseif ($output instanceof Response) {
        return $output;
      }
      elseif (isset($output)) {
        $title = isset($output['#title']) ? $output['#title'] : NULL;
        $page = [
          '#type' => 'page',
          '#title' => $title,
          '#show_messages' => FALSE,
          'content' => $output,
        ];
    
        // Also inject title as a page header (if available).
        if ($title) {
          $page['header'] = [
            '#type' => 'page_title',
            '#title' => $title,
          ];
        }
        return $page;
      }
    }
  • 代わりにdrupal_get_query_array()存在するparse_query()(関数GuzzleHttp\Psr7がつがつ食うの一部であり、名前空間)が、。

  • drupal_goto()RedirectResponseクラスから置き換えられました。ユーザーをリダイレクトする必要があるページコールバックは、次のようなコードを使用する必要があります。(フォーム送信ハンドラはこのクラスを使用しないでください。)

    // AddSectionController::build()
    public function build(SectionStorageInterface $section_storage, $delta, $plugin_id) {
      $section_storage
        ->insertSection($delta, new Section($plugin_id));
      $this->layoutTempstoreRepository
        ->set($section_storage);
      if ($this->isAjax()) {
        return $this->rebuildAndClose($section_storage);
      }
      else {
        $url = $section_storage->getLayoutBuilderUrl();
        return new RedirectResponse($url->setAbsolute()->toString());
      }
    }
  • drupal_http_request()ClientInterfaceインターフェースを実装するDrupal 8サービスから置き換えられました。Drupal 8のコードは次のようになります。

    // system_retrieve_file()
    try {
      $data = (string) \Drupal::httpClient()->get($url)->getBody();
      $local = $managed ? file_save_data($data, $path, $replace) : file_unmanaged_save_data($data, $path, $replace);
    } catch (RequestException $exception) {
      \Drupal::messenger()->addError(t('Failed to fetch file due to error "%error"', ['%error' => $exception->getMessage()]));
      return FALSE;
    }
  • drupal_not_found()NotFoundHttpExceptionクラスから置き換えられました。ページコールバックでは、次のようなコードを使用する必要があります。

    // BookController::bookExport()
    public function bookExport($type, NodeInterface $node) {
      $method = 'bookExport' . Container::camelize($type);
    
      // @todo Convert the custom export functionality to serializer.
      if (!method_exists($this->bookExport, $method)) {
        $this->messenger()->addStatus(t('Unknown export format.'));
        throw new NotFoundHttpException();
      }
      $exported_book = $this->bookExport->{$method}($node);
      return new Response($this->renderer->renderRoot($exported_book));
    }
  • drupal_site_offline() 次のようなイベントサブスクライバーに置き換える必要があります。

    public static function getSubscribedEvents() {
      $events[KernelEvents::REQUEST][] = ['onKernelRequestMaintenance', 30];
      $events[KernelEvents::EXCEPTION][] = ['onKernelRequestMaintenance'];
      return $events;
    }
    
    public function onKernelRequestMaintenance(GetResponseEvent $event) {
      $request = $event->getRequest();
      $route_match = RouteMatch::createFromRequest($request);
      if ($this->maintenanceMode->applies($route_match)) {
        // Don't cache maintenance mode pages.
        \Drupal::service('page_cache_kill_switch')->trigger();
        if (!$this->maintenanceMode->exempt($this->account)) {
          // Deliver the 503 page if the site is in maintenance mode and the
          // logged in user is not allowed to bypass it.
          // If the request format is not 'html' then show default maintenance
          // mode page else show a text/plain page with maintenance message.
          if ($request->getRequestFormat() !== 'html') {
            $response = new Response($this->getSiteMaintenanceMessage(), %03, ['Content-Type' => 'text/plain']);
            $event->setResponse($response);
            return;
          }
          drupal_maintenance_theme();
          $response = $this->bareHtmlPageRenderer->renderBarePage([          '#markup' => $this->getSiteMaintenanceMessage()], $this->t('Site under maintenance'), 'maintenance_page');
          $response->setStatusCode(503);
          $event->setResponse($response);
        }
        else {
          // Display a message if the logged in user has access to the site in
          // maintenance mode. However, suppress it on the maintenance mode
          // settings page.
          if ($route_match->getRouteName() != 'system.site_maintenance_mode') {
            if ($this->account->hasPermission('administer site configuration')) {
              $this->messenger->addMessage($this
          ->t('Operating in maintenance mode. <a href=":url">Go online.</a>', [':url' => $this->urlGenerator->generate('system.site_maintenance_mode')]), 'status', FALSE);
            }
            else {
              $this->messenger->addMessage($this->t('Operating in maintenance mode.'), 'status', FALSE);
            }
          }
        }
      }
    }
    • drupal_encode_path() に置き換えられました UrlHelper::encodePath()
    • drupal_get_query_parameters() に置き換えられました UrlHelper::filterQueryParameters()
    • drupal_http_build_query()UrlHelper::buildQuery()、Drupalコアが少なくともPHP 5.4を必要とするときに削除されます(その時点で、を直接使用することが可能になり http_build_query()ます)。
    • drupal_parse_url() に置き換えられました UrlHelper::parse()

以前のDrupalバージョンと比較して、いくつかの重要な変更があることに注意してください。たとえば、Urlクラス内にあった一部のメソッドはクラス内で移動されましたUrlHelper。一部のGuzzleクラスは使用されなくなりました。


一部のAPIリンクは無効です。
-rudolfbyker

これらの機能がDrupalコアから削除された可能性があります。どのリンクが死んでいるかを確認し、それらを削除します。
キアムルノ

また、一部のリンクはもう有効ではないようですが、クラス/関数/メソッドはまだ存在しています。リンク形式を変更するか、クラス/関数/メソッドが別のファイルに移動されました。
キアムルノ
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.