You are here

function search_by_page_attach_search_by_page_settings in Search by Page 8

Implements Search by Page hook_search_by_page_settings().

Adds a node type selection form to the Search by Page settings page, and some options related to presentation.

File

search_by_page_attach/search_by_page_attach.module, line 376
Module file for Search by Page Attachments, a sub-module for Search by Page.

Code

function search_by_page_attach_search_by_page_settings($environment) {
  $form = array();
  $form['search_by_page_attach'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#weight' => -70,
    '#title' => t('Attachments'),
  );

  // Explanation
  $form['search_by_page_attach']['explain'] = array(
    '#type' => 'markup',
    '#value' => '<p>' . t('Search by Page Attachments indexes file attachments on your content types for searching. The attachments are added to content types via the core File field or similar fields. Choose the content types you want to index attachments of below, and also choose which fields you would like to index.') . '</p>',
    '#weight' => -10,
  );
  $form['search_by_page_attach']['explain2'] = array(
    '#type' => 'markup',
    '#value' => '<p>' . t('You must choose at least one content type to index, and at least one field type, or no attachments will be indexed. Clear all content and field types or disable the Search by Page Attachments module to stop indexing file attachments.') . '</p>',
    '#weight' => -9,
  );

  // Which node types to index attachments for
  $form['search_by_page_attach']['search_by_page_attach_node_types'] = array(
    '#type' => 'select',
    '#multiple' => TRUE,
    '#title' => t('Content types to index attachments of'),
    '#default_value' => \Drupal::service('search_by_page.settings')
      ->getSetting('search_by_page_attach_node_types', $environment, array()),
    '#options' => node_type_get_names(),
    '#description' => t('Choose the content types whose file field attachments you would like to have indexed by Search by Page. Use the Control or Command key to select multiple types or undo your selections.'),
    '#weight' => 0,
  );

  // Which field types to index.
  // Note: Don't check for File type itself - leave it open to other
  // field modules that use 'fid', and let the user decide
  $fields = field_info_fields();
  $opts = array();
  foreach ($fields as $field) {
    $columns = $field['columns'];

    // See if this field has an 'fid' column. Assume it's a potential file
    // field if so.
    if (isset($field['columns']['fid'])) {
      $name = Html::escape($field['field_name']);
      $opts[$name] = Html::escape($name);
    }
  }
  if (count($opts)) {
    $form['search_by_page_attach']['search_by_page_attach_field_types'] = array(
      '#type' => 'select',
      '#multiple' => TRUE,
      '#title' => t('Fields to index'),
      '#default_value' => \Drupal::service('search_by_page.settings')
        ->getSetting('search_by_page_attach_field_types', $environment, array()),
      '#options' => $opts,
      '#description' => t('Choose which field types contain attachments you would like to have indexed by Search by Page. Use the Control or Command key to select multiple types or undo your selections.'),
      '#weight' => 1,
    );
  }
  else {
    $form['search_by_page_attach']['no_field_info'] = array(
      '#type' => 'markup',
      '#value' => '<p>' . t('No field types supporting file attachments were found. No attachments will be indexed.') . '</p>',
      '#weight' => 1,
    );
  }

  // Other options
  $form['search_by_page_attach']['search_by_page_options_section_markup'] = array(
    '#type' => 'markup',
    '#value' => '<p><strong>' . t('Options:') . '</strong></p>',
    '#weight' => 2,
  );
  $form['search_by_page_attach']['search_by_page_attach_only_listed'] = array(
    '#type' => 'checkbox',
    '#title' => t('Limit to displayed files'),
    '#default_value' => \Drupal::service('search_by_page.settings')
      ->getSetting('search_by_page_attach_only_listed', $environment, 0),
    '#description' => t('The File field allows you to specify whether a particular attached file should be displayed on the site. If you check this option, Search by Page Attachments will only search for files that have been marked as Displayed. Otherwise, all attached files will be searched.'),
    '#weight' => 3,
  );
  $form['search_by_page_attach']['search_by_page_attach_prepend_node_title'] = array(
    '#type' => 'checkbox',
    '#title' => t('Prepend content item title to file name'),
    '#default_value' => \Drupal::service('search_by_page.settings')
      ->getSetting('search_by_page_attach_prepend_node_title', $environment, 0),
    '#description' => t('When an attached file is presented to the user in search results, the results will display a title, which defaults to the file name. If you check this box, the content item title will be prepended to the file name.'),
    '#weight' => 4,
  );
  $form['search_by_page_attach']['search_by_page_attach_title_sep'] = array(
    '#type' => 'textfield',
    '#title' => t('Content item title separator'),
    '#default_value' => \Drupal::service('search_by_page.settings')
      ->getSetting('search_by_page_attach_title_sep', $environment, '/'),
    '#description' => t('If you have chosen to prepend the content item title to the file name, this separator will be put between the item title and the file name.'),
    '#weight' => 5,
  );
  $form['search_by_page_attach']['search_by_page_attach_use_description'] = array(
    '#type' => 'checkbox',
    '#title' => t('Substitute description for file name'),
    '#default_value' => \Drupal::service('search_by_page.settings')
      ->getSetting('search_by_page_attach_use_description', $environment, 0),
    '#description' => t('When an attached file is presented to the user in search results, the results will display a title, which is the file name by default. If you check this box, and the file has a description defined, the description will be substituted for the file name in the search result title.'),
    '#weight' => 6,
  );
  $form['search_by_page_attach']['search_by_page_attach_role'] = array(
    '#type' => 'radios',
    '#title' => t('Role for indexing'),
    '#options' => user_roles(),
    '#default_value' => \Drupal::service('search_by_page.settings')
      ->getSetting('search_by_page_attach_role', $environment, AccountInterface::ANONYMOUS_ROLE),
    '#weight' => 7,
    '#description' => t("When Search by Page indexes attached files for searching, the files will be viewed from the perspective and permissions of a user with this role."),
  );
  $times = array(
    '1' => t('1 second'),
    '3600' => t('1 hour'),
    '86400' => t('1 day'),
    '604800' => t('1 week'),
    '31449600' => t('1 year'),
    '0' => t('Never'),
  );
  $form['search_by_page_attach']['search_by_page_attach_min_time'] = array(
    '#type' => 'select',
    '#title' => t('Minimum reindexing time'),
    '#options' => $times,
    '#default_value' => \Drupal::service('search_by_page.settings')
      ->getSetting('search_by_page_attach_min_time', $environment, 0),
    '#weight' => 8,
    '#description' => t("Search by Page automatically indexes attachments on new content items, and reindexes attachments if the content item they are attached to is updated. After indexing these updated attachments, Search by Page can also cycle through previously-indexed attachments, in case the file may have been updated without the content item it is attached to being updated. This would be necessary if files on the site are maintained and updated by a separate process (e.g., log files, replacement files added by FTP, etc.). By default, Search by Page will not reindex an attachment until the content item it is attached to is updated, but if you need attachments to be reindexed, you can set the minimum indexing cycle time here. In this case, attachments will be reindexed periodically, along with other Search by Page content, after waiting at least the minimum time set here before reindexing."),
  );
  $form['search_by_page_attach']['search_by_page_attach_max_time'] = array(
    '#type' => 'select',
    '#title' => t('Maximum reindexing time'),
    '#options' => $times,
    '#default_value' => \Drupal::service('search_by_page.settings')
      ->getSetting('search_by_page_attach_max_time', $environment, 0),
    '#weight' => 9,
    '#description' => t("Conversely to the minimum reindexing time (see above), Search by Page can be set to prioritize reindexing each attachment (by marking it as needing immediate reindexing) after this amount of time has passed. This has higher priority than the cycle-through reindexing of the setting above.") . ' ' . t('But be careful with this setting! If you set it too small, it can interfere with new content being indexed, because the reindexed content will have equal priority to content that has never been indexed. So make sure your settings allow for enough time for new content to be indexed before forcing reindexing.'),
  );

  // Add a link to helper function config page
  $form['search_by_page_attach']['helpers'] = array(
    '#type' => 'markup',
    '#value' => '<p>' . l(t('Configure helper functions for extracting text from attachment files'), 'admin/config/search/search_files/helpers') . '</p>',
    '#weight' => 10,
  );
  return $form;
}