You are here

function sarnia_entity_manage_form in Sarnia 7

This form appears on a "Sarnia" tab on Search API server configuration pages. It allows administrators to "enable sarnia" for a particular server; when Sarnia is enabled for a Search API server, a Sarnia entity type is registered and a Search API index is created.

See also

_sarnia_entity_manage_form_access(), sarnia_entity_manage_form_submit(), sarnia_entity_type_save(), search_api_index_insert()

1 string reference to 'sarnia_entity_manage_form'
sarnia_menu in ./sarnia.module
Implements hook_menu().

File

./sarnia.entities.inc, line 121

Code

function sarnia_entity_manage_form($form, &$form_state, $server) {
  $form['server'] = array(
    '#type' => 'value',
    '#value' => $server,
  );

  // Look up the entity type for this server.
  $entity_type_exists = FALSE;
  if ($entity_type_machine_name = sarnia_entity_server_name_load($server->machine_name)) {
    $entity_type = sarnia_entity_type_load($entity_type_machine_name);
    $entity_type_exists = TRUE;
  }

  // sarnia_index_get_options() does the same thing, but expects an index
  // machine name, which we don't have at this point.
  $options = array();
  foreach ($server
    ->getRemoteFields() as $key => $field) {
    if ($field
      ->isPossibleKey()) {
      $options[$key] = $key . ' (' . $field
        ->getType() . ')';
    }
  }
  if (!$entity_type_exists) {
    $form['info'] = array(
      '#type' => 'container',
    );
    $form['info']['info'] = array(
      '#markup' => '<h3>' . t('Would you like to enable an entity type based on data within this Solr core?') . '</h3>',
    );
    $form['id_field'] = array(
      '#type' => 'select',
      '#title' => t('ID field'),
      '#required' => TRUE,
      '#options' => $options,
      '#default_value' => isset($entity_type['id_field']) ? $entity_type['id_field'] : key($options),
      '#description' => t('Choose a field from the Solr core to use as ids for this entity type. This field should contain unique integer values. Only fields that are stored, not dynamic, and not multi-valued according to the Solr <code>schema.xml</code> may be used.'),
    );
    $form['enable'] = array(
      '#type' => 'submit',
      '#name' => 'enable',
      '#value' => t('Enable'),
    );
  }
  else {
    $form['info'] = array(
      '#type' => 'container',
    );
    $form['info']['info'] = array(
      '#markup' => '<h3>' . t('An entity based on data within this Solr core is enabled.') . '</h3>' . '<div><strong>' . t('Label') . ':</strong> ' . check_plain($entity_type['label']) . '</div>' . '<div><strong>' . t('Machine name') . ':</strong> ' . check_plain($entity_type['machine_name']) . '</div>' . '<div><strong>' . t('Search API server') . ':</strong> ' . check_plain($entity_type['search_api_server']) . '</div>' . '<div><strong>' . t('Search API index') . ':</strong> ' . check_plain($entity_type['search_api_index']) . '</div>' . '<div><strong>' . t('ID field') . ':</strong> ' . check_plain($options[$entity_type['id_field']]) . '</div>',
    );
    $form['delete_link'] = array(
      '#type' => 'container',
    );
    $form['delete_link']['delete_link'] = array(
      '#type' => 'link',
      '#title' => t('Delete this entity type.'),
      '#href' => 'admin/config/search/search_api/server/' . $server->machine_name . '/sarnia/delete',
    );
  }
  return $form;
}