/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $index = $this->entity; // Do not allow the form to be cached. See // \Drupal\views_ui\ViewEditForm::form(). $form_state->disableCache(); if ($index instanceof UnsavedConfigurationInterface && $index->hasChanges()) { if ($index->isLocked()) { $form['#disabled'] = TRUE; $username = array('#theme' => 'username', '#account' => $index->getLockOwner($this->entityTypeManager)); $lock_message_substitutions = array('@user' => $this->getRenderer()->render($username), '@age' => $this->dateFormatter->formatTimeDiffSince($index->getLastUpdated()), ':url' => $index->toUrl('break-lock-form')->toString()); $form['locked'] = array('#type' => 'container', '#attributes' => array('class' => array('index-locked', 'messages', 'messages--warning')), '#children' => $this->t('This index is being edited by user @user, and is therefore locked from editing by others. This lock is @age old. Click here to <a href=":url">break this lock</a>.', $lock_message_substitutions), '#weight' => -10); } } $args['%index'] = $index->label(); $form['#title'] = $this->t('Add fields to index %index', $args); $form['properties'] = array('#theme' => 'search_api_form_item_list'); $datasources = array('' => NULL); $datasources += $this->entity->getDatasources(); foreach ($datasources as $datasource) { $form['properties'][] = $this->getDatasourceListItem($datasource); } // Log any unmapped types that were encountered. if ($this->unmappedFields) { $unmapped_types = array(); foreach ($this->unmappedFields as $type => $fields) { $unmapped_types[] = implode(', ', $fields) . ' (' . new FormattableMarkup('type @type', array('@type' => $type)) . ')'; } $vars['@fields'] = implode('; ', $unmapped_types); $vars['%index'] = $this->entity->label(); \Drupal::logger('search_api')->warning('Warning while retrieving available fields for index %index: could not find a type mapping for the following fields: @fields.', $vars); } $form['actions'] = $this->actionsElement($form, $form_state); return $form; }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { parent::submitForm($form, $form_state); /** @var $index \Drupal\search_api\IndexInterface */ $index = $this->getEntity(); $index->setOptions($form_state->getValue('options', array()) + $this->originalEntity->getOptions()); $datasources = $form_state->getValue('datasources', array()); /** @var \Drupal\search_api\Datasource\DatasourceInterface[] $datasource_plugins */ $datasource_plugins = $this->originalEntity->getDatasources(FALSE); $datasource_settings = array(); foreach ($datasources as $datasource_id) { $datasource = $datasource_plugins[$datasource_id]; $datasource_form = !empty($form['datasource_configs'][$datasource_id]) ? $form['datasource_configs'][$datasource_id] : array(); $datasource_form_state = new SubFormState($form_state, array('datasource_configs', $datasource_id)); $datasource->submitConfigurationForm($datasource_form, $datasource_form_state); $datasource_settings[$datasource_id] = $datasource; } $index->setDatasources($datasource_settings); // Call submitConfigurationForm() for the (possibly new) tracker. // @todo It seems if we change the tracker, we would validate/submit the old // tracker's form using the new tracker. Shouldn't be done, of course. // Similar above for datasources, though there of course the values will // just always be empty (because datasources have their plugin ID in the // form structure). $tracker_id = $form_state->getValue('tracker', NULL); if ($this->originalEntity->getTrackerId() == $tracker_id) { $tracker = $this->originalEntity->getTrackerInstance(); } else { $tracker = $this->trackerPluginManager->createInstance($tracker_id, array('index' => $this->originalEntity)); } $tracker_form_state = new SubFormState($form_state, array('tracker_config')); $tracker->submitConfigurationForm($form['tracker_config'], $tracker_form_state); $index->setTracker($tracker); }
/** * Tests whether module dependencies are handled correctly. */ public function testModuleDependency() { // Test with all types of plugins at once. $datasources['search_api_test_dependencies'] = \Drupal::getContainer()->get('plugin.manager.search_api.datasource')->createInstance('search_api_test_dependencies', array('index' => $this->index)); $datasources['entity:user'] = \Drupal::getContainer()->get('plugin.manager.search_api.datasource')->createInstance('entity:user', array('index' => $this->index)); $this->index->setDatasources($datasources); $processor = \Drupal::getContainer()->get('plugin.manager.search_api.processor')->createInstance('search_api_test_dependencies'); $this->index->addProcessor($processor); $tracker = \Drupal::getContainer()->get('plugin.manager.search_api.tracker')->createInstance('search_api_test_dependencies'); $this->index->setTracker($tracker); $this->index->save(); // Check the dependencies were calculated correctly. $dependencies = $this->index->getDependencies(); $this->assertContains('search_api_test_dependencies', $dependencies['module'], 'Module dependency correctly inserted'); // When the index resets the tracker, it needs to know the ID of the default // tracker. \Drupal::configFactory()->getEditable('search_api.settings')->set('default_tracker', 'default')->save(); // Disabling modules in Kernel tests normally doesn't trigger any kind of // reaction, just removes it from the list of modules (e.g., to avoid // calling of a hook). Therefore, we have to trigger that behavior // ourselves. \Drupal::getContainer()->get('config.manager')->uninstall('module', 'search_api_test_dependencies'); // Reload the index and check it's still there. $this->reloadIndex(); $this->assertInstanceOf('Drupal\\search_api\\IndexInterface', $this->index, 'Index not removed'); // Make sure the dependency has been removed. $dependencies = $this->index->getDependencies(); $dependencies += array('module' => array()); $this->assertNotContains('search_api_test_dependencies', $dependencies['module'], 'Module dependency removed from index'); // Make sure all the plugins have been removed. $this->assertNotContains('search_api_test_dependencies', $this->index->getDatasources(), 'Datasource was removed'); $this->assertArrayNotHasKey('search_api_test_dependencies', $this->index->getProcessors(), 'Processor was removed'); $this->assertEquals('default', $this->index->getTrackerId(), 'Tracker was reset'); }
/** * {@inheritdoc} */ public function buildOptionsForm(&$form, FormStateInterface $form_state) { parent::buildOptionsForm($form, $form_state); /** @var \Drupal\search_api\Datasource\DatasourceInterface $datasource */ foreach ($this->index->getDatasources() as $datasource_id => $datasource) { $datasource_label = $datasource->label(); $bundles = $datasource->getBundles(); if (!$datasource->getViewModes()) { $form['view_modes'][$datasource_id] = array('#type' => 'item', '#title' => $this->t('Default View mode for datasource %name', array('%name' => $datasource_label)), '#description' => $this->t("This datasource doesn't have any view modes available. It is therefore not possible to display results of this datasource using this row plugin.")); continue; } foreach ($bundles as $bundle_id => $bundle_label) { $title = $this->t('View mode for datasource %datasource, bundle %bundle', array('%datasource' => $datasource_label, '%bundle' => $bundle_label)); $view_modes = $datasource->getViewModes($bundle_id); if (!$view_modes) { $form['view_modes'][$datasource_id][$bundle_id] = array('#type' => 'item', '#title' => $title, '#description' => $this->t("This bundle doesn't have any view modes available. It is therefore not possible to display results of this bundle using this row plugin.")); continue; } $form['view_modes'][$datasource_id][$bundle_id] = array('#type' => 'select', '#options' => $view_modes, '#title' => $title, '#default_value' => key($view_modes)); if (isset($this->options['view_modes'][$datasource_id][$bundle_id])) { $form['view_modes'][$datasource_id][$bundle_id]['#default_value'] = $this->options['view_modes'][$datasource_id][$bundle_id]; } } } }
/** * Overrides \Drupal\search_api\Processor\ProcessorPluginBase::supportsIndex(). * * This plugin only supports indexes containing users. */ public static function supportsIndex(IndexInterface $index) { foreach ($index->getDatasources() as $datasource) { if ($datasource->getEntityTypeId() == 'user') { return TRUE; } } return FALSE; }
/** * {@inheritdoc} */ public static function supportsIndex(IndexInterface $index) { foreach ($index->getDatasources() as $datasource) { if (in_array($datasource->getEntityTypeId(), array('node', 'comment'))) { return TRUE; } } return FALSE; }
public function getEntityTypes($return_bool = FALSE) { $types = array(); foreach ($this->index->getDatasources() as $datasource_id => $datasource) { if ($type = $datasource->getEntityTypeId()) { if ($return_bool) { return TRUE; } $types[$datasource_id] = $type; } } return $return_bool ? FALSE : $types; }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { parent::submitForm($form, $form_state); /** @var $index \Drupal\search_api\IndexInterface */ $index = $this->getEntity(); $index->setOptions($form_state->getValue('options', array()) + $this->originalEntity->getOptions()); $datasources = $form_state->getValue('datasources', array()); /** @var \Drupal\search_api\Datasource\DatasourceInterface[] $datasource_plugins */ $datasource_plugins = $this->originalEntity->getDatasources(FALSE); $datasource_configuration = array(); foreach ($datasources as $datasource_id) { $datasource = $datasource_plugins[$datasource_id]; $datasource_form = (!empty($form['datasource_configs'][$datasource_id])) ? $form['datasource_configs'][$datasource_id] : array(); $datasource_form_state = new SubFormState($form_state, array('datasource_configs', $datasource_id)); $datasource->submitConfigurationForm($datasource_form, $datasource_form_state); $datasource_configuration[$datasource_id] = $datasource->getConfiguration(); } $index->set('datasource_configs', $datasource_configuration); // Call submitConfigurationForm() for the (possibly new) tracker. // @todo It seems if we change the tracker, we would validate/submit the old // tracker's form using the new tracker. Shouldn't be done, of course. // Similar above for datasources, though there of course the values will // just always be empty (because datasources have their plugin ID in the // form structure). $tracker_id = $form_state->getValue('tracker', NULL); if ($this->originalEntity->getTrackerId() == $tracker_id) { $tracker = $this->originalEntity->getTracker(); } else { $tracker = $this->trackerPluginManager->createInstance($tracker_id, array('index' => $this->originalEntity)); } $tracker_form_state = new SubFormState($form_state, array('tracker_config')); $tracker->submitConfigurationForm($form['tracker_config'], $tracker_form_state); $index->set('tracker_config', $tracker->getConfiguration()); // Invalidate caches, so this gets picked up by the views wizard. Cache::invalidateTags(array('views_data')); // Remove this line when https://www.drupal.org/node/2370365 gets fixed. Cache::invalidateTags(array('extension:views')); }
/** * {@inheritdoc} */ public function getDatasources($only_enabled = TRUE) { return $this->entity->getDatasources($only_enabled); }