features_ui.admin.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * @file
  4. * Administration functions for features.module.
  5. */
  6. use Drupal\Core\Render\Element;
  7. Use \Drupal\Component\Render\FormattableMarkup;
  8. use Drupal\Component\Utility\Html;
  9. /**
  10. * Returns HTML for the features listing form.
  11. *
  12. * @param array $variables
  13. * An associative array containing:
  14. * - form: A render element representing the form.
  15. *
  16. * @ingroup themeable
  17. */
  18. function theme_features_listing(array $variables) {
  19. $form = $variables['form'];
  20. $renderer = \Drupal::service('renderer');
  21. // Individual table headers.
  22. $rows = array();
  23. // Iterate through all the features, which are children of this element.
  24. foreach (Element::children($form) as $key) {
  25. // Stick the key into $module for easier access.
  26. $element = $form[$key];
  27. // Create the row for the table.
  28. $row = array();
  29. // Add the checkbox into the first cell.
  30. unset($element['enable']['#title']);
  31. $row[] = array('class' => array('checkbox'), 'data' => $renderer->render($element['enable']));
  32. // Add the module label and expand/collapse functionalty.
  33. $id = Html::getUniqueId('feature-' . $key);
  34. $col2 = new FormattableMarkup('<label id="@id" for="@for" class="module-name table-filter-text-source">@name</label>',
  35. array(
  36. '@id' => $id,
  37. '@for' => $element['enable']['#id'],
  38. '@name' => $renderer->render($element['name']),
  39. )
  40. );
  41. $row[] = array('class' => array('module'), 'data' => $col2);
  42. $row[] = array('class' => array('machine_name'), 'data' => $renderer->render($element['machine_name']));
  43. $description = t('@details', array('@details' => $renderer->render($element['details'])));
  44. $details = array(
  45. '#type' => 'details',
  46. '#title' => new FormattableMarkup('<span class="text">@desc</span>', array( '@desc' => $renderer->render($element['description']))),
  47. '#attributes' => array('id' => $element['enable']['#id'] . '-description'),
  48. '#description' => $description,
  49. );
  50. $row[] = array(
  51. 'class' => array('description', 'expand'),
  52. 'data' => $renderer->render($details),
  53. );
  54. $row[] = array(
  55. 'class' => array('feature-version'),
  56. 'data' => $renderer->render($element['version']),
  57. );
  58. $row[] = array(
  59. 'class' => array('feature-state'),
  60. 'data' => $renderer->render($element['state']),
  61. );
  62. $rows[] = array('data' => $row);
  63. }
  64. $table = array(
  65. '#type' => 'tableselect',
  66. '#header' => $form['#header'],
  67. '#options' => $rows,
  68. '#empty' => t('No Features packages available.'),
  69. );
  70. return $renderer->render($table);
  71. }
  72. /**
  73. * Prepares variables for package assignment configuration form.
  74. *
  75. * @param array $variables
  76. * An associative array containing:
  77. * - form: A render element representing the form.
  78. */
  79. function template_preprocess_features_assignment_configure_form(&$variables) {
  80. $form =& $variables['form'];
  81. $header = array(
  82. t('Assignment method'),
  83. t('Description'),
  84. t('Enabled'),
  85. t('Weight'),
  86. );
  87. // If there is at least one operation enabled, show the operation column.
  88. if ($form['#show_operations']) {
  89. $header[] = t('Operations');
  90. }
  91. $table = array(
  92. '#type' => 'table',
  93. '#weight' => 5,
  94. '#header' => $header,
  95. '#attributes' => array('id' => 'features-assignment-methods'),
  96. '#tabledrag' => array(
  97. array(
  98. 'action' => 'order',
  99. 'relationship' => 'sibling',
  100. 'group' => 'assignment-method-weight',
  101. ),
  102. ),
  103. );
  104. foreach ($form['title'] as $id => $element) {
  105. // Do not take form control structures.
  106. if (is_array($element) && Element::child($id)) {
  107. $table[$id]['#attributes']['class'][] = 'draggable';
  108. $table[$id]['#weight'] = $element['#weight'];
  109. $table[$id]['title'] = array(
  110. '#prefix' => '<strong>',
  111. $form['title'][$id],
  112. '#suffix' => '</strong>',
  113. );
  114. $table[$id]['description'] = $form['description'][$id];
  115. $table[$id]['enabled'] = $form['enabled'][$id];
  116. $table[$id]['weight'] = $form['weight'][$id];
  117. if ($form['#show_operations']) {
  118. $table[$id]['operation'] = $form['operation'][$id];
  119. }
  120. // Unset to prevent rendering along with children.
  121. unset($form['title'][$id]);
  122. unset($form['description'][$id]);
  123. unset($form['enabled'][$id]);
  124. unset($form['weight'][$id]);
  125. unset($form['operation'][$id]);
  126. }
  127. }
  128. // For some reason, the #weight is not being handled by drupal_render.
  129. // So we remove the actions and then put them back into the form after the
  130. // table.
  131. $actions = $form['actions'];
  132. unset($form['actions']);
  133. $form['table'] = $table;
  134. $form['actions'] = $actions;
  135. }
  136. /**
  137. * Themes individual items in an item list.
  138. */
  139. function theme_features_items(array $variables) {
  140. $items = $variables['items'];
  141. $list = array();
  142. foreach ($items as $item) {
  143. $class = !empty($item['class']) ? $item['class'] : '';
  144. $list[] = '<span class="features-item ' . $class . '" title="' . $item['name'] . '">' . $item['label'] . '</span>';
  145. }
  146. return '<span class="features-item-list">' . implode(' ', $list) . '</span>';
  147. }
  148. /**
  149. * Themes the assignment form.
  150. */
  151. function theme_assignment_form(array $variables) {
  152. $renderer = \Drupal::service('renderer');
  153. return $renderer->render($variables['form']);
  154. }