Skip to content

Instantly share code, notes, and snippets.

@larowlan
Last active April 11, 2016 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larowlan/0f7f42440d538c3c7bcc4ba7967ee13c to your computer and use it in GitHub Desktop.
Save larowlan/0f7f42440d538c3c7bcc4ba7967ee13c to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Contains \Drupal\search_config\Plugin\Search\ConfigurableNodeSearch.
*/
namespace Drupal\search_config\Plugin\Search;
use Drupal\Core\Database\StatementInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
use Drupal\node\Plugin\Search\NodeSearch;
/**
* Configurable searching for node entities using the Search module index.
*
* @SearchPlugin(
* id = "configurable_node_search",
* title = @Translation("Content")
* )
*/
class ConfigurableNodeSearch extends NodeSearch {
/**
* {@inheritdoc}
*/
protected function prepareResults(StatementInterface $found) {
$results = parent::prepareResults($found);
foreach ($results as $ix => $result) {
if (in_array($result['node']->bundle(), $this->configuration['exclude_node_types'], TRUE)) {
unset($results[$ix]);
}
}
return array_values($results);
}
/**
* Indexes a single node.
*
* @param \Drupal\node\NodeInterface $node
* The node to index.
*/
protected function indexNode(NodeInterface $node) {
if (in_array($node->bundle(), $this->configuration['exclude_node_types'], TRUE)) {
return;
}
parent::indexNode($node);
}
/**
* {@inheritdoc}
*/
public function searchFormAlter(array &$form, FormStateInterface $form_state) {
parent::searchFormAlter($form, $form_state);
// Unset hidden node-types.
foreach ($this->configuration['exclude_node_types'] as $type) {
unset($form['advanced']['types-fieldset']['type']['#options'][$type]);
}
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
$configuration = parent::defaultConfiguration();
$configuration['exclude_node_types'] = [];
return $configuration;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$types = array_map(array('\Drupal\Component\Utility\Html', 'escape'), node_type_get_names());
$form['exclude_node_types'] = [
'#type' => 'checkboxes',
'#title' => t('Exclude the following node-types:'),
'#options' => $types,
'#default_value' => $this->configuration['exclude_node_types'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['exclude_node_types'] = array_filter($form_state->getValue('exclude_node_types'));
}
/**
* {@inheritdoc}
*/
public function indexStatus() {
$total = $this->database->select('node', 'n')
->condition('type', $this->configuration['exclude_node_types'], 'NOT IN')
->countQuery()
->execute()
->fetchField();
$remaining = $this->database->query("SELECT COUNT(DISTINCT n.nid)
FROM {node} n LEFT JOIN {search_dataset} sd
ON sd.sid = n.nid AND sd.type = :type
WHERE sd.sid IS NULL OR sd.reindex <> 0
AND n.type NOT IN (:types[])", [
':type' => $this->getPluginId(),
':types[]' => $this->configuration['exclude_node_types'],
])->fetchField();
return array('remaining' => $remaining, 'total' => $total);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment