public function page(Request $request, $search_api_page, $keyword = '')
 {
     $build = array();
     /** @var $searchApiPage SearchApiPageInterface */
     $searchApiPage = SearchApiPage::load($search_api_page);
     // Page title.
     $build['#title'] = $searchApiPage->label();
     /** @var $searchApiIndex IndexInterface */
     $searchApiIndex = Index::load($searchApiPage->getIndex());
     // Create the query.
     $query = $searchApiIndex->query(['parse_mode' => 'direct', 'limit' => $searchApiPage->getLimit(), 'offset' => !is_null($request->get('page')) ? $request->get('page') : 0, 'search id' => 'search_api_page:' . $searchApiPage->id()]);
     // Keywords.
     if (!empty($keyword)) {
         $query->keys($keyword);
     }
     // Index fields.
     $query->setFulltextFields(array('rendered_item'));
     $result = $query->execute();
     $items = $result->getResultItems();
     /** @var $item ItemInterface*/
     $results = array();
     foreach ($items as $item) {
         list(, $path, $langcode) = explode(':', $item->getId());
         list($entity_type, $id) = explode('/', $path);
         $entity = $this->entityManager()->getStorage($entity_type)->load($id);
         $results[] = $this->entityManager()->getViewBuilder($entity_type)->view($entity, 'teaser');
     }
     if (!empty($results)) {
         $build['results'] = $results;
         // Build pager.
         pager_default_initialize($result->getResultCount(), $searchApiPage->getLimit());
         $build['pager'] = array('#type' => 'pager');
     }
     return $build;
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function calculateDependencies()
 {
     /* @var $search_api_page \Drupal\search_api_page\SearchApiPageInterface */
     $search_api_page = SearchApiPage::load($this->configuration['search_api_page']);
     $config_name = $search_api_page->getConfigDependencyName();
     return ['config' => [$config_name]];
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, $args = array())
 {
     /* @var $search_api_page \Drupal\search_api_page\SearchApiPageInterface */
     $search_api_page = SearchApiPage::load($args['search_api_page']);
     $route = 'search_api_page.' . $this->languageManager->getCurrentLanguage()->getId() . '.' . $search_api_page->id();
     $form['#action'] = $this->getUrlGenerator()->generateFromRoute($route);
     $form['search_api_page'] = array('#type' => 'value', '#value' => $search_api_page->id());
     $form['keys'] = array('#type' => 'search', '#title' => $this->t('Search'), '#title_display' => 'invisible', '#size' => 15, '#default_value' => isset($args['keys']) ? $args['keys'] : '', '#attributes' => array('title' => $this->t('Enter the terms you wish to search for.')));
     $form['actions'] = array('#type' => 'actions');
     $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Search'));
     if (!$search_api_page->getCleanUrl()) {
         $form['#method'] = 'get';
         $form['actions']['submit']['#name'] = '';
     }
     // Dependency on search api config entity.
     $this->renderer->addCacheableDependency($form, $search_api_page->getConfigDependencyName());
     return $form;
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, $args = array())
 {
     // Set up the form to submit using GET to the correct search page.
     /*$entity_id = $this->searchPageRepository->getDefaultSearchPage();
       if (!$entity_id) {
         $form['message'] = array(
           '#markup' => $this->t('Search is currently disabled'),
         );
         return $form;
       }*/
     /** @var $searchApiPage SearchApiPageInterface */
     $searchApiPage = SearchApiPage::load($args['search_api_page']);
     $route = 'search_api_page.' . $searchApiPage->id();
     $form['#action'] = $this->url($route);
     $form['search_api_page'] = array('#type' => 'value', '#value' => $searchApiPage->id());
     $form['keys'] = array('#type' => 'search', '#title' => $this->t('Search'), '#title_display' => 'invisible', '#size' => 15, '#default_value' => '', '#attributes' => array('title' => $this->t('Enter the terms you wish to search for.')));
     $form['actions'] = array('#type' => 'actions');
     $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Search'));
     // SearchPageRepository::getDefaultSearchPage() depends on search.settings.
     //$this->renderer->addCacheableDependency($form, $this->configFactory->get('search.settings'));
     return $form;
 }
Exemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function getPath()
 {
     /* @var $search_api_page \Drupal\search_api_page\SearchApiPageInterface */
     list(, $search_api_page_name) = explode(':', $this->pluginId);
     $search_api_page = SearchApiPageEntity::load($search_api_page_name);
     if ($search_api_page->getCleanUrl()) {
         return $search_api_page->getPath() . '/' . \Drupal::request()->get('keys');
     } else {
         return $search_api_page->getPath();
     }
 }
Exemplo n.º 6
0
 /**
  * {@inheritdoc}
  */
 public function getPath()
 {
     /** @var $searchApiPage SearchApiPageInterface */
     list(, $search_api_page) = explode(':', $this->pluginId);
     $searchApiPage = \Drupal\search_api_page\Entity\SearchApiPage::load($search_api_page);
     return $searchApiPage->getPath();
 }
    /**
     * Router callback.
     *
     * @param Request $request
     *   The request.
     * @param string $search_api_page_name
     *   The search api page name.
     * @param string $keys
     *   The search word.
     *
     * @return array $build
     *   The page build.
     */
    public function page(Request $request, $search_api_page_name, $keys = '')
    {
        $build = array();
        /* @var $search_api_page \Drupal\search_api_page\SearchApiPageInterface */
        $search_api_page = SearchApiPage::load($search_api_page_name);
        // Keys can be in the query.
        if (!$search_api_page->getCleanUrl()) {
            $keys = $request->get('keys');
        }
        // Page title.
        $build['#title'] = $search_api_page->label();
        // Show the search form.
        if ($search_api_page->showSearchForm()) {
            $args = array('search_api_page' => $search_api_page->id(), 'keys' => $keys);
            $build['#form'] = $this->formBuilder()->getForm('Drupal\\search_api_page\\Form\\SearchApiPageBlockForm', $args);
        }
        $perform_search = TRUE;
        if (empty($keys) && !$search_api_page->showAllResultsWhenNoSearchIsPerformed()) {
            $perform_search = FALSE;
        }
        if ($perform_search) {
            /* @var $search_api_index \Drupal\search_api\IndexInterface */
            $search_api_index = Index::load($search_api_page->getIndex());
            // Create the query.
            $query = $search_api_index->query(['parse_mode' => 'direct', 'limit' => $search_api_page->getLimit(), 'offset' => !is_null($request->get('page')) ? $request->get('page') : 0, 'search id' => 'search_api_page:' . $search_api_page->id()]);
            // Search for keys.
            if (!empty($keys)) {
                $query->keys($keys);
            }
            // Index fields.
            $query->setFulltextFields($search_api_page->getSearchedFields());
            $result = $query->execute();
            $items = $result->getResultItems();
            /* @var $item \Drupal\search_api\Item\ItemInterface*/
            $results = array();
            foreach ($items as $item) {
                list(, $path, $langcode) = explode(':', $item->getId());
                list($entity_type, $id) = explode('/', $path);
                // Get the entity.
                $entity = $this->entityTypeManager()->getStorage($entity_type)->load($id);
                // Render as view modes.
                if ($search_api_page->renderAsViewModes()) {
                    $key = 'entity:' . $entity_type . '_' . $entity->bundle();
                    $view_mode_configuration = $search_api_page->getViewModeConfiguration();
                    $view_mode = isset($view_mode_configuration[$key]) ? $view_mode_configuration[$key] : 'default';
                    $results[] = $this->entityTypeManager()->getViewBuilder($entity_type)->view($entity, $view_mode);
                }
                // Render as snippets.
                if ($search_api_page->renderAsSnippets()) {
                    $results[] = array('#theme' => 'search_api_page_result', '#item' => $item, '#entity' => $entity);
                }
            }
            if (!empty($results)) {
                $build['#search_title'] = array('#markup' => $this->t('Search results'));
                $build['#no_of_results'] = array('#markup' => $this->formatPlural($result->getResultCount(), '1 result found', '@count results found'));
                $build['#results'] = $results;
                // Build pager.
                pager_default_initialize($result->getResultCount(), $search_api_page->getLimit());
                $build['#pager'] = array('#type' => 'pager');
            } elseif ($perform_search) {
                $build['#no_results_found'] = array('#markup' => $this->t('Your search yielded no results.'));
                $build['#search_help'] = array('#markup' => $this->t('<ul>
<li>Check if your spelling is correct.</li>
<li>Remove quotes around phrases to search for each word individually. <em>bike shed</em> will often show more results than <em>&quot;bike shed&quot;</em>.</li>
<li>Consider loosening your query with <em>OR</em>. <em>bike OR shed</em> will often show more results than <em>bike shed</em>.</li>
</ul>'));
            }
        }
        $build['#theme'] = 'search_api_page';
        // TODO caching dependencies.
        return $build;
    }