image_styles_admin.module 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * @file Hook and callback implementations that must be available at all times.
  4. */
  5. /**
  6. * Implements hook_menu().
  7. */
  8. function image_styles_admin_menu() {
  9. $items = array();
  10. $items['admin/config/media/image-styles/duplicate/%image_style'] = array(
  11. 'title' => 'Duplicate style',
  12. 'description' => 'Make a copy of an image style.',
  13. 'page callback' => 'image_styles_admin_duplicate_page_callback',
  14. 'page arguments' => array(5),
  15. 'access arguments' => array('administer image styles'),
  16. 'file' => 'image_styles_admin.inc',
  17. );
  18. $items['admin/config/media/image-styles/export/%image_style'] = array(
  19. 'title' => 'Export style',
  20. 'description' => 'Export an image style.',
  21. 'page callback' => 'drupal_get_form',
  22. 'page arguments' => array('image_styles_admin_export_form', 5),
  23. 'access arguments' => array('administer image styles'),
  24. 'file' => 'image_styles_admin.inc',
  25. );
  26. $items['admin/config/media/image-styles/import'] = array(
  27. 'title' => 'Import style',
  28. 'description' => 'Import an image style.',
  29. 'page callback' => 'drupal_get_form',
  30. 'page arguments' => array('image_styles_admin_import_form'),
  31. 'access arguments' => array('administer image styles'),
  32. 'type' => MENU_LOCAL_ACTION,
  33. 'weight' => 3,
  34. 'file' => 'image_styles_admin.inc',
  35. );
  36. return $items;
  37. }
  38. /**
  39. * Implements hook_preprocess_HOOK for theme image_style_list.
  40. */
  41. function image_styles_admin_preprocess_image_style_list(&$variables) {
  42. // Sort the image styles by name.
  43. uasort($variables['styles'], function ($a, $b) {
  44. return strcasecmp($a['label'], $b['label']);
  45. });
  46. // Tell imagecache_actions_preprocess_table to preprocess the next call to
  47. // theme_table().
  48. $image_styles = array_values($variables['styles']);
  49. image_styles_admin_preprocess_table($image_styles);
  50. // Add CSS and JS files.
  51. drupal_add_css(drupal_get_path('module', 'image_styles_admin') . '/image_styles_admin.css');
  52. if (base_path() !== '/') {
  53. $base_path = base_path();
  54. drupal_add_css("
  55. #image-styles .expand.inner { background-image: url($base_path/misc/menu-collapsed.png) }
  56. #image-styles .expanded.expand.inner { background-image: url($base_path/misc/menu-expanded.png) }",
  57. array('type' => 'inline'));
  58. }
  59. drupal_add_js(drupal_get_path('module', 'image_styles_admin') . '/image_styles_admin.js');
  60. }
  61. /**
  62. * Implements hook_preprocess_HOOK for theme table.
  63. */
  64. function image_styles_admin_preprocess_table(&$variables) {
  65. static $image_styles = NULL;
  66. // If called from image_styles_admin_preprocess_image_style_list(), the
  67. // parameter will be a sequential array.
  68. if (key($variables) === 0) {
  69. $image_styles = $variables;
  70. }
  71. else if (!empty($image_styles)) {
  72. // Normal preprocess hook call: we only process if theme('table', ...) has
  73. // been called via theme_image_style_list() and we have a non empty list of
  74. // styles;
  75. // Set an ID on the table so it can be targeted by our CSS.
  76. $variables['attributes']['id'] = 'image-styles';
  77. // Add a class to the Style name and Settings columns for styling.
  78. foreach ($variables['header'] as &$cell) {
  79. $temp_cell = is_string($cell) ? array('data' => $cell) : $cell;
  80. $class_names = array(
  81. 'style-name' => t('Style name'),
  82. 'settings' => t('Settings'),
  83. );
  84. foreach ($class_names as $class => $name) {
  85. if ($temp_cell['data'] == $name) {
  86. $temp_cell['class'][] = $class;
  87. $cell = $temp_cell;
  88. }
  89. }
  90. }
  91. // Add the effects column header.
  92. array_splice($variables['header'], 1, 0, array(array(
  93. 'data' => t('Effects') . ' <span class="description expand" role="button">(' . t('expand all') . ')</span>',
  94. 'class' => array('effects expand-all')
  95. )));
  96. // Add a column with a summary of all effects to each row.
  97. foreach ($variables['rows'] as $i => &$row) {
  98. $style = $image_styles[$i];
  99. $effects_list = array();
  100. foreach ($style['effects'] as $key => $effect) {
  101. $definition = image_effect_definition_load($effect['name']);
  102. $effect = array_merge($definition, $effect);
  103. $style['effects'][$key] = $effect;
  104. $effect_details = isset($effect['summary theme']) ? theme($effect['summary theme'], array('data' => $effect['data'])) : '';
  105. $effects_list[] = '<span class="details">' . $effect['label'] . ' ' . $effect_details . '</span>';
  106. }
  107. // Add the effects summary column to the row.
  108. $effects_summary = array(
  109. 'data' => '<div class="inner expand" role="button">' . implode('<span class="separator">, </span>', $effects_list) . '</div>',
  110. 'class' => 'description'
  111. );
  112. array_splice($row, 1, 0, array($effects_summary));
  113. }
  114. // Find the column with the edit link in it.
  115. $i = 0;
  116. $first_row = reset($variables['rows']);
  117. foreach ($first_row as $i => &$cell) {
  118. $cell_data = is_array($cell) ? $cell['data'] : $cell;
  119. if (strpos($cell_data, '>' . t('edit') . '<') !== FALSE) {
  120. break;
  121. }
  122. }
  123. // Increase the colspan for the column with the edit link to include the
  124. // duplicate and export links as well. This *should* be 2, but Drupal core
  125. // specifies 1 more than should be there.
  126. $variables['header'][$i]['colspan'] += 1;
  127. // Add the 2 links to each row by duplicating the edit link and then
  128. // changing the text and the link.
  129. $edit_column = $i;
  130. foreach ($variables['rows'] as &$row) {
  131. $i = $edit_column;
  132. // Duplicate the edit link twice.
  133. array_splice($row, $i + 1, 0, array($row[$i], $row[$i]));
  134. // Replace edit with duplicate in text and href
  135. $i++;
  136. $row[$i] = str_replace('>' . t('edit') . '<', '>' . t('duplicate') . '<', $row[$i]);
  137. $row[$i] = preg_replace('#/admin/config/media/image-styles/edit/#', '/admin/config/media/image-styles/duplicate/', $row[$i]);
  138. // Replace edit with export in text and href
  139. $i++;
  140. $row[$i] = str_replace('>' . t('edit') . '<', '>' . t('export') . '<', $row[$i]);
  141. $row[$i] = preg_replace('#/admin/config/media/image-styles/edit/#', '/admin/config/media/image-styles/export/', $row[$i]);
  142. }
  143. // Don't preprocess subsequent calls to theme_table().
  144. $image_styles = NULL;
  145. }
  146. }