You are here

function _elasticsearch_connector_ec_index_process in Elasticsearch Connector 7.5

Same name and namespace in other branches
  1. 7 elasticsearch_connector.module \_elasticsearch_connector_ec_index_process()
  2. 7.2 elasticsearch_connector.module \_elasticsearch_connector_ec_index_process()

Build two dropdowns, one for the cluster and one for the indices.

Parameters

array $element:

Return value

arrat $element

1 string reference to '_elasticsearch_connector_ec_index_process'
elasticsearch_connector_element_info in ./elasticsearch_connector.module
Implements hook_element_info()

File

./elasticsearch_connector.module, line 322
This module provide an interface to connecting to the elasticsearch cluster and implementing the official Elasticsearch library.

Code

function _elasticsearch_connector_ec_index_process($element, &$form_state, $form) {
  $element['#tree'] = TRUE;
  $element_id = $element['#id'];
  $wrapper_id = $element_id . '-index-wrapper';

  // TODO: Add icon if the cluster is OK or not.
  if (empty($element['#cluster_id'])) {
    $element['cluster_id'] = array(
      '#type' => 'select',
      '#id' => $element_id . '-cluster-id',
      '#title' => t('Select cluster'),
      '#required' => $element['#required'],
      '#default_value' => isset($element['#default_value']) && is_array($element['#default_value']) && isset($element['#default_value']['cluster_id']) ? $element['#default_value']['cluster_id'] : '',
      // TODO: Allow this option to be overwritten and #value if we had such.
      '#description' => t('Select the cluster.'),
      '#ajax' => array(
        'callback' => '_elasticsearch_connector_ec_index_ajax',
        'wrapper' => $wrapper_id,
        'method' => 'replace',
        'effect' => 'fade',
      ),
    );
  }
  else {
    $element['cluster_id'] = array(
      '#type' => 'value',
      '#value' => $element['#cluster_id'],
    );
  }
  if (!isset($element['cluster_id']['#current_path'])) {
    $element['cluster_id']['#current_path'] = current_path();
  }
  if (empty($element['#skip_default_options'])) {
    $element['#only_active'] = isset($element['#only_active']) ? $element['#only_active'] : TRUE;
    $element['#empty_option'] = isset($element['#empty_option']) ? $element['#empty_option'] : TRUE;
    $clusters = elasticsearch_connector_cluster_load_all($element['#only_active'], $element['#empty_option']);
    $element['cluster_id']['#options'] = $clusters;
  }

  // TODO: We need to handle the incomming tree name if such.
  $links = $manage_indices = array();
  $index_options = array(
    '' => t('Select index'),
  );
  if (is_array($element['#value']) && !empty($element['#value']['cluster_id']) || !empty($element['#cluster_id'])) {
    $cluster_id = is_array($element['#value']) && !empty($element['#value']['cluster_id']) ? $element['#value']['cluster_id'] : $element['#cluster_id'];
    $index_options = array();
    try {
      $index_options = elasticsearch_connector_get_indices_options($cluster_id, TRUE);
    } catch (Exception $e) {
      if (!empty($element['#throw_exp'])) {
        throw $e;
      }
    }
    $links[] = array(
      'title' => t('Add index'),
      'href' => 'admin/config/elasticsearch-connector/clusters/' . $cluster_id . '/indices/add',
      'attributes' => array(
        'target' => '_blank',
        'class' => 'ec-index-dialog ec-index-dialog-add',
      ),
      'query' => array(
        'render' => 'elasticsearch-connector-dialog',
        'index_element_id' => $element_id . '-index',
        'cluster_element_id' => $element_id . '-cluster-id',
      ),
    );
    $manage_indices = array(
      'title' => t('Manage indices'),
      'href' => 'admin/config/elasticsearch-connector/clusters/' . $cluster_id . '/indices',
      'attributes' => array(
        'target' => '_blank',
        'class' => 'manage-indices',
      ),
    );
  }
  $index = '';
  if (isset($element['#value']) && is_array($element['#value']) && isset($element['#value']['index'])) {
    $index = $element['#value']['index'];
  }
  elseif (isset($element['#default_value']) && is_array($element['#default_value']) && isset($element['#default_value']['index'])) {
    $index = $element['#default_value']['index'];
  }
  if (!empty($index)) {
    $links[] = array(
      'title' => t('Edit @index index', array(
        '@index' => $index,
      )),
      'href' => 'admin/config/elasticsearch-connector/clusters/' . $cluster_id . '/indices/' . $index . '/edit',
      'attributes' => array(
        'target' => '_blank',
        'class' => 'ec-index-dialog ec-index-dialog-edit',
      ),
      'query' => array(
        'render' => 'elasticsearch-connector-dialog',
        'index_element_id' => $element_id . '-index',
        'cluster_element_id' => $element_id . '-cluster-id',
      ),
    );
  }
  if (!empty($manage_indices)) {
    $links[] = $manage_indices;
  }
  $element['index'] = array(
    '#type' => 'select',
    '#title' => t('Select index'),
    '#id' => $element_id . '-index',
    '#required' => $element['#required'],
    '#default_value' => isset($element['#default_value']) && is_array($element['#default_value']) && isset($element['#default_value']['index']) ? $element['#default_value']['index'] : '',
    '#description' => t('Select the index.'),
    '#options' => $index_options,
    '#ajax' => array(
      'callback' => '_elasticsearch_connector_ec_index_links_ajax',
      'wrapper' => $element['#id'] . '-dialog-links',
      'method' => 'replace',
      'effect' => 'fade',
    ),
    '#prefix' => '<div id="' . $wrapper_id . '">',
    '#suffix' => '<div id="' . $element['#id'] . '-dialog-links" class="dialog-links ' . $element['#id'] . '">' . theme('links__es_index_links', array(
      'links' => $links,
      'attributes' => array(
        'class' => 'index-dialog-links',
      ),
    )) . '</div></div>',
  );
  unset($element['#title']);
  $context = array(
    'form' => $form,
  );
  drupal_alter('ec_index_process', $element, $form_state, $context);
  return $element;
}