php.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * @file
  4. * Token engine for Custom Formatters modules.
  5. */
  6. /**
  7. *
  8. */
  9. function custom_formatters_engine_php_theme_alter(&$theme) {
  10. $theme['custom_formatters_php_export'] = array(
  11. 'variables' => array(
  12. 'item' => NULL,
  13. 'module' => NULL,
  14. ),
  15. 'template' => 'php.export',
  16. 'path' => drupal_get_path('module', 'custom_formatters') . '/engines',
  17. );
  18. }
  19. /**
  20. * Settings form callback for Custom Formatters PHP engine.
  21. */
  22. function custom_formatters_engine_php_settings_form(&$form) {
  23. $form['code']['#description'] = t('Enter the PHP code that will be evaluated. You should NOT include %php tags. The $variables object is available.', array('%php' => '<?php ?>'));
  24. $form['code']['#attributes']['class'][] = 'syntax-php';
  25. $form['settings'] = array(
  26. '#type' => 'container',
  27. );
  28. // Additional debugging modes.
  29. $form['preview']['options']['dpm'] = array(
  30. '#type' => 'container',
  31. );
  32. $form['preview']['options']['dpm']['vars'] = array(
  33. '#type' => 'checkbox',
  34. '#title' => t('Output <strong>$variables</strong> array (requires <a href="http://drupal.org/project/devel">Devel</a> module).'),
  35. '#default_value' => module_exists('devel'),
  36. '#disabled' => !module_exists('devel'),
  37. );
  38. $form['preview']['options']['dpm']['html'] = array(
  39. '#type' => 'checkbox',
  40. '#title' => t('Output raw HTML (requires <a href="http://drupal.org/project/devel">Devel</a> module).'),
  41. '#default_value' => module_exists('devel'),
  42. '#disabled' => !module_exists('devel'),
  43. );
  44. }
  45. /**
  46. * Render callback for Custom Formatters PHP engine.
  47. */
  48. function custom_formatters_engine_php_render($formatter, $obj_type, $object, $field, $instance, $langcode, $items, $display) {
  49. global $theme_path, $theme_info, $conf;
  50. // Store current theme path.
  51. $old_theme_path = $theme_path;
  52. // Restore theme_path to the theme, as long as php_eval() executes,
  53. // so code evaluated will not see the caller module as the current theme.
  54. // If theme info is not initialized get the path from theme_default.
  55. if (!isset($theme_info)) {
  56. $theme_path = drupal_get_path('theme', $conf['theme_default']);
  57. }
  58. else {
  59. $theme_path = dirname($theme_info->filename);
  60. }
  61. // Build variables array for formatter.
  62. $variables = array(
  63. '#obj_type' => $obj_type,
  64. '#object' => $object,
  65. '#field' => $field,
  66. '#instance' => $instance,
  67. '#langcode' => $langcode,
  68. '#items' => $items,
  69. '#display' => $display,
  70. );
  71. ob_start();
  72. $output = eval($formatter->code);
  73. $output = !empty($output) ? $output : ob_get_contents();
  74. ob_end_clean();
  75. // Preview debugging; Show the available variables data.
  76. if (module_exists('devel') && isset($formatter->preview) && $formatter->preview['options']['dpm']['vars']) {
  77. dpm($variables);
  78. }
  79. // Recover original theme path.
  80. $theme_path = $old_theme_path;
  81. return $output;
  82. }
  83. /**
  84. * Export callback for Custom Formatters PHP engine.
  85. */
  86. function custom_formatters_engine_php_export($item, $module) {
  87. $formatter = clone $item;
  88. if (isset($formatter->fapi)) {
  89. ob_start();
  90. eval($formatter->fapi);
  91. ob_get_clean();
  92. if (isset($form)) {
  93. $formatter->form = $form;
  94. }
  95. }
  96. // Modify #default_value for hook_field_formatter_settings_form() export.
  97. include drupal_get_path('module', 'form_builder') . '/examples/form_builder_examples.module';
  98. foreach (element_children($form) as $key) {
  99. if (isset($form[$key]['#default_value'])) {
  100. unset($form[$key]['#default_value']);
  101. }
  102. $form[$key]['#default_value'] = "[default-value::{$key}]";
  103. }
  104. $formatter->fapi = form_builder_examples_export_recurse($form);
  105. preg_match_all('/\'\[default-value::(.*)?\]\'/', $formatter->fapi, $matches, PREG_SET_ORDER);
  106. foreach ($matches as $match) {
  107. $formatter->fapi = str_replace($match[0], "\$settings['{$match[1]}']", $formatter->fapi);
  108. }
  109. return theme('custom_formatters_php_export', array('item' => $formatter, 'module' => $module));
  110. }
  111. /**
  112. * Help callback for Custom Formatters PHP engine.
  113. */
  114. function custom_formatters_engine_php_help() {
  115. return t('A PHP Formatter is essentially a standard Drupal API Field Formatter, the primary difference is that all availble parameters have been bundled into a <strong>$variables</strong> array for ease of use.') . "<br />\n"
  116. . t('The Formatter should be PHP code that returns either a value or <a href="http://drupal.org/node/930760">Render Array</a> and it should NOT include <em><?php ?></em> tags.') . "\n";
  117. }