search_by_page_users.module in Search by Page 8
Module file for Search by Page Users, a sub-module for Search by Page.
Allows you to add user profiles, by role, to Search by Page.
File
search_by_page_users/search_by_page_users.moduleView source
<?php
/**
* @file
* Module file for Search by Page Users, a sub-module for Search by Page.
*
* Allows you to add user profiles, by role, to Search by Page.
* @ingroup search_by_page
*/
use Drupal\Core\Session\AccountInterface;
use Drupal\Component\Utility\Html;
/**
* Implements Search by Page hook_search_by_page_paths().
*
* Returns a list of all the user pages that should be indexed.
*/
function search_by_page_users_search_by_page_paths($environment) {
// What user roles do they want to index?
$rolelist = \Drupal::service('search_by_page.settings')
->getSetting('search_by_page_users_roles_indexed', $environment, array());
if (!is_array($rolelist) || !count($rolelist)) {
return array();
}
// This variable comes from a checkbox array form element. So it
// gives us an array like '3' => '3', '4' => 0
// meaning role ID 3 should be indexed, but not 4,
// so pick out the ones to actually index
// Special case: if they chose Authenticated User, they want all users, but
// only non-blocked and skip user 0 (non-logged-in user placeholder).
$doall = $rolelist[\Drupal\Core\Session\AccountInterface::AUTHENTICATED_ROLE];
if ($doall) {
$res = \Drupal::database()
->select('users', 'u')
->fields('u', [
'uid',
'language',
])
->condition('status', '1')
->condition('uid', '0', '>')
->execute();
}
else {
$rids = array();
foreach ($rolelist as $key => $item) {
if ($item) {
$rids[] = $key;
}
}
if (!count($rids)) {
return array();
}
$query = \Drupal::database()
->select('users', 'u')
->fields('u', array(
'uid',
'language',
));
$query
->join('users_roles', 'ur', 'u.uid = ur.uid');
$query
->condition('u.status', 1)
->condition('ur.rid', $rids, 'IN');
$res = $query
->execute()
->fetchAll();
}
// Build an array of paths
$min_time = \Drupal::service('search_by_page.settings')
->getSetting('search_by_page_users_min_time', $environment, 1);
$max_time = \Drupal::service('search_by_page.settings')
->getSetting('search_by_page_users_max_time', $environment, 0);
$languageManager = new \Drupal\Core\Language\LanguageManager();
$langs = $languageManager
->getLanguages();
$langs = array_keys($langs);
$lang_opt = 'all';
if (count($langs) > 1) {
$lang_opt = \Drupal::service('search_by_page.settings')
->getSetting('search_by_page_users_language', $environment, 'all');
}
$role = \Drupal::service('search_by_page.settings')
->getSetting('search_by_page_users_role', $environment, AccountInterface::ANONYMOUS_ROLE);
$ret = array();
foreach ($res as $item) {
$stuff = array(
'id' => $item->uid,
'role' => $role,
'min_time' => $min_time,
'max_time' => $max_time,
);
if ($lang_opt == 'user') {
$stuff['languages'] = array(
$item->language,
);
}
else {
$stuff['languages'] = $langs;
}
$ret['user/' . $item->uid] = $stuff;
}
return $ret;
}
/**
* Implements Search by Page hook_search_by_page_query_modify().
*
* Adds an access permission check to the search query.
*/
function search_by_page_users_search_by_page_query_modify($environment, $query) {
// @TODO
// User profile access is simple yes/no wholesale permission.
/*$ok = \Drupal::currentUser()->hasPermission('access user profiles');
$cond = new \Drupal\Core\Database\Query\Condition('AND');
if ($ok) {
// If they can access users in general, join to user table to make
// sure user still exists and isn't blocked.
$query->leftJoin('users', 'sbpu_u', 'sbpu_u.uid = sp.modid');
$cond->condition('status', 1);
}
else {
$cond->where('0=1');
}
return $cond;*/
}
/**
* Implements Search by Page hook_search_by_page_details().
*
* Returns details for a particular user ID, for particular search keys.
*/
function search_by_page_users_search_by_page_details($id, $environment, $keys = NULL) {
$id = intval($id);
if (!$id) {
return NULL;
}
$user = Drupal\user\Entity\User::load($id);
if (!$user->name) {
return NULL;
}
// Get basic user info
$ret = array(
'type' => t('User'),
'title' => Html::escape($user->name),
'object' => $user,
);
// Get snippet from stored page data.
if ($keys) {
$content = search_by_page_stored_page_content('search_by_page_users', $id, $environment);
if ($content) {
$ret['snippet'] = \Drupal::service('search_by_page.settings')
->excerpt($keys, $content);
}
}
return $ret;
}
/**
* Implements Search by Page hook_search_by_page_settings().
*
* Adds a user role selection form to the Search by Page settings page.
*/
function search_by_page_users_search_by_page_settings($environment) {
$form = array();
$form['search_by_page_users'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#weight' => -20,
'#title' => t('Users'),
);
$form['search_by_page_users']['search_by_page_users_roles_indexed'] = array(
'#type' => 'checkboxes',
'#title' => t('User roles to index'),
'#default_value' => \Drupal::service('search_by_page.settings')
->getSetting('search_by_page_users_roles_indexed', $environment, array()),
'#options' => user_roles(TRUE),
'#weight' => 0,
'#description' => t('Choose which user roles you would like to have indexed by Search by Page. If you select "authenticated user", you will get all users registered on your site.'),
);
// Give language options if there is more than one language on the site.
$languageManager = new \Drupal\Core\Language\LanguageManager();
$langs = $languageManager
->getLanguages();
if (count($langs) > 1) {
$form['search_by_page_users']['search_by_page_users_language'] = array(
'#type' => 'radios',
'#title' => t('Language(s) to use when indexing user pages'),
'#default_value' => \Drupal::service('search_by_page.settings')
->getSetting('search_by_page_users_language', $environment, 'all'),
'#options' => array(
'all' => t('All available languages'),
'user' => t("Each profile in that user's preferred language only"),
),
'#weight' => 2,
'#description' => t("If you choose <em>All available languages</em>, each profile will be indexed several times, with the viewing language set to each available language in turn, which may result in either content, field labels, or both changing to the viewing language. If you choose to index the profile in the user's preferred language only, each page will be indexed only in one language. In either case, when a search is done, only content in the current language will be selected."),
);
}
$form['search_by_page_users']['search_by_page_users_role'] = array(
'#type' => 'radios',
'#title' => t('Role for indexing'),
'#options' => user_roles(),
'#default_value' => \Drupal::service('search_by_page.settings')
->getSetting('search_by_page_users_role', $environment, AccountInterface::ANONYMOUS_ROLE),
'#weight' => 4,
'#description' => t("When Search by Page indexes pages for searching, the pages 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_users']['search_by_page_users_min_time'] = array(
'#type' => 'select',
'#title' => t('Minimum reindexing time'),
'#options' => $times,
'#default_value' => \Drupal::service('search_by_page.settings')
->getSetting('search_by_page_users_min_time', $environment, 1),
'#weight' => 5,
'#description' => t("After indexing any new and updated user pages, Search by Page also cycles through previously-indexed user pages, in case the rendered view of the user page has changed (even though the user account information has not been edited, other information displayed on the page may have changed, such as points, ratings, a list of content the user has created, a Twitter feed, or a Content Profile). On some sites, you may want to limit the amount of reindexing, by setting a minimum time -- user pages will not be reindexed until this time has passed, unless the user account information has been updated."),
);
$form['search_by_page_users']['search_by_page_users_max_time'] = array(
'#type' => 'select',
'#title' => t('Maximum reindexing time'),
'#options' => $times,
'#default_value' => \Drupal::service('search_by_page.settings')
->getSetting('search_by_page_users_max_time', $environment, 0),
'#weight' => 6,
'#description' => t("Conversely to the minimum reindexing time (see above), Search by Page can be set to prioritize reindexing each user page (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.'),
);
return $form;
}
/**
* Implements hook_user_update().
*
* Mark the user path as "time to reindex".
*/
function search_by_page_users_user_update(&$edit, $account, $category) {
search_by_page_force_reindex('search_by_page_users', $account->uid);
}
/**
* Implements hook_user_delete().
*
* Remove from index.
*/
function search_by_page_users_user_delete($account) {
search_by_page_force_remove('search_by_page_users', $account->uid);
}
Functions
Name | Description |
---|---|
search_by_page_users_search_by_page_details | Implements Search by Page hook_search_by_page_details(). |
search_by_page_users_search_by_page_paths | Implements Search by Page hook_search_by_page_paths(). |
search_by_page_users_search_by_page_query_modify | Implements Search by Page hook_search_by_page_query_modify(). |
search_by_page_users_search_by_page_settings | Implements Search by Page hook_search_by_page_settings(). |
search_by_page_users_user_delete | Implements hook_user_delete(). |
search_by_page_users_user_update | Implements hook_user_update(). |