fontyourface.features.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @file
  4. * Integrates Features for @font-your-face.
  5. */
  6. /**
  7. * Implements hook_features_export_options. [component_hook]
  8. *
  9. * This hook will alert features of which specific items of this component may
  10. * be exported. For instances, in this case, we want to make available all the
  11. * existing items. If there are no items to be exported, this component will
  12. * not be made available in the features export page.
  13. *
  14. * @return array
  15. * A keyed array of items, suitable for use with a FormAPI select or
  16. * checkboxes element.
  17. */
  18. function fontyourface_features_export_options() {
  19. $fonts = array();
  20. foreach (fontyourface_get_fonts('enabled = 1') as $font) {
  21. $fonts[$font->name] = $font->name;
  22. }
  23. return $fonts;
  24. }
  25. /**
  26. * Implements hook_features_export [component hook]
  27. *
  28. * This is a component hook, rather then a module hook, therefore this is the
  29. * callback from hook_features_api which relates to the specific component we
  30. * are looking to export. When a specific instance of the component we are
  31. * looking to export is selected, this will include the necessariy item, plus
  32. * any dependencies into our export array.
  33. *
  34. * @param array $data
  35. * this is the machine name for the component in question
  36. * @param array &$export
  37. * array of all components to be exported
  38. * @param string $module_name
  39. * The name of the feature module to be generated.
  40. * @return array
  41. * The pipe array of further processors that should be called
  42. */
  43. function fontyourface_features_export($data, &$export, $module_name = '') {
  44. // fontyourface_default_fonts integration is provided by Features.
  45. $export['dependencies']['features'] = 'features';
  46. $export['dependencies']['fontyourface'] = 'fontyourface';
  47. // Add dependencies for each font.
  48. $fonts = fontyourface_get_fonts('enabled = 1');
  49. foreach ($fonts as $font) {
  50. if (in_array($font->name, $data)) {
  51. // Make the font provider required
  52. $export['dependencies'][$font->provider] = $font->provider;
  53. $export['features']['fontyourface'][$font->name] = $font->name;
  54. }
  55. }
  56. return $export;
  57. }
  58. /**
  59. * Implements hook_features_export_render. [component hook]
  60. *
  61. * This hook will be invoked in order to export
  62. * Component hook. The hook should be implemented using the name ot the
  63. * component, not the module, eg. [component]_features_export() rather than
  64. * [module]_features_export().
  65. *
  66. * Render one or more component objects to code.
  67. *
  68. * @param string $module_name
  69. * The name of the feature module to be exported.
  70. * @param array $data
  71. * An array of machine name identifiers for the objects to be rendered.
  72. * @param array $export
  73. * The full export array of the current feature being exported. This is only
  74. * passed when hook_features_export_render() is invoked for an actual feature
  75. * update or recreate, not during state checks or other operations.
  76. * @return array
  77. * An associative array of rendered PHP code where the key is the name of the
  78. * hook that should wrap the PHP code. The hook should not include the name
  79. * of the module, e.g. the key for `hook_example` should simply be `example`.
  80. */
  81. function fontyourface_features_export_render($module, $data) {
  82. $fonts = fontyourface_get_fonts('enabled = 1');
  83. $code = array();
  84. foreach ($data as $name) {
  85. foreach ($fonts as $font) {
  86. if ($font->name == $name) {
  87. // We don't want to break the entity cache, so we need to clone the
  88. // font before unsetting the id.
  89. $font = clone $font;
  90. unset($font->fid);
  91. unset($font->tags);
  92. $code[$name] = $font;
  93. }
  94. }
  95. }
  96. $code = " return " . features_var_export($code, ' ') . ";";
  97. return array('fontyourface_features_default_font' => $code);
  98. }
  99. /**
  100. * Implements hook_features_revert().
  101. */
  102. function fontyourface_features_revert($module) {
  103. fontyourface_features_rebuild($module);
  104. }
  105. /**
  106. * Implements hook_features_rebuild().
  107. *
  108. * Rebuilds @font-your-face fonts from code defaults.
  109. */
  110. function fontyourface_features_rebuild($module) {
  111. $saved_fonts = module_invoke($module, 'fontyourface_features_default_font');
  112. foreach ($saved_fonts as $key => $font) {
  113. $font = (object) $font;
  114. $font->tags = array();
  115. $saved = fontyourface_save_font($font, TRUE);
  116. }
  117. }