You are here

data_search.admin.inc in Data 8

File

data_search/data_search.admin.inc
View source
<?php

/**
 * Views handler configuration form.
 */
function data_search_admin_form($form, &$form_state, $table) {
  $messenger = \Drupal::messenger();
  drupal_set_title($table
    ->get('title'));
  $schema = $table
    ->get('table_schema');
  $meta = $table
    ->get('meta');
  $form = array();
  if (count($schema['primary key']) != 1) {
    $messenger
      ->addError(t('Only tables with a single-field primary key can be indexed.'), 'error');
    return $form;
  }

  // Keep table.
  $form['#table'] = $table;

  // Existing fields.
  $form['fields'] = array(
    '#tree' => TRUE,
  );
  if (isset($schema['fields'])) {
    $enabled_fields = data_search_get_fields($table);
    foreach ($schema['fields'] as $field_name => $field) {
      $form['fields'][$field_name] = array();
      $form['fields'][$field_name]['name'] = array(
        '#value' => $field_name,
      );
      $form['fields'][$field_name]['search'] = array(
        '#type' => 'checkbox',
        '#default_value' => in_array($field_name, $enabled_fields),
      );
    }
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}

/**
 * Submit handler for search form.
 */
function data_search_admin_form_submit(&$form, &$form_state) {
  $table = $form['#table'];
  $meta = $table
    ->get('meta');
  if (isset($form_state['values']['fields'])) {
    foreach ($form_state['values']['fields'] as $field_name => $settings) {
      foreach ($settings as $setting => $value) {
        $meta['fields'][$field_name][$setting] = $value;
      }
    }
  }
  $table
    ->update(array(
    'meta' => $meta,
  ));
  views_invalidate_cache();
}

/**
 * Theme data_search_admin_form.
 */
function theme_data_search_admin_form($variables) {
  $form = $variables['form'];
  $render_service = \Drupal::service('renderer');

  // Format existing fields.
  $rows = array();
  foreach (element_children($form['fields']) as $e) {
    $row = array();
    foreach (element_children($form['fields'][$e]) as $f) {
      $row[] = $render_service
        ->render($form['fields'][$e][$f]);
    }
    $rows[] = $row;
  }
  $header = array(
    t('Name'),
    t('Search index'),
  );
  $output = theme('table', array(
    'header' => $header,
    'rows' => $rows,
  ));
  $output .= $render_service
    ->render($form);
  return $output;
}

Functions

Namesort descending Description
data_search_admin_form Views handler configuration form.
data_search_admin_form_submit Submit handler for search form.
theme_data_search_admin_form Theme data_search_admin_form.