我应该使用HTTP处理的等效功能是什么?


17

查看Drupal 7HTTP处理页面中列出的功能时,我注意到在Drupal 8中不再存在以下功能。功能缺失。)

我应该在Drupal 8中使用哪些功能/方法?


1
这是一个问题是关于Drupal的7和Drupal 8之间的差异问题,一个系列的一部分
kiamlaluno

Answers:


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版本相比,有一些重要的变化。例如,Url该类中的某些方法已在该类中移动UrlHelper;不再使用某些Guzzle类。


一些API链接已失效。
rudolfbyker '18 -10-1

这些功能很可能已从Drupal核心中删除。我将检查哪些链接已失效并删除它们。
kiamlaluno

似乎有些链接不再有效,但是类/函数/方法仍然存在。他们只是更改链接格式,或者类/函数/方法已移至其他文件。
kiamlaluno
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.