features.image.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Implements hook_features_api().
  4. */
  5. function image_features_api() {
  6. return array(
  7. 'image' => array(
  8. 'name' => t('Image styles'),
  9. 'feature_source' => TRUE,
  10. 'default_hook' => 'image_default_styles',
  11. 'alter_hook' => 'image_styles',
  12. )
  13. );
  14. }
  15. /**
  16. * Implements hook_features_export_options().
  17. */
  18. function image_features_export_options() {
  19. $options = array();
  20. foreach (image_styles() as $name => $style) {
  21. $options[$name] = $style['name'];
  22. }
  23. return $options;
  24. }
  25. /**
  26. * Implements hook_features_export().
  27. */
  28. function image_features_export($data, &$export, $module_name = '') {
  29. $pipe = array();
  30. $map = features_get_default_map('image');
  31. foreach ($data as $style) {
  32. $export['dependencies']['image'] = 'image';
  33. // If another module provides this style, add it as a dependency
  34. if (isset($map[$style]) && $map[$style] != $module_name) {
  35. $module = $map[$style];
  36. $export['dependencies'][$module] = $module;
  37. }
  38. // Otherwise, export the style
  39. elseif (image_style_load($style)) {
  40. $export['features']['image'][$style] = $style;
  41. }
  42. }
  43. return $pipe;
  44. }
  45. /**
  46. * Implements hook_features_export_render().
  47. */
  48. function image_features_export_render($module_name, $data, $export = NULL) {
  49. $code = array();
  50. $code[] = ' $styles = array();';
  51. $code[] = '';
  52. foreach ($data as $name) {
  53. if ($style = image_style_load($name)) {
  54. _image_features_style_sanitize($style);
  55. $style_export = features_var_export($style, ' ');
  56. $style_identifier = features_var_export($name);
  57. $code[] = " // Exported image style: {$name}.";
  58. $code[] = " \$styles[{$style_identifier}] = {$style_export};";
  59. $code[] = "";
  60. }
  61. }
  62. $code[] = ' return $styles;';
  63. $code = implode("\n", $code);
  64. return array('image_default_styles' => $code);
  65. }
  66. /**
  67. * Implements hook_features_revert().
  68. */
  69. function image_features_revert($module) {
  70. if ($default_styles = features_get_default('image', $module)) {
  71. foreach ($default_styles as $default_style_name => $default_style) {
  72. if ($style = image_style_load($default_style_name)) {
  73. if ($style['storage'] != IMAGE_STORAGE_DEFAULT) {
  74. image_default_style_revert($style);
  75. }
  76. else {
  77. // Verify that the loaded style still matches what's in code.
  78. if ($default_style['effects'] !== $style['effects']) {
  79. image_default_style_revert($style);
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. /**
  87. * Remove unnecessary keys for export.
  88. */
  89. function _image_features_style_sanitize(array &$style) {
  90. // Sanitize style: Don't export numeric IDs and things which get overwritten
  91. // in image_styles() or are code/storage specific. The name property will be
  92. // the key of the exported $style array.
  93. $style = array_diff_key($style, array_flip(array(
  94. 'isid',
  95. 'name',
  96. 'module',
  97. 'storage',
  98. )));
  99. // Sanitize effects: all that needs to be kept is name, weight and data,
  100. // which holds all the style-specific configuration. Other keys are assumed
  101. // to belong to the definition of the effect itself, so not configuration.
  102. foreach ($style['effects'] as $id => $effect) {
  103. $style['effects'][$id] = array_intersect_key($effect, array_flip(array(
  104. 'name',
  105. 'data',
  106. 'weight',
  107. )));
  108. }
  109. }