language.admin.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * @file
  4. * Administration functions for language.module.
  5. */
  6. use Drupal\Core\Render\Element;
  7. use Drupal\Core\Template\Attribute;
  8. /**
  9. * Prepares variables for language negotiation configuration form.
  10. *
  11. * Default template: language-content-configuration-form.html.twig.
  12. *
  13. * @param array $variables
  14. * An associative array containing:
  15. * - form: A render element representing the form.
  16. */
  17. function template_preprocess_language_negotiation_configure_form(&$variables) {
  18. $form =& $variables['form'];
  19. $variables['language_types'] = [];
  20. foreach ($form['#language_types'] as $type) {
  21. $header = [
  22. t('Detection method'),
  23. t('Description'),
  24. t('Enabled'),
  25. t('Weight'),
  26. ];
  27. // If there is at least one operation enabled show the operation column.
  28. if ($form[$type]['#show_operations']) {
  29. $header[] = t('Operations');
  30. }
  31. $table = [
  32. '#type' => 'table',
  33. '#header' => $header,
  34. '#attributes' => ['id' => 'language-negotiation-methods-' . $type],
  35. '#tabledrag' => [
  36. [
  37. 'action' => 'order',
  38. 'relationship' => 'sibling',
  39. 'group' => 'language-method-weight-' . $type,
  40. ],
  41. ],
  42. ];
  43. foreach ($form[$type]['title'] as $id => $element) {
  44. // Do not take form control structures.
  45. if (is_array($element) && Element::child($id)) {
  46. $table[$id]['#attributes']['class'][] = 'draggable';
  47. $table[$id]['#weight'] = $element['#weight'];
  48. $table[$id]['title'] = [
  49. '#prefix' => '<strong>',
  50. $form[$type]['title'][$id],
  51. '#suffix' => '</strong>',
  52. ];
  53. $table[$id]['description'] = $form[$type]['description'][$id];
  54. $table[$id]['enabled'] = $form[$type]['enabled'][$id];
  55. $table[$id]['weight'] = $form[$type]['weight'][$id];
  56. if ($form[$type]['#show_operations']) {
  57. $table[$id]['operation'] = $form[$type]['operation'][$id];
  58. }
  59. // Unset to prevent rendering along with children.
  60. unset($form[$type]['title'][$id]);
  61. unset($form[$type]['description'][$id]);
  62. unset($form[$type]['enabled'][$id]);
  63. unset($form[$type]['weight'][$id]);
  64. unset($form[$type]['operation'][$id]);
  65. }
  66. }
  67. // Unset configurable to prevent rendering twice with children.
  68. $configurable = isset($form[$type]['configurable']) ? $form[$type]['configurable'] : NULL;
  69. unset($form[$type]['configurable']);
  70. $variables['language_types'][] = [
  71. 'type' => $type,
  72. 'title' => $form[$type]['#title'],
  73. 'description' => $form[$type]['#description'],
  74. 'configurable' => $configurable,
  75. 'table' => $table,
  76. 'children' => $form[$type],
  77. 'attributes' => new Attribute(),
  78. ];
  79. // Prevent the type from rendering with the remaining form child elements.
  80. unset($form[$type]);
  81. }
  82. $variables['children'] = $form;
  83. }
  84. /**
  85. * Prepares variables for language content settings table templates.
  86. *
  87. * Default template: language-content-settings-table.html.twig.
  88. *
  89. * @param array $variables
  90. * An associative array containing:
  91. * - element: An associative array containing the properties of the element.
  92. * Properties used: #bundle_label, #title.
  93. */
  94. function template_preprocess_language_content_settings_table(&$variables) {
  95. // Add a render element representing the bundle language settings table.
  96. $element = $variables['element'];
  97. $header = [
  98. [
  99. 'data' => $element['#bundle_label'],
  100. 'class' => ['bundle'],
  101. ],
  102. [
  103. 'data' => t('Configuration'),
  104. 'class' => ['operations'],
  105. ],
  106. ];
  107. $rows = [];
  108. foreach (Element::children($element) as $bundle) {
  109. $rows[$bundle] = [
  110. 'data' => [
  111. [
  112. 'data' => [
  113. '#prefix' => '<label>',
  114. '#suffix' => '</label>',
  115. '#plain_text' => $element[$bundle]['settings']['#label'],
  116. ],
  117. 'class' => ['bundle'],
  118. ],
  119. [
  120. 'data' => $element[$bundle]['settings'],
  121. 'class' => ['operations'],
  122. ],
  123. ],
  124. 'class' => ['bundle-settings'],
  125. ];
  126. }
  127. $variables['title'] = $element['#title'];
  128. $variables['build'] = [
  129. '#header' => $header,
  130. '#rows' => $rows,
  131. '#type' => 'table',
  132. ];
  133. }