search_api.theme.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <?php
  2. /**
  3. * @file
  4. * Defines theme functions for the Search API module.
  5. */
  6. use Drupal\Component\Render\FormattableMarkup;
  7. use Drupal\Component\Utility\Html;
  8. use Drupal\Core\Render\Element;
  9. use Drupal\Core\Url;
  10. use Drupal\search_api\Query\QueryInterface;
  11. use Drupal\search_api\SearchApiException;
  12. use Drupal\search_api\Utility\Utility;
  13. /**
  14. * Returns HTML for a fields form table.
  15. *
  16. * @param array $variables
  17. * An associative array containing:
  18. * - element: A render element representing the form.
  19. *
  20. * @return string
  21. * The rendered HTML for a fields form table.
  22. *
  23. * @ingroup themeable
  24. */
  25. function theme_search_api_admin_fields_table(array $variables) {
  26. $form = $variables['element'];
  27. $rows = [];
  28. if (!empty($form['fields'])) {
  29. foreach (Element::children($form['fields']) as $name) {
  30. $row = [];
  31. foreach (Element::children($form['fields'][$name]) as $field) {
  32. if ($cell = render($form['fields'][$name][$field])) {
  33. $row[] = $cell;
  34. }
  35. }
  36. $row = [
  37. 'data' => $row,
  38. 'data-field-row-id' => $name,
  39. ];
  40. if (!empty($form['fields'][$name]['description']['#value'])) {
  41. $row['title'] = strip_tags($form['fields'][$name]['description']['#value']);
  42. }
  43. $rows[] = $row;
  44. }
  45. }
  46. $note = isset($form['note']) ? $form['note'] : '';
  47. unset($form['note'], $form['submit']);
  48. $output = '';
  49. foreach (Element::children($form) as $key) {
  50. if (!empty($form[$key])) {
  51. $output .= render($form[$key]);
  52. }
  53. }
  54. $build = [
  55. '#theme' => 'table',
  56. '#header' => $form['#header'],
  57. '#rows' => $rows,
  58. '#empty' => t('No fields have been added for this datasource.'),
  59. ];
  60. $output .= render($build);
  61. $output .= render($note);
  62. return $output;
  63. }
  64. /**
  65. * Returns HTML for the data type overview table.
  66. *
  67. * @param array $variables
  68. * An associative array containing:
  69. * - data_types: An associative array of data types, keyed by data type ID and
  70. * containing associative arrays with information about the data type:
  71. * - label: The (translated) human-readable label for the data type.
  72. * - description: The (translated) description of the data type.
  73. * - fallback: The type ID of the fallback type.
  74. * - fallback_mapping: array of fallback data types for unsupported data
  75. * types.
  76. *
  77. * @return string
  78. * The rendered HTML for a fields form table.
  79. *
  80. * @ingroup themeable
  81. */
  82. function theme_search_api_admin_data_type_table(array $variables) {
  83. $data_types = $variables['data_types'];
  84. $fallback_mapping = $variables['fallback_mapping'];
  85. $header = [
  86. t('Data Type'),
  87. t('Description'),
  88. t('Supported'),
  89. ];
  90. // Only show the column with fallback types if there is actually an
  91. // unsupported type listed.
  92. if ($fallback_mapping) {
  93. $header[] = t('Fallback data type');
  94. }
  95. $rows = [];
  96. $yes = t('Yes');
  97. $yes_img = 'core/misc/icons/73b355/check.svg';
  98. $no = t('No');
  99. $no_img = 'core/misc/icons/e32700/error.svg';
  100. foreach ($data_types as $data_type_id => $data_type) {
  101. $has_fallback = isset($fallback_mapping[$data_type_id]);
  102. $supported_label = $has_fallback ? $no : $yes;
  103. $supported_icon = [
  104. '#theme' => 'image',
  105. '#uri' => $has_fallback ? $no_img : $yes_img,
  106. '#width' => 18,
  107. '#height' => 18,
  108. '#alt' => $supported_label,
  109. '#title' => $supported_label,
  110. ];
  111. $row = [
  112. $data_type['label'],
  113. $data_type['description'],
  114. ['data' => $supported_icon],
  115. ];
  116. if ($fallback_mapping) {
  117. $row[] = $has_fallback ? $data_types[$data_type['fallback']]['label'] : '';
  118. }
  119. $rows[] = $row;
  120. }
  121. $build = [
  122. '#theme' => 'table',
  123. '#header' => $header,
  124. '#rows' => $rows,
  125. ];
  126. return render($build);
  127. }
  128. /**
  129. * Returns HTML for a list of form items.
  130. *
  131. * Wrapper around the "item_list" theme which uses the child elements as the
  132. * list items instead of the "#items" key.
  133. *
  134. * @param array $variables
  135. * An associative array containing a single value, "element", containing the
  136. * element to be rendered.
  137. *
  138. * @return string
  139. * The rendered HTML for the list.
  140. *
  141. * @ingroup themeable
  142. */
  143. function theme_search_api_form_item_list(array $variables) {
  144. $element = $variables['element'];
  145. $build = [
  146. '#theme' => 'item_list',
  147. ];
  148. if (!empty($element['#title'])) {
  149. $build['#title'] = $element['#title'];
  150. }
  151. foreach (Element::children($element) as $key) {
  152. $build['#items'][$key] = $element[$key];
  153. }
  154. return render($build);
  155. }
  156. /**
  157. * Returns HTML for a search server.
  158. *
  159. * @param array $variables
  160. * An associative array containing:
  161. * - server: The server that should be displayed.
  162. *
  163. * @return string
  164. * The rendered HTML for a search server.
  165. *
  166. * @ingroup themeable
  167. */
  168. function theme_search_api_server(array $variables) {
  169. // Get the search server.
  170. /** @var \Drupal\search_api\ServerInterface $server */
  171. $server = $variables['server'];
  172. $output = '';
  173. if (($description = $server->getDescription())) {
  174. // Sanitize the description and append to the output.
  175. $output .= '<p class="description">' . nl2br(Html::escape($description)) . '</p>';
  176. }
  177. // Initialize the $rows variable which will hold the different parts of server
  178. // information.
  179. $rows = [];
  180. // Create a row template with references so we don't have to deal with the
  181. // complicated structure for each individual row.
  182. $row = [
  183. 'data' => [
  184. ['header' => TRUE],
  185. '',
  186. ],
  187. 'class' => [''],
  188. ];
  189. // Get the individual parts of the row by reference.
  190. $label = &$row['data'][0]['data'];
  191. $info = &$row['data'][1];
  192. $classes = &$row['class'];
  193. // Check if the server is enabled.
  194. if ($server->status()) {
  195. $classes[] = 'ok';
  196. $info = t('enabled (<a href=":url">disable</a>)', [':url' => $server->toUrl('disable')->toString()]);
  197. }
  198. else {
  199. $classes[] = 'warning';
  200. $info = t('disabled (<a href=":url">enable</a>)', [':url' => $server->toUrl('enable')->toString()]);
  201. }
  202. // Append the row and reset variables.
  203. $label = t('Status');
  204. $classes[] = 'search-api-server-summary--status';
  205. $rows[] = Utility::deepCopy($row);
  206. $classes = [];
  207. // Check if the backend used by the server is valid and get its label.
  208. if ($server->hasValidBackend()) {
  209. $backend = $server->getBackend();
  210. $info = Html::escape($backend->label());
  211. }
  212. else {
  213. $classes[] = 'error';
  214. $info = t('Invalid or missing backend plugin: %backend_id', ['%backend_id' => $server->getBackendId()]);
  215. }
  216. // Append the row and reset variables.
  217. $label = t('Backend class');
  218. $classes[] = 'search-api-server-summary--backend';
  219. $rows[] = Utility::deepCopy($row);
  220. $classes = [];
  221. // Build the indexes links container.
  222. $indexes = [
  223. '#theme' => 'links',
  224. '#attributes' => ['class' => ['inline']],
  225. '#links' => [],
  226. ];
  227. // Add links for all indexes attached to this server.
  228. foreach ($server->getIndexes() as $index) {
  229. $indexes['#links'][] = [
  230. 'title' => $index->label(),
  231. 'url' => $index->toUrl('canonical'),
  232. ];
  233. }
  234. // Check if the indexes variable contains links.
  235. if ($indexes['#links']) {
  236. $label = t('Search indexes');
  237. $info = render($indexes);
  238. $classes[] = 'search-api-server-summary--indexes';
  239. $rows[] = Utility::deepCopy($row);
  240. $classes = [];
  241. }
  242. // Add backend-specific additional information.
  243. foreach ($server->viewSettings() as $information) {
  244. // Convert the extra information and append the information to the row.
  245. $label = $information['label'];
  246. $info = $information['info'];
  247. if (!empty($information['status'])) {
  248. $classes[] = $information['status'];
  249. }
  250. $rows[] = Utility::deepCopy($row);
  251. $classes = [];
  252. }
  253. // Append the server info table to the output.
  254. $server_info_table = [
  255. '#theme' => 'table',
  256. '#rows' => $rows,
  257. '#attributes' => [
  258. 'class' => [
  259. 'search-api-server-summary',
  260. ],
  261. ],
  262. ];
  263. $output .= render($server_info_table);
  264. return $output;
  265. }
  266. /**
  267. * Implements hook_preprocess_search_api_index().
  268. */
  269. function search_api_preprocess_search_api_index(array &$variables) {
  270. /** @var \Drupal\search_api\IndexInterface $index */
  271. $index = $variables['index'];
  272. if ($index->status()) {
  273. try {
  274. $variables['server_count'] = $index->query()
  275. ->setProcessingLevel(QueryInterface::PROCESSING_NONE)
  276. ->addTag('server_index_status')
  277. ->range(0, 0)
  278. ->execute()
  279. ->getResultCount();
  280. }
  281. catch (SearchApiException $e) {
  282. $variables['server_count_error'] = $e->getMessage();
  283. }
  284. }
  285. }
  286. /**
  287. * Returns HTML for a search index.
  288. *
  289. * @param array $variables
  290. * An associative array containing:
  291. * - index: The search index to display.
  292. * - server_count: The count of items on the server for this index.
  293. * - server_count_error: If set, the error that occurred while trying to get
  294. * the server items count.
  295. *
  296. * @return string
  297. * The rendered HTML for a search index.
  298. *
  299. * @ingroup themeable
  300. */
  301. function theme_search_api_index(array $variables) {
  302. // Get the index.
  303. /** @var \Drupal\search_api\IndexInterface $index */
  304. $index = $variables['index'];
  305. $server = $index->hasValidServer() ? $index->getServerInstance() : NULL;
  306. $tracker = $index->hasValidTracker() ? $index->getTrackerInstance() : NULL;
  307. $output = '';
  308. if (($description = $index->getDescription())) {
  309. // Sanitize the description and append to the output.
  310. $output .= '<p class="description">' . nl2br(Html::escape($description)) . '</p>';
  311. }
  312. // Initialize the $rows variable which will hold the different parts of server
  313. // information.
  314. $rows = [];
  315. // Create a row template with references so we don't have to deal with the
  316. // complicated structure for each individual row.
  317. $row = [
  318. 'data' => [
  319. ['header' => TRUE],
  320. '',
  321. ],
  322. 'class' => [],
  323. ];
  324. // Get the individual parts of the row by reference.
  325. $label = &$row['data'][0]['data'];
  326. $info = &$row['data'][1];
  327. $classes = &$row['class'];
  328. // Check if the index is enabled.
  329. if ($index->status()) {
  330. $classes[] = 'ok';
  331. $info = t('enabled (<a href=":url">disable</a>)', [':url' => $index->toUrl('disable')->toString()]);
  332. }
  333. // Check if a server is available and enabled.
  334. elseif ($server && $server->status()) {
  335. $classes[] = 'warning';
  336. $info = t('disabled (<a href=":url">enable</a>)', [':url' => $index->toUrl('enable')->toString()]);
  337. }
  338. else {
  339. $classes[] = 'warning';
  340. $info = t('disabled');
  341. }
  342. // Append the row and reset variables.
  343. $label = t('Status');
  344. $classes[] = 'search-api-index-summary--status';
  345. $rows[] = Utility::deepCopy($row);
  346. $classes = [];
  347. foreach ($index->getDatasourceIds() as $datasource_id) {
  348. // Check if the datasource is valid.
  349. if ($index->isValidDatasource($datasource_id)) {
  350. $info = $index->getDatasource($datasource_id)->label();
  351. if ($tracker) {
  352. $args = [
  353. '@indexed' => $tracker->getIndexedItemsCount($datasource_id),
  354. '@total' => $tracker->getTotalItemsCount($datasource_id),
  355. ];
  356. $indexed = t('@indexed/@total indexed', $args);
  357. $args = [
  358. '@datasource' => $info,
  359. '@indexed' => $indexed,
  360. ];
  361. $info = new FormattableMarkup('@datasource <small>(@indexed)</small>', $args);
  362. }
  363. }
  364. else {
  365. $classes[] = 'error';
  366. $info = t('Invalid or missing datasource plugin: %datasource_id', ['%datasource_id' => $datasource_id]);
  367. }
  368. // Append the row and reset variables.
  369. $label = t('Datasource');
  370. $classes[] = 'search-api-index-summary--datasource';
  371. $rows[] = Utility::deepCopy($row);
  372. $classes = [];
  373. }
  374. // Check if the tracker is valid.
  375. if ($tracker) {
  376. $info = $tracker->label();
  377. }
  378. else {
  379. $classes[] = 'error';
  380. $info = t('Invalid or missing tracker plugin: %tracker_id', ['%tracker_id' => $index->getTrackerId()]);
  381. }
  382. // Append the row and reset variables.
  383. $label = t('Tracker');
  384. $classes[] = 'search-api-index-summary--tracker';
  385. $rows[] = Utility::deepCopy($row);
  386. $classes = [];
  387. // Check if a server is available.
  388. $classes[] = 'search-api-index-summary--server';
  389. if ($server) {
  390. $label = t('Server');
  391. $info = $server->toLink(NULL, 'canonical')->toString();
  392. $rows[] = Utility::deepCopy($row);
  393. }
  394. elseif ($index->getServerId()) {
  395. $classes[] = 'error';
  396. $label = t('Server');
  397. $info = t('Unknown server set for index: %server_id', ['%server_id' => $index->getServerId()]);
  398. $rows[] = Utility::deepCopy($row);
  399. }
  400. $classes = [];
  401. // Check if the index is enabled.
  402. if ($index->status()) {
  403. $label = t('Server index status');
  404. if (isset($variables['server_count'])) {
  405. $vars = [':url' => Url::fromUri('https://drupal.org/node/2009804#server-index-status')->toString()];
  406. // Build the server index status info.
  407. $info = \Drupal::translation()->formatPlural($variables['server_count'], 'There is 1 item indexed on the server for this index. (<a href=":url">More information</a>)', 'There are @count items indexed on the server for this index. (<a href=":url">More information</a>)', $vars);
  408. }
  409. else {
  410. $args = ['@message' => $variables['server_count_error']];
  411. $info = t('Error while checking server index status: @message', $args);
  412. $classes[] = 'error';
  413. }
  414. $classes[] = 'search-api-index-summary--server-index-status';
  415. $rows[] = Utility::deepCopy($row);
  416. $classes = [];
  417. $cron_limit = $index->getOption('cron_limit', \Drupal::config('search_api.settings')->get('default_cron_limit'));
  418. // Check if the cron limit is higher than zero.
  419. if ($cron_limit != 0) {
  420. $classes[] = 'ok';
  421. if ($cron_limit > 0) {
  422. $info = \Drupal::translation()->formatPlural($cron_limit, 'During cron runs, 1 item will be indexed per batch.', 'During cron runs, @count items will be indexed per batch.');
  423. }
  424. else {
  425. $info = t('All items will be indexed at once during cron runs.');
  426. }
  427. }
  428. else {
  429. $classes[] = 'warning';
  430. $info = t('No items will be indexed during cron runs.');
  431. }
  432. // Append the row and reset variables.
  433. $label = t('Cron batch size');
  434. $classes[] = 'search-api-index-summary--cron-batch-size';
  435. $rows[] = Utility::deepCopy($row);
  436. $classes = [];
  437. // Add the indexing progress bar.
  438. if ($tracker) {
  439. $indexed_count = $tracker->getIndexedItemsCount();
  440. $total_count = $tracker->getTotalItemsCount();
  441. $index_progress = [
  442. '#theme' => 'progress_bar',
  443. '#percent' => $total_count ? (int) (100 * $indexed_count / $total_count) : 100,
  444. '#message' => t('@indexed/@total indexed', ['@indexed' => $indexed_count, '@total' => $total_count]),
  445. ];
  446. $output .= '<h3>' . t('Index status') . '</h3>';
  447. $output .= '<div class="search-api-index-status">' . render($index_progress) . '</div>';
  448. }
  449. }
  450. // Append the index info table to the output.
  451. $index_info_table = [
  452. '#theme' => 'table',
  453. '#rows' => $rows,
  454. '#attributes' => [
  455. 'class' => [
  456. 'search-api-index-summary',
  457. ],
  458. ],
  459. ];
  460. $output .= render($index_info_table);
  461. return $output;
  462. }