You are here

function sarnia_entity_type_save in Sarnia 7

Create or update a Sarnia entity type.

1 call to sarnia_entity_type_save()
sarnia_entity_manage_form_submit in ./sarnia.entities.inc
Submit function. Creates a Sarnia entity type for a Search API server.

File

./sarnia.entities.inc, line 27

Code

function sarnia_entity_type_save($entity_type) {
  db_merge('sarnia_entity_type')
    ->key(array(
    'machine_name' => $entity_type['machine_name'],
  ))
    ->fields(array(
    'label' => $entity_type['label'],
    'search_api_server' => $entity_type['search_api_server'],
    'search_api_index' => $entity_type['search_api_index'],
    'id_field' => $entity_type['id_field'],
  ))
    ->execute();

  // Add the sarnia field.
  $field = field_info_field('solr_document');
  $instance = field_info_instance($entity_type['machine_name'], 'solr_document', $entity_type['machine_name']);

  // Create the field if it doesn't already exist.
  if (empty($field)) {
    $field = array(
      'field_name' => 'solr_document',
      'type' => 'sarnia',
      'locked' => TRUE,
    );
    $field = field_create_field($field);
  }

  // Create the field instance if it doesn't already exist.
  if (empty($instance)) {
    $weight = -5;
    $instance = array(
      'field_name' => 'solr_document',
      'entity_type' => $entity_type['machine_name'],
      'bundle' => $entity_type['machine_name'],
      'label' => 'Solr Document',
      'widget_type' => 'sarnia_no_input',
      'widget' => array(),
      'required' => TRUE,
      'locked' => TRUE,
      'display' => array(),
    );
    field_create_instance($instance);
  }

  // Create a Search API Index for the Sarnia entity type, if one doesn't
  // already exist. The entity type and index will use matching machine names.
  if (!search_api_index_load($entity_type['machine_name'])) {
    $index = array(
      'name' => $entity_type['label'],
      'server' => $entity_type['search_api_server'],
      'machine_name' => $entity_type['machine_name'],
      'entity_type' => $entity_type['machine_name'],
      'item_type' => $entity_type['machine_name'],
      'enabled' => TRUE,
      'description' => 'This index is managed by the Sarnia module.',
      'options' => array(
        'cron_limit' => 0,
      ),
      'read_only' => 1,
    );
    search_api_index_insert($index);
  }

  // Reset the array returned by _sarnia_entity_types().
  drupal_static_reset('_sarnia_entity_types');
}