features.image.inc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 (array_keys($default_styles) as $default_style) {
  72. if ($style = image_style_load($default_style)) {
  73. if ($style['storage'] != IMAGE_STORAGE_DEFAULT) {
  74. image_default_style_revert($style);
  75. }
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * Remove unnecessary keys for export.
  82. */
  83. function _image_features_style_sanitize(array &$style) {
  84. // Sanitize style: Don't export numeric IDs and things which get overwritten
  85. // in image_styles() or are code/storage specific. The name property will be
  86. // the key of the exported $style array.
  87. $style = array_diff_key($style, array_flip(array(
  88. 'isid',
  89. 'name',
  90. 'module',
  91. 'storage',
  92. )));
  93. // Sanitize effects: all that needs to be kept is name, weight and data,
  94. // which holds all the style-specific configuration. Other keys are assumed
  95. // to belong to the definition of the effect itself, so not configuration.
  96. foreach ($style['effects'] as $id => $effect) {
  97. $style['effects'][$id] = array_intersect_key($effect, array_flip(array(
  98. 'name',
  99. 'data',
  100. 'weight',
  101. )));
  102. }
  103. }