You are here

function current_search_settings_form in Facet API 7.2

Same name and namespace in other branches
  1. 6.3 contrib/current_search/plugins/export_ui/current_search_export_ui.class.php \current_search_settings_form()
  2. 7 contrib/current_search/plugins/export_ui/current_search_export_ui.class.php \current_search_settings_form()

Define the preset add/edit form.

See also

current_search_add_item_submit()

current_search_settings_form_submit()

1 string reference to 'current_search_settings_form'
current_search_ctools_export_ui.inc in contrib/current_search/plugins/export_ui/current_search_ctools_export_ui.inc

File

contrib/current_search/plugins/export_ui/current_search_export_ui.class.php, line 160
Export UI display customizations.

Code

function current_search_settings_form(&$form, &$form_state) {
  $item =& $form_state['item'];
  $form['info']['#weight'] = -30;
  if (empty($item->settings)) {
    $item->settings = current_search_get_defaults();
  }

  // NOTE: We need to add the #id in order for the machine_name to work.
  $form['info']['label'] = array(
    '#id' => 'edit-label',
    '#title' => t('Name'),
    '#type' => 'textfield',
    '#default_value' => $item->label,
    '#description' => t('The human-readable name of the current search block configuration.'),
    '#required' => TRUE,
    '#maxlength' => 255,
    '#size' => 30,
  );
  $form['info']['name'] = array(
    '#type' => 'machine_name',
    '#default_value' => $item->name,
    '#maxlength' => 32,
    '#machine_name' => array(
      'exists' => 'current_search_config_exists',
      'source' => array(
        'info',
        'label',
      ),
    ),
    '#disabled' => 'clone' != $form_state['form type'] && !empty($item->name),
    '#description' => t('The machine readable name of the current search block configuration. This value can only contain letters, numbers, and underscores.'),
  );
  $form['info']['searcher'] = array(
    '#type' => 'select',
    '#title' => t('Search page'),
    '#options' => current_search_get_searcher_options(),
    '#description' => t('The search page this configuration will be active on.'),
    '#default_value' => current_search_get_default_searcher(),
    '#access' => empty($item->name),
  );

  // Hide the standard buttons.
  $form['buttons']['#access'] = FALSE;

  // Add our custom buttons.
  $form['actions'] = array(
    '#type' => 'actions',
    '#weight' => -100,
  );

  // Gets destination from query string which is set when the page is navigated
  // to via a contextual link. Builds messages based on where user came from.
  if (isset($_GET['destination']) && !url_is_external($_GET['destination'])) {
    $submit_text = t('Save and go back to search page');
    $cancel_title = t('Return to the search page without saving configuration changes.');
    $url = drupal_parse_url($_GET['destination']);
  }
  else {
    $submit_text = t('Save and go back to list');
    $cancel_title = t('Return to the list without saving configuration changes.');
    $url = array();
  }
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save and edit'),
  );

  // Do not show the button if the page was navigated to via a contextual link
  // because it would redirect the user back to the search page.
  $form['actions']['submit_list'] = array(
    '#type' => 'submit',
    '#value' => $submit_text,
  );
  $form['actions']['cancel'] = array(
    '#type' => 'link',
    '#title' => t('Cancel'),
    '#href' => !$url ? 'admin/config/search/current_search' : $url['path'],
    '#options' => !$url ? array() : array(
      'query' => $url['query'],
    ),
    '#attributes' => array(
      'title' => $cancel_title,
    ),
  );
  if (empty($item->name)) {
    $description = t('Add a new current search block configuration.');
  }
  else {
    $description = t('Add new items to the current search block or configure existing ones.');
  }
  $form['description'] = array(
    '#prefix' => '<div class="current-search-description">',
    '#suffix' => '</div>',
    '#markup' => $description,
    '#weight' => -90,
  );
  drupal_add_css(drupal_get_path('module', 'current_search') . '/current_search.css');

  // If we are creating the configuration, only display the basic config items.
  // Otherwise set the breadcrumb due to possible bug in CTools Export UI.
  if (empty($form['info']['name']['#default_value'])) {
    return;
  }

  // Gets list of plugins, sanitizes label for output.
  $plugins = array_map('check_plain', current_search_get_plugins());

  ////

  ////

  //// Add plugin section

  ////

  ////
  $form['plugins_title'] = array(
    '#type' => 'item',
    '#title' => t('Add item to block'),
  );
  $form['plugins'] = array(
    '#tree' => TRUE,
    '#prefix' => '<div class="clearfix">',
    '#suffix' => '</div>',
  );
  $form['plugins']['plugin'] = array(
    '#type' => 'select',
    '#title' => t('Type'),
    '#options' => $plugins,
    '#prefix' => '<div class="current-search-setting current-search-plugin">',
    '#suffix' => '</div>',
  );
  $form['plugins']['item_label'] = array(
    '#title' => t('Name'),
    '#type' => 'textfield',
    '#default_value' => '',
    '#required' => FALSE,
    '#size' => 30,
    '#prefix' => '<div class="current-search-setting current-search-label">',
    '#suffix' => '</div>',
  );
  $form['plugins']['item_name'] = array(
    '#type' => 'machine_name',
    '#default_value' => '',
    '#maxlength' => 32,
    '#machine_name' => array(
      'exists' => 'current_search_item_exists',
      'source' => array(
        'plugins',
        'item_label',
      ),
    ),
    '#required' => FALSE,
    '#description' => t('The machine readable name of the item being added to the current search block. This value can only contain letters, numbers, and underscores.'),
  );
  $form['plugins']['actions'] = array(
    '#type' => 'actions',
    '#prefix' => '<div class="current-search-setting current-search-button">',
    '#suffix' => '</div>',
  );
  $form['plugins']['actions']['add_item'] = array(
    '#type' => 'submit',
    '#value' => t('Add item'),
    '#submit' => array(
      'current_search_add_item_submit',
    ),
    '#validate' => array(
      'current_search_add_item_validate',
    ),
  );

  ////

  ////

  //// Sort settings

  ////

  ////

  // Ensure the theme function is available after upgrading to 7.x-1.3. A cache
  // clear is all that is needed to fix the broken sort table, however this hack
  // ensures that the theme function is available without having to do so.
  // @todo Remove this in later versions of the module.
  // @see http://drupal.org/node/1795556
  module_load_include('inc', 'current_search', 'current_search.theme');
  $form['plugin_sort'] = array(
    '#type' => 'item',
    '#access' => !empty($item->settings['items']),
    '#title' => t('Item display order'),
    '#theme' => 'current_search_sort_settings_table',
    '#current_search' => $item->settings,
    '#tree' => TRUE,
  );

  // Builds checkbox options and weight dropboxes.
  foreach ($item->settings['items'] as $name => $settings) {
    $form['plugin_sort'][$name]['item'] = array(
      '#markup' => check_plain($settings['label']),
    );
    $form['plugin_sort'][$name]['remove'] = array(
      '#type' => 'link',
      '#title' => t('Remove item'),
      '#href' => 'admin/config/search/current_search/item/' . $item->name . '/delete/' . $name,
    );
    $form['plugin_sort'][$name]['weight'] = array(
      '#type' => 'weight',
      '#title' => t('Weight for @title', array(
        '@title' => $settings['label'],
      )),
      '#title_display' => 'invisible',
      '#delta' => 50,
      '#default_value' => isset($settings['weight']) ? $settings['weight'] : 0,
      '#attributes' => array(
        'class' => array(
          'current-search-sort-weight',
        ),
      ),
    );
  }

  ////

  ////

  //// Filter settings

  ////

  ////
  $form['plugin_settings_title'] = array(
    '#type' => 'item',
    '#access' => !empty($item->settings['items']),
    '#title' => t('Item settings'),
  );
  $form['plugin_settings'] = array(
    '#type' => 'vertical_tabs',
    '#tree' => TRUE,
  );

  // Builds table, adds settings to vertical tabs.
  $has_settings = FALSE;
  foreach ($item->settings['items'] as $name => $settings) {
    if ($class = ctools_plugin_load_class('current_search', 'items', $settings['id'], 'handler')) {
      $plugin = new $class($name, $item);

      // Initializes vertical tab for the item's settings.
      $form['plugin_settings'][$name] = array(
        '#type' => 'fieldset',
        '#title' => check_plain($settings['label']),
        '#group' => 'settings',
        '#tree' => TRUE,
      );
      $form['plugin_settings'][$name]['id'] = array(
        '#type' => 'value',
        '#value' => $settings['id'],
      );
      $form['plugin_settings'][$name]['label'] = array(
        '#type' => 'value',
        '#value' => $settings['label'],
      );

      // Gets settings from plugin.
      $plugin
        ->settingsForm($form['plugin_settings'][$name], $form_state);
      $has_settings = TRUE;
    }
  }

  // Removes fieldset if there aren't any settings.
  if (!$has_settings) {
    unset($form['plugin_settings']);
  }

  ////

  ////

  //// Advanced settings

  ////

  ////
  $form['advanced_settings_title'] = array(
    '#type' => 'item',
    '#title' => t('Advanced settings'),
  );
  $form['advanced_settings'] = array(
    '#tree' => TRUE,
  );

  // This setting was originally intended as a way for site builders to enable
  // the current search block on pages where no keywords were submitted by the
  // end user, which is known as an "empty search". The display settings were
  // expanded beyond empty searches at http://drupal.org/node/1779670 leaving
  // us with the unfortunate "empty_searches" key which no longer reflects what
  // this setting does.
  $form['advanced_settings']['empty_searches'] = array(
    '#type' => 'radios',
    '#title' => t('Display settings'),
    '#options' => array(
      CURRENT_SEARCH_DISPLAY_KEYS => t('Display only when keywords are entered.'),
      CURRENT_SEARCH_DISPLAY_ALWAYS => t('Display on empty searches where no keywords are entered.'),
      CURRENT_SEARCH_DISPLAY_FILTERS => t('Display only when one or more facet items are active.'),
      CURRENT_SEARCH_DISPLAY_KEYS_FILTERS => t('Display when either keywords are entered one or more facet items are active.'),
    ),
    '#default_value' => $item->settings['advanced']['empty_searches'],
    '#description' => t('This setting determines when the current search block will be displayed.'),
  );
}