Commit 60901852 authored by xjm's avatar xjm

Issue #70722 by David Lesieur, bircher, rocket_nova, paranojik, Pasqualle,...

Issue #70722 by David Lesieur, bircher, rocket_nova, paranojik, Pasqualle, jhodgdon, robertDouglass, douggreen, petar.gnjidic, pfournier: Search results should respect the content type's "Display author and date information." option
parent a33414e5
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessibleInterface; use Drupal\Core\Access\AccessibleInterface;
use Drupal\Core\Database\Query\Condition; use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Render\RendererInterface;
use Drupal\node\NodeInterface; use Drupal\node\NodeInterface;
use Drupal\search\Plugin\ConfigurableSearchPluginBase; use Drupal\search\Plugin\ConfigurableSearchPluginBase;
use Drupal\search\Plugin\SearchIndexingInterface; use Drupal\search\Plugin\SearchIndexingInterface;
...@@ -79,6 +80,13 @@ class NodeSearch extends ConfigurableSearchPluginBase implements AccessibleInter ...@@ -79,6 +80,13 @@ class NodeSearch extends ConfigurableSearchPluginBase implements AccessibleInter
*/ */
protected $account; protected $account;
/**
* The Renderer service to format the username and node.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/** /**
* An array of additional rankings from hook_ranking(). * An array of additional rankings from hook_ranking().
* *
...@@ -119,6 +127,7 @@ static public function create(ContainerInterface $container, array $configuratio ...@@ -119,6 +127,7 @@ static public function create(ContainerInterface $container, array $configuratio
$container->get('module_handler'), $container->get('module_handler'),
$container->get('config.factory')->get('search.settings'), $container->get('config.factory')->get('search.settings'),
$container->get('language_manager'), $container->get('language_manager'),
$container->get('renderer'),
$container->get('current_user') $container->get('current_user')
); );
} }
...@@ -145,12 +154,13 @@ static public function create(ContainerInterface $container, array $configuratio ...@@ -145,12 +154,13 @@ static public function create(ContainerInterface $container, array $configuratio
* @param \Drupal\Core\Session\AccountInterface $account * @param \Drupal\Core\Session\AccountInterface $account
* The $account object to use for checking for access to advanced search. * The $account object to use for checking for access to advanced search.
*/ */
public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, Config $search_settings, LanguageManagerInterface $language_manager, AccountInterface $account = NULL) { public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, Config $search_settings, LanguageManagerInterface $language_manager, RendererInterface $renderer, AccountInterface $account = NULL) {
$this->database = $database; $this->database = $database;
$this->entityManager = $entity_manager; $this->entityManager = $entity_manager;
$this->moduleHandler = $module_handler; $this->moduleHandler = $module_handler;
$this->searchSettings = $search_settings; $this->searchSettings = $search_settings;
$this->languageManager = $language_manager; $this->languageManager = $language_manager;
$this->renderer = $renderer;
$this->account = $account; $this->account = $account;
parent::__construct($configuration, $plugin_id, $plugin_definition); parent::__construct($configuration, $plugin_id, $plugin_definition);
} }
...@@ -310,11 +320,16 @@ protected function prepareResults(StatementInterface $found) { ...@@ -310,11 +320,16 @@ protected function prepareResults(StatementInterface $found) {
/** @var \Drupal\node\NodeInterface $node */ /** @var \Drupal\node\NodeInterface $node */
$node = $node_storage->load($item->sid)->getTranslation($item->langcode); $node = $node_storage->load($item->sid)->getTranslation($item->langcode);
$build = $node_render->view($node, 'search_result', $item->langcode); $build = $node_render->view($node, 'search_result', $item->langcode);
/** @var \Drupal\node\NodeTypeInterface $type*/
$type = $this->entityManager->getStorage('node_type')->load($node->bundle());
unset($build['#theme']); unset($build['#theme']);
$build['#pre_render'][] = array($this, 'removeSubmittedInfo');
// Fetch comment count for snippet. // Fetch comment count for snippet.
$node->rendered = SafeMarkup::set( $rendered = SafeMarkup::set(
drupal_render($build) . ' ' . $this->renderer->render($build) . ' ' .
SafeMarkup::escape($this->moduleHandler->invoke('comment', 'node_update_index', array($node, $item->langcode))) SafeMarkup::escape($this->moduleHandler->invoke('comment', 'node_update_index', array($node, $item->langcode)))
); );
...@@ -325,22 +340,50 @@ protected function prepareResults(StatementInterface $found) { ...@@ -325,22 +340,50 @@ protected function prepareResults(StatementInterface $found) {
'#theme' => 'username', '#theme' => 'username',
'#account' => $node->getOwner(), '#account' => $node->getOwner(),
); );
$results[] = array(
$result = array(
'link' => $node->url('canonical', array('absolute' => TRUE, 'language' => $language)), 'link' => $node->url('canonical', array('absolute' => TRUE, 'language' => $language)),
'type' => SafeMarkup::checkPlain($this->entityManager->getStorage('node_type')->load($node->bundle())->label()), 'type' => SafeMarkup::checkPlain($type->label()),
'title' => $node->label(), 'title' => $node->label(),
'user' => drupal_render($username),
'date' => $node->getChangedTime(),
'node' => $node, 'node' => $node,
'extra' => $extra, 'extra' => $extra,
'score' => $item->calculated_score, 'score' => $item->calculated_score,
'snippet' => search_excerpt($keys, $node->rendered, $item->langcode), 'snippet' => search_excerpt($keys, $rendered, $item->langcode),
'langcode' => $node->language()->getId(), 'langcode' => $node->language()->getId(),
); );
if ($type->displaySubmitted()) {
$result += array(
'user' => $this->renderer->render($username),
'date' => $node->getChangedTime(),
);
}
$results[] = $result;
} }
return $results; return $results;
} }
/**
* Removes the submitted by information from the build array.
*
* This information is being removed from the rendered node that is used to
* build the search result snippet. It just doesn't make sense to have it
* displayed in the snippet.
*
* @param array $build
* The build array.
*
* @return array
* The modified build array.
*/
public function removeSubmittedInfo(array $build) {
unset($build['created']);
unset($build['uid']);
return $build;
}
/** /**
* Adds the configured rankings to the search query. * Adds the configured rankings to the search query.
* *
...@@ -400,9 +443,9 @@ protected function indexNode(NodeInterface $node) { ...@@ -400,9 +443,9 @@ protected function indexNode(NodeInterface $node) {
$build = $node_render->view($node, 'search_index', $language->getId()); $build = $node_render->view($node, 'search_index', $language->getId());
unset($build['#theme']); unset($build['#theme']);
$node->rendered = drupal_render($build); $rendered = $this->renderer->render($build);
$text = '<h1>' . SafeMarkup::checkPlain($node->label($language->getId())) . '</h1>' . $node->rendered; $text = '<h1>' . SafeMarkup::checkPlain($node->label($language->getId())) . '</h1>' . $rendered;
// Fetch extra data normally not visible. // Fetch extra data normally not visible.
$extra = $this->moduleHandler->invokeAll('node_update_index', array($node, $language->getId())); $extra = $this->moduleHandler->invokeAll('node_update_index', array($node, $language->getId()));
......
...@@ -18,7 +18,8 @@ class SearchExactTest extends SearchTestBase { ...@@ -18,7 +18,8 @@ class SearchExactTest extends SearchTestBase {
*/ */
function testExactQuery() { function testExactQuery() {
// Login with sufficient privileges. // Login with sufficient privileges.
$this->drupalLogin($this->drupalCreateUser(array('create page content', 'search content'))); $user = $this->drupalCreateUser(array('create page content', 'search content'));
$this->drupalLogin($user);
$settings = array( $settings = array(
'type' => 'page', 'type' => 'page',
...@@ -34,6 +35,9 @@ function testExactQuery() { ...@@ -34,6 +35,9 @@ function testExactQuery() {
$settings['body'] = array(array('value' => 'love cheesy pizza')); $settings['body'] = array(array('value' => 'love cheesy pizza'));
$this->drupalCreateNode($settings); $this->drupalCreateNode($settings);
} }
// Create another node and save it for later.
$settings['body'] = array(array('value' => 'Druplicon'));
$node = $this->drupalCreateNode($settings);
// Update the search index. // Update the search index.
$this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex(); $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
...@@ -55,5 +59,25 @@ function testExactQuery() { ...@@ -55,5 +59,25 @@ function testExactQuery() {
$this->drupalPostForm('search/node', $edit, t('Search')); $this->drupalPostForm('search/node', $edit, t('Search'));
$this->assertLinkByHref('page=1', 0, '2nd page link is found for exact phrase search.'); $this->assertLinkByHref('page=1', 0, '2nd page link is found for exact phrase search.');
$this->assertNoLinkByHref('page=2', '3rd page link is not found for exact phrase search.'); $this->assertNoLinkByHref('page=2', '3rd page link is not found for exact phrase search.');
// Check that with post settings turned on the post information is displayed.
$node_type_config = \Drupal::configFactory()->getEditable('node.type.page');
$node_type_config->set('display_submitted', TRUE);
$node_type_config->save();
$edit = array('keys' => 'Druplicon');
$this->drupalPostForm('search/node', $edit, t('Search'));
$this->assertText($user->getUsername(), 'Basic page node displays author name when post settings are on.');
$this->assertText(format_date($node->getChangedTime(), 'short'), 'Basic page node displays post date when post settings are on.');
// Check that with post settings turned off the user and changed date
// information is not displayed.
$node_type_config->set('display_submitted', FALSE);
$node_type_config->save();
$edit = array('keys' => 'Druplicon');
$this->drupalPostForm('search/node', $edit, t('Search'));
$this->assertNoText($user->getUsername(), 'Basic page node does not display author name when post settings are off.');
$this->assertNoText(format_date($node->getChangedTime(), 'short'), 'Basic page node does not display post date when post settings are off.');
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment