features.image.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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(&$style, $child = FALSE) {
  84. $omit = $child ? array('isid', 'ieid', 'storage') : array('isid', 'ieid', 'storage', 'module');
  85. if (is_array($style)) {
  86. foreach ($style as $k => $v) {
  87. if (in_array($k, $omit, TRUE)) {
  88. unset($style[$k]);
  89. }
  90. else if (is_array($v)) {
  91. _image_features_style_sanitize($style[$k], TRUE);
  92. }
  93. }
  94. }
  95. }