You are here

function _acquia_make_environment_url in Acquia Connector 7.3

Ajjax callback to set enviroment fields in form.

Parameters

mixed $form: Form array.

mixed $form_state: Form state array.

Return value

mixed Returns Form array with field values.

1 string reference to '_acquia_make_environment_url'
acquia_search_form_apachesolr_environment_edit_form_alter in acquia_search/acquia_search.module
Implements hook_form_FORM_ID_alter().

File

acquia_search/acquia_search.module, line 563
Integration between Acquia Drupal and Acquia's hosted solr search service.

Code

function _acquia_make_environment_url($form, &$form_state) {
  $solr_version = $form_state['values']['acquia_search_solr_core'];
  variable_set('acquia_search_solr_core', $form_state['values']['acquia_search_solr_core']);
  if ($solr_version == ACQUIA_SEARCH_SOLR_CORE_V2) {
    if (apachesolr_environment_load(ACQUIA_SEARCH_ENVIRONMENT_ID)) {
      $message = t('Search environment already exists.');
      drupal_set_message($message, 'warning');
    }
    $acquia_environment = acquia_search_get_environment();

    // Override default values.
    foreach ($acquia_environment as $key => $value) {
      $environment[$key] = $value;
    }
    $form['name']['#value'] = t('Acquia Search');
    $form['url']['#value'] = $environment['url'];
    $form['env_id']['#value'] = ACQUIA_SEARCH_ENVIRONMENT_ID;
    $form['service_class']['#value'] = $environment['service_class'];

    // Allow other modules to override this.
    drupal_alter('acquia_search_enable', $environment);
  }
  elseif ($solr_version == ACQUIA_SEARCH_SOLR_CORE_V3) {
    $api = _acquia_search_solr_get_api();
    if (empty($api)) {
      $form['name']['#value'] = '';
      $form['url']['#value'] = '';
      $form['env_id']['#value'] = '';
      $form['service_class']['#value'] = '';
      $form_state['rebuild'] = TRUE;
      return $form;
    }
    if ($env = apachesolr_environment_load(ACQUIA_SEARCH_V3_ENVIRONMENT_ID)) {
      if (!empty($env['service_class'])) {
        $message = t('Search environment already exists.');
        drupal_set_message($message, 'warning');
      }
    }
    $preferred_index_service = $api
      ->getPreferredIndexService();
    if (!$preferred_index_service
      ->isPreferredIndexAvailable()) {
      $message = AcquiaSearchSolrMessages::getNoPreferredIndexError($preferred_index_service
        ->getAvailableIndexesIds());
      drupal_set_message($message, 'warning');
      $form['name']['#value'] = '';
      $form['url']['#value'] = '';
      $form['env_id']['#value'] = '';
      $form['service_class']['#value'] = '';
      $form_state['rebuild'] = TRUE;
      return $form;
    }
    $environment = apachesolr_environment_load(ACQUIA_SEARCH_V3_ENVIRONMENT_ID);
    if (!$environment) {
      $environment = [
        'env_id' => ACQUIA_SEARCH_V3_ENVIRONMENT_ID,
        'conf' => [],
      ];

      // Copy bundles from the previous default environment.
      $orig_env_id = apachesolr_default_environment();
      $orig_env = apachesolr_environment_load($orig_env_id);
      $environment['index_bundles'] = $orig_env['index_bundles'];
    }
    $environment['name'] = t('Acquia Search');
    $environment['service_class'] = AcquiaSearchSolrService::class;
    $preferred_index = $preferred_index_service
      ->getPreferredIndex();
    $environment['url'] = sprintf('https://%s', $preferred_index['data']['index_url']);
    $form['#environmet'] = $environment;
    $form['name']['#value'] = $environment['name'];
    $form['url']['#value'] = $environment['url'];
    $form['env_id']['#value'] = ACQUIA_SEARCH_V3_ENVIRONMENT_ID;
    $form['service_class']['#value'] = $environment['service_class'];
  }
  else {
    $form['name']['#value'] = '';
    $form['url']['#value'] = '';
    $form['env_id']['#value'] = '';
    $form['service_class']['#value'] = '';
  }
  $form_state['rebuild'] = TRUE;
  return $form;
}