2016-04-14 43 views
2

如何从我的.theme文件更改此标记?Drupal 8 - 核心搜索模块,更改标记

线119 \核心\模块\搜索的\ src \控制器\ SearchController.php

if (count($results)) { 
    $build['search_results_title'] = array(
     '#markup' => '<h2>' . $this->t('Search results') . '</h2>', 
    ); 
} 

我想从我的搜索页面中删除 “搜索结果” H2。

林能够改变上面的搜索形式和使用功能_preprocess_form在搜索resluts搜索表单和preprocess_search_result在H2下面的结果列表。

是否有预处理功能的IM丢失或我可以使用自定义模板树枝?

回答

-1

您可以覆盖项目列表 - 搜索results.html.twig,并更换标题,在这里:

{%- if title is not empty -%} 
    <h3>{{ title }}</h3> 
    {%- endif -%} 

只是删除H3。

+0

你正在谈论另一个模板。线程启动器实际上想知道如何操作搜索页面控制器的输出。因为带有'search_results_title'键的渲染数组是简单的标记并且没有主题或类型键,所以需要直接操作控制器输出。 –

0

您必须更改搜索模块定义的路由。 为了做到这一点:

  1. 定义在mymodule.services.yml文件下列:
 

    services: 
     mymodule.route_subscriber: 
     class: Drupal\mymodule\Routing\RouteSubscriber 
     tags: 
     - { name: event_subscriber } 

  • 创建扩展开/ MyModule的所述RouteSubscriberBase类的类/ SRC /路由/ RouteSubscriber.php如下:
  •  
    
        /** 
        * @file 
        * Contains \Drupal\mymodule\Routing\RouteSubscriber. 
        */ 
    
        namespace Drupal\mymodule\Routing; 
    
        use Drupal\Core\Routing\RouteSubscriberBase; 
        use Symfony\Component\Routing\RouteCollection; 
    
        /** 
        * Listens to the dynamic route events. 
        */ 
        class RouteSubscriber extends RouteSubscriberBase { 
    
         /** 
         * {@inheritdoc} 
         */ 
         public function alterRoutes(RouteCollection $collection) { 
         // Replace dynamically created "search.view_node_search" route's Controller 
         // with our own. 
         if ($route = $collection->get('search.view_node_search')) { 
          $route->setDefault('_controller', '\Drupal\mymodule\Controller\MyModuleSearchController::view'); 
         } 
         } 
        } 
    
    
  • 最后,控制器本身位于/mymodule/src/Controller/MyModuleSearchController.php
  •  
    
        namespace Drupal\mymodule\Controller; 
    
        use Drupal\search\SearchPageInterface; 
        use Symfony\Component\HttpFoundation\Request; 
        use Drupal\search\Controller\SearchController; 
    
        /** 
        * Override the Route controller for search. 
        */ 
        class MyModuleSearchController extends SearchController { 
    
         /** 
         * {@inheritdoc} 
         */ 
         public function view(Request $request, SearchPageInterface $entity) { 
         $build = parent::view($request, $entity); 
         // Unset the Result title. 
         if (isset($build['search_results_title'])) { 
          unset($build['search_results_title']); 
         } 
    
         return $build; 
         } 
    
        }