/** * {@inheritdoc} */ public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) { try { parent::init($view, $display, $options); $this->index = static::getIndexFromTable($view->storage->get('base_table')); if (!$this->index) { $this->abort(new FormattableMarkup('View %view is not based on Search API but tries to use its query plugin.', array('%view' => $view->storage->label()))); } $this->retrievedProperties = array_fill_keys($this->index->getDatasourceIds(), array()); $this->retrievedProperties[NULL] = array(); $this->query = $this->index->query(); $this->query->setParseMode($this->options['parse_mode']); $this->query->addTag('views'); $this->query->addTag('views_' . $view->id()); $this->query->setOption('search_api_view', $view); } catch (\Exception $e) { $this->abort($e->getMessage()); } }
/** * Builds the form for the basic index properties. * * @param \Drupal\search_api\IndexInterface $index * The index that is being created or edited. */ public function buildEntityForm(array &$form, FormStateInterface $form_state, IndexInterface $index) { $form['#tree'] = TRUE; $form['name'] = array('#type' => 'textfield', '#title' => $this->t('Index name'), '#description' => $this->t('Enter the displayed name for the index.'), '#default_value' => $index->label(), '#required' => TRUE); $form['id'] = array('#type' => 'machine_name', '#default_value' => $index->id(), '#maxlength' => 50, '#required' => TRUE, '#machine_name' => array('exists' => array($this->getIndexStorage(), 'load'), 'source' => array('name'))); // If the user changed the datasources or the tracker, notify them that they // need to be configured. // @todo Only do that if the datasources/tracker have configuration forms. // (Same in \Drupal\search_api\Form\ServerForm.) $values = $form_state->getValues(); if (!empty($values['datasources'])) { drupal_set_message($this->t('Please configure the used datasources.'), 'warning'); } if (!empty($values['tracker'])) { drupal_set_message($this->t('Please configure the used tracker.'), 'warning'); } $form['#attached']['library'][] = 'search_api/drupal.search_api.admin_css'; $datasource_options = array(); foreach ($this->getDatasourcePluginManager()->getDefinitions() as $datasource_id => $definition) { $datasource_options[$datasource_id] = !empty($definition['label']) ? $definition['label'] : $datasource_id; } $form['datasources'] = array('#type' => 'select', '#title' => $this->t('Data sources'), '#description' => $this->t('Select one or more data sources of items that will be stored in this index.'), '#options' => $datasource_options, '#default_value' => $index->getDatasourceIds(), '#multiple' => TRUE, '#required' => TRUE, '#ajax' => array('trigger_as' => array('name' => 'datasourcepluginids_configure'), 'callback' => '::buildAjaxDatasourceConfigForm', 'wrapper' => 'search-api-datasources-config-form', 'method' => 'replace', 'effect' => 'fade')); $form['datasource_configs'] = array('#type' => 'container', '#attributes' => array('id' => 'search-api-datasources-config-form'), '#tree' => TRUE); $form['datasource_configure_button'] = array('#type' => 'submit', '#name' => 'datasourcepluginids_configure', '#value' => $this->t('Configure'), '#limit_validation_errors' => array(array('datasources')), '#submit' => array('::submitAjaxDatasourceConfigForm'), '#ajax' => array('callback' => '::buildAjaxDatasourceConfigForm', 'wrapper' => 'search-api-datasources-config-form'), '#attributes' => array('class' => array('js-hide'))); $this->buildDatasourcesConfigForm($form, $form_state, $index); $tracker_options = $this->getTrackerPluginManager()->getOptionsList(); $form['tracker'] = array('#type' => 'radios', '#title' => $this->t('Tracker'), '#description' => $this->t('Select the type of tracker which should be used for keeping track of item changes.'), '#options' => $this->getTrackerPluginManager()->getOptionsList(), '#default_value' => $index->hasValidTracker() ? $index->getTracker()->getPluginId() : key($tracker_options), '#required' => TRUE, '#disabled' => !$index->isNew(), '#ajax' => array('trigger_as' => array('name' => 'trackerpluginid_configure'), 'callback' => '::buildAjaxTrackerConfigForm', 'wrapper' => 'search-api-tracker-config-form', 'method' => 'replace', 'effect' => 'fade'), '#access' => count($tracker_options) > 1); $form['tracker_config'] = array('#type' => 'container', '#attributes' => array('id' => 'search-api-tracker-config-form'), '#tree' => TRUE); $form['tracker_configure_button'] = array('#type' => 'submit', '#name' => 'trackerpluginid_configure', '#value' => $this->t('Configure'), '#limit_validation_errors' => array(array('tracker')), '#submit' => array('::submitAjaxTrackerConfigForm'), '#ajax' => array('callback' => '::buildAjaxTrackerConfigForm', 'wrapper' => 'search-api-tracker-config-form'), '#attributes' => array('class' => array('js-hide')), '#access' => count($tracker_options) > 1); $this->buildTrackerConfigForm($form, $form_state, $index); $form['server'] = array('#type' => 'radios', '#title' => $this->t('Server'), '#description' => $this->t('Select the server this index should use. Indexes cannot be enabled without a connection to a valid, enabled server.'), '#options' => array(NULL => '<em>' . $this->t('- No server -') . '</em>') + $this->getServerOptions(), '#default_value' => $index->hasValidServer() ? $index->getServerId() : NULL); $form['status'] = array('#type' => 'checkbox', '#title' => $this->t('Enabled'), '#description' => $this->t('Only enabled indexes can be used for indexing and searching. This setting will only take effect if the selected server is also enabled.'), '#default_value' => $index->status(), '#disabled' => !$index->status() && (!$index->hasValidServer() || !$index->getServer()->status()), '#states' => array('invisible' => array(':input[name="server"]' => array('value' => '')))); $form['description'] = array('#type' => 'textarea', '#title' => $this->t('Description'), '#description' => $this->t('Enter a description for the index.'), '#default_value' => $index->getDescription()); $form['options'] = array('#tree' => TRUE, '#type' => 'details', '#title' => $this->t('Index options'), '#collapsed' => TRUE); // We display the "read-only" flag along with the other options, even though // it is a property directly on the index object. We use "#parents" to move // it to the correct place in the form values. $form['options']['read_only'] = array('#type' => 'checkbox', '#title' => $this->t('Read only'), '#description' => $this->t('Do not write to this index or track the status of items in this index.'), '#default_value' => $index->isReadOnly(), '#parents' => array('read_only')); $form['options']['index_directly'] = array('#type' => 'checkbox', '#title' => $this->t('Index items immediately'), '#description' => $this->t('Immediately index new or updated items instead of waiting for the next cron run. This might have serious performance drawbacks and is generally not advised for larger sites.'), '#default_value' => $index->getOption('index_directly')); $form['options']['cron_limit'] = array('#type' => 'textfield', '#title' => $this->t('Cron batch size'), '#description' => $this->t('Set how many items will be indexed at once when indexing items during a cron run. "0" means that no items will be indexed by cron for this index, "-1" means that cron should index all items at once.'), '#default_value' => $index->getOption('cron_limit'), '#size' => 4); }
/** * Checks whether the index's datasources changed and reacts accordingly. * * Used as a helper method in postSave(). Should only be called when the index * was enabled before the change and remained so. * * @param \Drupal\search_api\IndexInterface $original * The previous version of the index. */ protected function reactToDatasourceSwitch(IndexInterface $original) { // Asserts that the index was enabled before saving and will still be // enabled afterwards. Otherwise, this method should not be called. assert('$this->status() && $original->status()', '::reactToDatasourceSwitch should only be called when the index is enabled'); $new_datasource_ids = $this->getDatasourceIds(); $original_datasource_ids = $original->getDatasourceIds(); if ($new_datasource_ids != $original_datasource_ids) { $added = array_diff($new_datasource_ids, $original_datasource_ids); $removed = array_diff($original_datasource_ids, $new_datasource_ids); $index_task_manager = \Drupal::getContainer()->get('search_api.index_task_manager'); $index_task_manager->stopTracking($this, $removed); $index_task_manager->startTracking($this, $added); } }
/** * Checks whether the index's datasources changed and reacts accordingly. * * Used as a helper method in postSave(). Should only be called when the index * was enabled before the change and remained so. * * @param \Drupal\search_api\IndexInterface $original * The previous version of the index. */ protected function reactToDatasourceSwitch(IndexInterface $original) { $new_datasource_ids = $this->getDatasourceIds(); $original_datasource_ids = $original->getDatasourceIds(); if ($new_datasource_ids != $original_datasource_ids) { $added = array_diff($new_datasource_ids, $original_datasource_ids); $removed = array_diff($original_datasource_ids, $new_datasource_ids); $index_task_manager = Utility::getIndexTaskManager(); $index_task_manager->stopTracking($this, $removed); $index_task_manager->startTracking($this, $added); } }
/** * Checks whether the index's datasources changed and reacts accordingly. * * Used as a helper method in postSave(). Should only be called when the index * was enabled before the change and remained so. * * @param \Drupal\search_api\IndexInterface $original * The previous version of the index. */ protected function reactToDatasourceSwitch(IndexInterface $original) { $new_datasource_ids = $this->getDatasourceIds(); $original_datasource_ids = $original->getDatasourceIds(); if ($new_datasource_ids != $original_datasource_ids) { $removed = array_diff($original_datasource_ids, $new_datasource_ids); $added = array_diff($new_datasource_ids, $original_datasource_ids); foreach ($removed as $datasource_id) { $this->getTracker()->trackAllItemsDeleted($datasource_id); } foreach ($added as $datasource_id) { $datasource = $this->getDatasource($datasource_id); $item_ids = $datasource->getItemIds(); $this->trackItemsInserted($datasource_id, $item_ids); } } }
/** * {@inheritdoc} */ public function getDatasourceIds() { return $this->entity->getDatasourceIds(); }
/** * Change the way the index's field names are mapped to Solr field names that * store only the first value of the field. * * @param \Drupal\search_api\IndexInterface $index * The index whose field mappings are altered. * @param array $fields * An associative array containing the index field names mapped to their Solr * counterparts. The special fields 'search_api_id' and 'search_api_relevance' * are also included. */ function hook_search_api_solr_single_value_field_mapping_alter(\Drupal\search_api\IndexInterface $index, array &$fields) { if (in_array('entity:node', $index->getDatasourceIds()) && isset($fields['entity:node|body'])) { $fields['entity:node|body'] = 'ts_entity$node|body_value'; } }